-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add asogroups #3574
Merged
Merged
add asogroups #3574
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package asogroups | ||
|
||
import ( | ||
"context" | ||
|
||
asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601" | ||
"github.com/pkg/errors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-azure/azure" | ||
"sigs.k8s.io/cluster-api-provider-azure/azure/services/aso" | ||
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler" | ||
"sigs.k8s.io/cluster-api-provider-azure/util/tele" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// ServiceName is the name of this service. | ||
const ServiceName = "group" | ||
|
||
// Service provides operations on Azure resources. | ||
type Service struct { | ||
Scope GroupScope | ||
aso.Reconciler | ||
} | ||
|
||
// GroupScope defines the scope interface for a group service. | ||
type GroupScope interface { | ||
azure.AsyncStatusUpdater | ||
ASOGroupSpec() azure.ASOResourceSpecGetter | ||
GetClient() client.Client | ||
} | ||
|
||
// New creates a new service. | ||
func New(scope GroupScope) *Service { | ||
return &Service{ | ||
Scope: scope, | ||
Reconciler: aso.New(scope.GetClient()), | ||
} | ||
} | ||
|
||
// Name returns the service name. | ||
func (s *Service) Name() string { | ||
return ServiceName | ||
} | ||
|
||
// Reconcile idempotently creates or updates a resource group. | ||
func (s *Service) Reconcile(ctx context.Context) error { | ||
ctx, _, done := tele.StartSpanWithLogger(ctx, "groups.Service.Reconcile") | ||
defer done() | ||
|
||
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout) | ||
defer cancel() | ||
|
||
groupSpec := s.Scope.ASOGroupSpec() | ||
if groupSpec == nil { | ||
return nil | ||
} | ||
|
||
_, err := s.CreateOrUpdateResource(ctx, groupSpec, ServiceName) | ||
s.Scope.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, ServiceName, err) | ||
return err | ||
} | ||
|
||
// Delete deletes the resource group if it is managed by capz. | ||
func (s *Service) Delete(ctx context.Context) error { | ||
ctx, log, done := tele.StartSpanWithLogger(ctx, "groups.Service.Delete") | ||
defer done() | ||
|
||
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout) | ||
defer cancel() | ||
|
||
groupSpec := s.Scope.ASOGroupSpec() | ||
if groupSpec == nil { | ||
return nil | ||
} | ||
|
||
// check that the resource group is not BYO. | ||
managed, err := s.IsManaged(ctx) | ||
if err != nil { | ||
nojnhuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if apierrors.IsNotFound(err) { | ||
// already deleted or doesn't exist, cleanup status and return. | ||
s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, nil) | ||
return nil | ||
} | ||
return errors.Wrap(err, "could not get resource group management state") | ||
} | ||
if !managed { | ||
log.V(2).Info("Skipping resource group deletion in unmanaged mode") | ||
return nil | ||
} | ||
|
||
err = s.DeleteResource(ctx, groupSpec, ServiceName) | ||
s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, err) | ||
return err | ||
} | ||
|
||
// IsManaged returns true if the resource group has an owned tag with the cluster name as value, | ||
// meaning that the resource group's lifecycle is managed. | ||
func (s *Service) IsManaged(ctx context.Context) (bool, error) { | ||
ctx, log, done := tele.StartSpanWithLogger(ctx, "groups.Service.IsManaged") | ||
defer done() | ||
|
||
spec := s.Scope.ASOGroupSpec() | ||
group := &asoresourcesv1.ResourceGroup{} | ||
err := s.Scope.GetClient().Get(ctx, client.ObjectKeyFromObject(spec.ResourceRef()), group) | ||
if err != nil { | ||
log.Error(err, "error getting resource group") | ||
return false, err | ||
} | ||
|
||
return true, nil // not yet implemented | ||
nojnhuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion for code organization: instead of prefixing every folder name with
aso
it might make sense to reorganize the services folder with two sub-folders:sdk
andaso
(we can even addsdkv2
if needed).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will be the only service to have both ASO and SDK versions in the repo at the same time. Once we're ready for more services, I think the way the proposal describes of transitioning each service in-place will be easiest. Do you think it's still important to distinguish that in the folder structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so long as we're able to do a targeted, quick revert (ideally just click the github revert PR button) per service it shouldn't matter. If one or the other makes that easier then I'd vote for that.
Sorry this didn't get hashed out in the proposal PR, probably this is too low level for being declared in the proposal, I should have caught that, if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nojnhuh if it's a one-off and not long-lived then probably fine as-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CecileRobertMichon Sounds good. We can always adjust later too if needed.
@jackfrancis I think reverting in this case would be as simple as reverting the relevant commits, but we'll verify that as part of #3546.