Skip to content

Commit

Permalink
Add IsManaged to service reconciler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecile Robert-Michon committed Mar 9, 2022
1 parent 4e0dca5 commit cbbe562
Show file tree
Hide file tree
Showing 36 changed files with 211 additions and 109 deletions.
3 changes: 0 additions & 3 deletions azure/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

// ErrNotOwned is returned when a resource can't be deleted because it isn't owned.
var ErrNotOwned = errors.New("resource is not managed and cannot be deleted")

const codeResourceGroupNotFound = "ResourceGroupNotFound"

// ResourceGroupNotFound parses the error to check if it's a resource group not found error.
Expand Down
1 change: 1 addition & 0 deletions azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Reconciler interface {
// ServiceReconciler is an Azure service reconciler which can reconcile an Azure service.
type ServiceReconciler interface {
Name() string
IsManaged(ctx context.Context) (bool, error)
Reconciler
}

Expand Down
15 changes: 15 additions & 0 deletions azure/mock_azure/azure_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,6 @@ func (s *ClusterScope) UpdateDeleteStatus(condition clusterv1.ConditionType, ser
switch {
case err == nil:
conditions.MarkFalse(s.AzureCluster, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureCluster, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
Expand All @@ -874,8 +872,6 @@ func (s *ClusterScope) UpdatePutStatus(condition clusterv1.ConditionType, servic
switch {
case err == nil:
conditions.MarkTrue(s.AzureCluster, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureCluster, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
Expand All @@ -888,8 +884,6 @@ func (s *ClusterScope) UpdatePatchStatus(condition clusterv1.ConditionType, serv
switch {
case err == nil:
conditions.MarkTrue(s.AzureCluster, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureCluster, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
Expand Down
6 changes: 0 additions & 6 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,6 @@ func (m *MachineScope) UpdateDeleteStatus(condition clusterv1.ConditionType, ser
switch {
case err == nil:
conditions.MarkFalse(m.AzureMachine, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachine, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
Expand All @@ -679,8 +677,6 @@ func (m *MachineScope) UpdatePutStatus(condition clusterv1.ConditionType, servic
switch {
case err == nil:
conditions.MarkTrue(m.AzureMachine, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachine, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
Expand All @@ -693,8 +689,6 @@ func (m *MachineScope) UpdatePatchStatus(condition clusterv1.ConditionType, serv
switch {
case err == nil:
conditions.MarkTrue(m.AzureMachine, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachine, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
Expand Down
6 changes: 0 additions & 6 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,6 @@ func (m *MachinePoolScope) UpdateDeleteStatus(condition clusterv1.ConditionType,
switch {
case err == nil:
conditions.MarkFalse(m.AzureMachinePool, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachinePool, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
Expand All @@ -640,8 +638,6 @@ func (m *MachinePoolScope) UpdatePutStatus(condition clusterv1.ConditionType, se
switch {
case err == nil:
conditions.MarkTrue(m.AzureMachinePool, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachinePool, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
Expand All @@ -654,8 +650,6 @@ func (m *MachinePoolScope) UpdatePatchStatus(condition clusterv1.ConditionType,
switch {
case err == nil:
conditions.MarkTrue(m.AzureMachinePool, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(m.AzureMachinePool, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
Expand Down
6 changes: 0 additions & 6 deletions azure/scope/machinepoolmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ func (s *MachinePoolMachineScope) UpdateDeleteStatus(condition clusterv1.Conditi
switch {
case err == nil:
conditions.MarkFalse(s.AzureMachinePoolMachine, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureMachinePoolMachine, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
Expand All @@ -198,8 +196,6 @@ func (s *MachinePoolMachineScope) UpdatePutStatus(condition clusterv1.ConditionT
switch {
case err == nil:
conditions.MarkTrue(s.AzureMachinePoolMachine, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureMachinePoolMachine, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
Expand All @@ -212,8 +208,6 @@ func (s *MachinePoolMachineScope) UpdatePatchStatus(condition clusterv1.Conditio
switch {
case err == nil:
conditions.MarkTrue(s.AzureMachinePoolMachine, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.AzureMachinePoolMachine, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
Expand Down
6 changes: 0 additions & 6 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,6 @@ func (s *ManagedControlPlaneScope) UpdateDeleteStatus(condition clusterv1.Condit
switch {
case err == nil:
conditions.MarkFalse(s.PatchTarget, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.PatchTarget, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
Expand All @@ -709,8 +707,6 @@ func (s *ManagedControlPlaneScope) UpdatePutStatus(condition clusterv1.Condition
switch {
case err == nil:
conditions.MarkTrue(s.PatchTarget, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.PatchTarget, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
Expand All @@ -723,8 +719,6 @@ func (s *ManagedControlPlaneScope) UpdatePatchStatus(condition clusterv1.Conditi
switch {
case err == nil:
conditions.MarkTrue(s.PatchTarget, condition)
case errors.Is(err, azure.ErrNotOwned):
// do nothing
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.PatchTarget, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
Expand Down
5 changes: 5 additions & 0 deletions azure/services/availabilitysets/availabilitysets.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.AvailabilitySetReadyCondition, serviceName, resultingErr)
return resultingErr
}

// IsManaged returns always returns true as CAPZ does not support BYO availability set.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions azure/services/bastionhosts/bastionhosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.BastionHostReadyCondition, serviceName, resultingErr)
return resultingErr
}

// IsManaged returns always returns true as CAPZ does not support BYO bastion.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions azure/services/disks/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.DisksReadyCondition, serviceName, result)
return result
}

// IsManaged returns always returns true as CAPZ does not support BYO disk.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
12 changes: 6 additions & 6 deletions azure/services/groups/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *Service) Delete(ctx context.Context) error {
}

// check that the resource group is not BYO.
managed, err := s.IsGroupManaged(ctx)
managed, err := s.IsManaged(ctx)
if err != nil {
if azure.ResourceNotFound(err) {
// already deleted or doesn't exist, cleanup status and return.
Expand All @@ -104,19 +104,19 @@ func (s *Service) Delete(ctx context.Context) error {
return errors.Wrap(err, "could not get resource group management state")
}
if !managed {
log.V(2).Info("Should not delete resource group in unmanaged mode")
return azure.ErrNotOwned
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
}

// IsGroupManaged returns true if the resource group has an owned tag with the cluster name as value,
// 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) IsGroupManaged(ctx context.Context) (bool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "groups.Service.IsGroupManaged")
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "groups.Service.IsManaged")
defer done()

groupSpec := s.Scope.GroupSpec()
Expand Down
3 changes: 1 addition & 2 deletions azure/services/groups/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
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/async/mock_async"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/groups/mock_groups"
gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestDeleteGroups(t *testing.T) {
},
{
name: "resource group is not managed by capz",
expectedError: azure.ErrNotOwned.Error(),
expectedError: "",
expect: func(s *mock_groups.MockGroupScopeMockRecorder, m *mock_groups.MockclientMockRecorder, r *mock_async.MockReconcilerMockRecorder) {
s.GroupSpec().AnyTimes().Return(&fakeGroupSpec)
m.Get(gomockinternal.AContext(), &fakeGroupSpec).Return(sampleBYOGroup, nil)
Expand Down
5 changes: 5 additions & 0 deletions azure/services/inboundnatrules/inboundnatrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.InboundNATRulesReadyCondition, serviceName, result)
return result
}

// IsManaged returns always returns true as CAPZ does not support BYO inbound NAT rules.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions azure/services/loadbalancers/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.LoadBalancersReadyCondition, serviceName, result)
return result
}

// IsManaged returns always returns true as CAPZ does not support BYO load balancers.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
5 changes: 5 additions & 0 deletions azure/services/managedclusters/managedclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,8 @@ func (s *Service) Delete(ctx context.Context) error {
klog.V(2).Infof("successfully deleted managed cluster %s ", s.Scope.ClusterName())
return nil
}

// IsManaged returns always returns true as CAPZ does not support BYO managed cluster.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
return true, nil
}
20 changes: 14 additions & 6 deletions azure/services/natgateways/natgateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (s *Service) Reconcile(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout)
defer cancel()

if !s.Scope.Vnet().IsManaged(s.Scope.ClusterName()) {
if managed, err := s.IsManaged(ctx); err == nil && !managed {
log.V(4).Info("Skipping nat gateways reconcile in custom vnet mode")

s.Scope.UpdatePutStatus(infrav1.NATGatewaysReadyCondition, serviceName, nil)
return nil
} else {
errors.Wrap(err, "failed to check if NAT gateways are managed")
}

// We go through the list of NatGatewaySpecs to reconcile each one, independently of the resultingErr of the previous one.
Expand Down Expand Up @@ -115,11 +115,11 @@ func (s *Service) Delete(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultAzureServiceReconcileTimeout)
defer cancel()

if !s.Scope.Vnet().IsManaged(s.Scope.ClusterName()) {
if managed, err := s.IsManaged(ctx); err == nil && !managed {
log.V(4).Info("Skipping nat gateway deletion in custom vnet mode")

s.Scope.UpdateDeleteStatus(infrav1.NATGatewaysReadyCondition, serviceName, nil)
return nil
} else if err != nil {
return errors.Wrap(err, "failed to check if NAT gateways are managed")
}

specs := s.Scope.NatGatewaySpecs()
Expand All @@ -141,3 +141,11 @@ func (s *Service) Delete(ctx context.Context) error {
s.Scope.UpdateDeleteStatus(infrav1.NATGatewaysReadyCondition, serviceName, resultingErr)
return resultingErr
}

// IsManaged returns true if the NAT gateways' lifecycles are managed.
func (s *Service) IsManaged(ctx context.Context) (bool, error) {
_, _, done := tele.StartSpanWithLogger(ctx, "natgateways.Service.IsManaged")
defer done()

return s.Scope.IsVnetManaged(), nil
}
Loading

0 comments on commit cbbe562

Please sign in to comment.