diff --git a/azure/interfaces.go b/azure/interfaces.go index 345d7e15994..230fba9d90c 100644 --- a/azure/interfaces.go +++ b/azure/interfaces.go @@ -27,6 +27,7 @@ import ( // Reconciler is a generic interface used by components offering a type of service. // Example: virtualnetworks service would offer Reconcile/Delete methods. type Reconciler interface { + Name() string Reconcile(ctx context.Context) error Delete(ctx context.Context) error } diff --git a/azure/mock_azure/azure_mock.go b/azure/mock_azure/azure_mock.go index ba7053081b1..23ffdb56849 100644 --- a/azure/mock_azure/azure_mock.go +++ b/azure/mock_azure/azure_mock.go @@ -67,6 +67,20 @@ func (mr *MockReconcilerMockRecorder) Delete(ctx interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockReconciler)(nil).Delete), ctx) } +// Name mocks base method. +func (m *MockReconciler) Name() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Name") + ret0, _ := ret[0].(string) + return ret0 +} + +// Name indicates an expected call of Name. +func (mr *MockReconcilerMockRecorder) Name() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Name", reflect.TypeOf((*MockReconciler)(nil).Name)) +} + // Reconcile mocks base method. func (m *MockReconciler) Reconcile(ctx context.Context) error { m.ctrl.T.Helper() @@ -133,6 +147,20 @@ func (mr *MockCredentialGetterMockRecorder) GetCredentials(ctx, group, cluster i return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCredentials", reflect.TypeOf((*MockCredentialGetter)(nil).GetCredentials), ctx, group, cluster) } +// Name mocks base method. +func (m *MockCredentialGetter) Name() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Name") + ret0, _ := ret[0].(string) + return ret0 +} + +// Name indicates an expected call of Name. +func (mr *MockCredentialGetterMockRecorder) Name() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Name", reflect.TypeOf((*MockCredentialGetter)(nil).Name)) +} + // Reconcile mocks base method. func (m *MockCredentialGetter) Reconcile(ctx context.Context) error { m.ctrl.T.Helper() diff --git a/azure/services/agentpools/agentpools.go b/azure/services/agentpools/agentpools.go index 3ebd2758ca2..4f5e8020d0c 100644 --- a/azure/services/agentpools/agentpools.go +++ b/azure/services/agentpools/agentpools.go @@ -31,6 +31,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "agentpools" + // ManagedMachinePoolScope defines the scope interface for a managed machine pool. type ManagedMachinePoolScope interface { azure.ClusterDescriber @@ -57,6 +59,11 @@ func New(scope ManagedMachinePoolScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile idempotently creates or updates a agent pool, if possible. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger( diff --git a/azure/services/availabilitysets/availabilitysets.go b/azure/services/availabilitysets/availabilitysets.go index 280e8958714..7e10e9f121a 100644 --- a/azure/services/availabilitysets/availabilitysets.go +++ b/azure/services/availabilitysets/availabilitysets.go @@ -57,6 +57,11 @@ func New(scope AvailabilitySetScope, skuCache *resourceskus.Cache) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile creates or updates availability sets. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "availabilitysets.Service.Reconcile") diff --git a/azure/services/bastionhosts/bastionhosts.go b/azure/services/bastionhosts/bastionhosts.go index 396a663f67e..6888959221c 100644 --- a/azure/services/bastionhosts/bastionhosts.go +++ b/azure/services/bastionhosts/bastionhosts.go @@ -50,6 +50,11 @@ func New(scope BastionScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a bastion host. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "bastionhosts.Service.Reconcile") diff --git a/azure/services/disks/disks.go b/azure/services/disks/disks.go index 00e092558b7..fa0a60ab7ff 100644 --- a/azure/services/disks/disks.go +++ b/azure/services/disks/disks.go @@ -50,6 +50,11 @@ func New(scope DiskScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile on disk is currently no-op. OS disks should only be deleted and will create with the VM automatically. func (s *Service) Reconcile(ctx context.Context) error { _, _, done := tele.StartSpanWithLogger(ctx, "disks.Service.Reconcile") diff --git a/azure/services/groups/groups.go b/azure/services/groups/groups.go index e301df5de5b..bdaf2d64d7d 100644 --- a/azure/services/groups/groups.go +++ b/azure/services/groups/groups.go @@ -29,7 +29,7 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) -const serviceName = "group" +const ServiceName = "group" // Service provides operations on Azure resources. type Service struct { @@ -56,6 +56,11 @@ func New(scope GroupScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return ServiceName +} + // Reconcile gets/creates/updates a resource group. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "groups.Service.Reconcile") @@ -69,8 +74,8 @@ func (s *Service) Reconcile(ctx context.Context) error { return nil } - _, err := s.CreateResource(ctx, groupSpec, serviceName) - s.Scope.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, serviceName, err) + _, err := s.CreateResource(ctx, groupSpec, ServiceName) + s.Scope.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, ServiceName, err) return err } @@ -92,8 +97,8 @@ func (s *Service) Delete(ctx context.Context) error { if err != nil { if azure.ResourceNotFound(err) { // already deleted or doesn't exist, cleanup status and return. - s.Scope.DeleteLongRunningOperationState(groupSpec.ResourceName(), serviceName) - s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, serviceName, nil) + s.Scope.DeleteLongRunningOperationState(groupSpec.ResourceName(), ServiceName) + s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, nil) return nil } return errors.Wrap(err, "could not get resource group management state") @@ -103,8 +108,8 @@ func (s *Service) Delete(ctx context.Context) error { return azure.ErrNotOwned } - err = s.DeleteResource(ctx, groupSpec, serviceName) - s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, serviceName, err) + err = s.DeleteResource(ctx, groupSpec, ServiceName) + s.Scope.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, err) return err } diff --git a/azure/services/groups/groups_test.go b/azure/services/groups/groups_test.go index c3500c3141d..cca1e643bd6 100644 --- a/azure/services/groups/groups_test.go +++ b/azure/services/groups/groups_test.go @@ -74,8 +74,8 @@ func TestReconcileGroups(t *testing.T) { expectedError: "", expect: func(s *mock_groups.MockGroupScopeMockRecorder, m *mock_groups.MockclientMockRecorder, r *mock_async.MockReconcilerMockRecorder) { s.GroupSpec().Return(&fakeGroupSpec) - r.CreateResource(gomockinternal.AContext(), &fakeGroupSpec, serviceName).Return(nil, nil) - s.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, serviceName, nil) + r.CreateResource(gomockinternal.AContext(), &fakeGroupSpec, ServiceName).Return(nil, nil) + s.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, ServiceName, nil) }, }, { @@ -83,8 +83,8 @@ func TestReconcileGroups(t *testing.T) { expectedError: "#: Internal Server Error: StatusCode=500", expect: func(s *mock_groups.MockGroupScopeMockRecorder, m *mock_groups.MockclientMockRecorder, r *mock_async.MockReconcilerMockRecorder) { s.GroupSpec().Return(&fakeGroupSpec) - r.CreateResource(gomockinternal.AContext(), &fakeGroupSpec, serviceName).Return(nil, internalError) - s.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, serviceName, internalError) + r.CreateResource(gomockinternal.AContext(), &fakeGroupSpec, ServiceName).Return(nil, internalError) + s.UpdatePutStatus(infrav1.ResourceGroupReadyCondition, ServiceName, internalError) }, }, } @@ -140,8 +140,8 @@ func TestDeleteGroups(t *testing.T) { s.GroupSpec().AnyTimes().Return(&fakeGroupSpec) m.Get(gomockinternal.AContext(), &fakeGroupSpec).Return(sampleManagedGroup, nil) s.ClusterName().Return("test-cluster") - r.DeleteResource(gomockinternal.AContext(), &fakeGroupSpec, serviceName).Return(nil) - s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, serviceName, nil) + r.DeleteResource(gomockinternal.AContext(), &fakeGroupSpec, ServiceName).Return(nil) + s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, nil) }, }, { @@ -167,8 +167,8 @@ func TestDeleteGroups(t *testing.T) { 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(resources.Group{}, notFoundError) - s.DeleteLongRunningOperationState("test-group", serviceName) - s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, serviceName, nil) + s.DeleteLongRunningOperationState("test-group", ServiceName) + s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, nil) }, }, { @@ -178,8 +178,8 @@ func TestDeleteGroups(t *testing.T) { s.GroupSpec().AnyTimes().Return(&fakeGroupSpec) m.Get(gomockinternal.AContext(), &fakeGroupSpec).Return(sampleManagedGroup, nil) s.ClusterName().Return("test-cluster") - r.DeleteResource(gomockinternal.AContext(), &fakeGroupSpec, serviceName).Return(internalError) - s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, serviceName, gomockinternal.ErrStrEq("#: Internal Server Error: StatusCode=500")) + r.DeleteResource(gomockinternal.AContext(), &fakeGroupSpec, ServiceName).Return(internalError) + s.UpdateDeleteStatus(infrav1.ResourceGroupReadyCondition, ServiceName, gomockinternal.ErrStrEq("#: Internal Server Error: StatusCode=500")) }, }, } diff --git a/azure/services/inboundnatrules/inboundnatrules.go b/azure/services/inboundnatrules/inboundnatrules.go index c95a680f2ee..212ceee647c 100644 --- a/azure/services/inboundnatrules/inboundnatrules.go +++ b/azure/services/inboundnatrules/inboundnatrules.go @@ -54,6 +54,11 @@ func New(scope InboundNatScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates an inbound NAT rule. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "inboundnatrules.Service.Reconcile") diff --git a/azure/services/loadbalancers/loadbalancers.go b/azure/services/loadbalancers/loadbalancers.go index 3e5d6165fa5..2f69c157407 100644 --- a/azure/services/loadbalancers/loadbalancers.go +++ b/azure/services/loadbalancers/loadbalancers.go @@ -55,6 +55,11 @@ func New(scope LBScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a load balancer. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "loadbalancers.Service.Reconcile") diff --git a/azure/services/managedclusters/managedclusters.go b/azure/services/managedclusters/managedclusters.go index 464b9cd9473..78362a7e0bf 100644 --- a/azure/services/managedclusters/managedclusters.go +++ b/azure/services/managedclusters/managedclusters.go @@ -35,6 +35,8 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ) +const serviceName = "managedclusters" + var ( defaultUser = "azureuser" managedIdentity = "msi" @@ -145,6 +147,11 @@ func New(scope ManagedClusterScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile idempotently creates or updates a managed cluster, if possible. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "managedclusters.Service.Reconcile") diff --git a/azure/services/natgateways/natgateways.go b/azure/services/natgateways/natgateways.go index 1b64d94875c..1fd9ae7ed3d 100644 --- a/azure/services/natgateways/natgateways.go +++ b/azure/services/natgateways/natgateways.go @@ -53,6 +53,11 @@ func New(scope NatGatewayScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a NAT gateway. // Only when the NAT gateway 'Name' property is defined we create the NAT gateway: it's opt-in. func (s *Service) Reconcile(ctx context.Context) error { diff --git a/azure/services/networkinterfaces/networkinterfaces.go b/azure/services/networkinterfaces/networkinterfaces.go index bce8a6322f8..96d91f5dd87 100644 --- a/azure/services/networkinterfaces/networkinterfaces.go +++ b/azure/services/networkinterfaces/networkinterfaces.go @@ -53,6 +53,11 @@ func New(scope NICScope, skuCache *resourceskus.Cache) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a network interface. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "networkinterfaces.Service.Reconcile") diff --git a/azure/services/privatedns/privatedns.go b/azure/services/privatedns/privatedns.go index c87f9dba908..0ad47829328 100644 --- a/azure/services/privatedns/privatedns.go +++ b/azure/services/privatedns/privatedns.go @@ -28,6 +28,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "privatedns" + // Scope defines the scope interface for a private dns service. type Scope interface { azure.ClusterDescriber @@ -48,6 +50,11 @@ func New(scope Scope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile creates or updates the private zone, links it to the vnet, and creates DNS records. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "privatedns.Service.Reconcile") diff --git a/azure/services/publicips/publicips.go b/azure/services/publicips/publicips.go index a9be7749476..bd1a32dece6 100644 --- a/azure/services/publicips/publicips.go +++ b/azure/services/publicips/publicips.go @@ -29,6 +29,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "publicips" + // PublicIPScope defines the scope interface for a public IP service. type PublicIPScope interface { azure.ClusterDescriber @@ -49,6 +51,11 @@ func New(scope PublicIPScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a public ip. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "publicips.Service.Reconcile") diff --git a/azure/services/roleassignments/roleassignments.go b/azure/services/roleassignments/roleassignments.go index 239f7e5402c..dd360a3c760 100644 --- a/azure/services/roleassignments/roleassignments.go +++ b/azure/services/roleassignments/roleassignments.go @@ -31,7 +31,10 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) -const azureBuiltInContributorID = "b24988ac-6180-42a0-ab88-20f7382dd24c" +const ( + azureBuiltInContributorID = "b24988ac-6180-42a0-ab88-20f7382dd24c" + serviceName = "roleassignments" +) // RoleAssignmentScope defines the scope interface for a role assignment service. type RoleAssignmentScope interface { @@ -57,6 +60,11 @@ func New(scope RoleAssignmentScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile creates a role assignment. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "roleassignments.Service.Reconcile") diff --git a/azure/services/routetables/routetables.go b/azure/services/routetables/routetables.go index 456aa77c14d..02b763371fd 100644 --- a/azure/services/routetables/routetables.go +++ b/azure/services/routetables/routetables.go @@ -51,6 +51,11 @@ func New(scope RouteTableScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates route tables. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "routetables.Service.Reconcile") diff --git a/azure/services/scalesets/scalesets.go b/azure/services/scalesets/scalesets.go index 0f6dc793479..c1a81edec45 100644 --- a/azure/services/scalesets/scalesets.go +++ b/azure/services/scalesets/scalesets.go @@ -35,6 +35,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "scalesets" + type ( // ScaleSetScope defines the scope interface for a scale sets service. ScaleSetScope interface { @@ -68,6 +70,11 @@ func NewService(scope ScaleSetScope, skuCache *resourceskus.Cache) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile idempotently gets, creates, and updates a scale set. func (s *Service) Reconcile(ctx context.Context) (retErr error) { ctx, log, done := tele.StartSpanWithLogger(ctx, "scalesets.Service.Reconcile") diff --git a/azure/services/scalesetvms/scalesetvms.go b/azure/services/scalesetvms/scalesetvms.go index 85fc7e3a9ad..4d8b8852557 100644 --- a/azure/services/scalesetvms/scalesetvms.go +++ b/azure/services/scalesetvms/scalesetvms.go @@ -54,6 +54,11 @@ func NewService(scope ScaleSetVMScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile idempotently gets, creates, and updates a scale set. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "scalesetvms.Service.Reconcile") diff --git a/azure/services/securitygroups/securitygroups.go b/azure/services/securitygroups/securitygroups.go index 5a53d476817..f797b01393a 100644 --- a/azure/services/securitygroups/securitygroups.go +++ b/azure/services/securitygroups/securitygroups.go @@ -51,6 +51,11 @@ func New(scope NSGScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates network security groups. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "securitygroups.Service.Reconcile") diff --git a/azure/services/subnets/subnets.go b/azure/services/subnets/subnets.go index aa3a713e840..9af9429debc 100644 --- a/azure/services/subnets/subnets.go +++ b/azure/services/subnets/subnets.go @@ -55,6 +55,11 @@ func New(scope SubnetScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a subnet. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "subnets.Service.Reconcile") diff --git a/azure/services/tags/tags.go b/azure/services/tags/tags.go index e10998d508d..6361d9c83f8 100644 --- a/azure/services/tags/tags.go +++ b/azure/services/tags/tags.go @@ -27,6 +27,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "tags" + // TagScope defines the scope interface for a tags service. type TagScope interface { azure.Authorizer @@ -50,6 +52,11 @@ func New(scope TagScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile ensures tags are correct. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "tags.Service.Reconcile") diff --git a/azure/services/virtualmachines/virtualmachines.go b/azure/services/virtualmachines/virtualmachines.go index 57daeaa0678..a2ffa5fe9c3 100644 --- a/azure/services/virtualmachines/virtualmachines.go +++ b/azure/services/virtualmachines/virtualmachines.go @@ -67,6 +67,11 @@ func New(scope VMScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a virtual machine. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachines.Service.Reconcile") diff --git a/azure/services/virtualnetworks/virtualnetworks.go b/azure/services/virtualnetworks/virtualnetworks.go index 8206e52cfc0..4cc9bd89d6d 100644 --- a/azure/services/virtualnetworks/virtualnetworks.go +++ b/azure/services/virtualnetworks/virtualnetworks.go @@ -59,6 +59,11 @@ func New(scope VNetScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualnetworks.Service.Reconcile") defer done() diff --git a/azure/services/vmextensions/vmextensions.go b/azure/services/vmextensions/vmextensions.go index 63b15e01642..b77ade4ad4d 100644 --- a/azure/services/vmextensions/vmextensions.go +++ b/azure/services/vmextensions/vmextensions.go @@ -26,6 +26,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "vmextensions" + // VMExtensionScope defines the scope interface for a vm extension service. type VMExtensionScope interface { azure.ClusterDescriber @@ -47,6 +49,11 @@ func New(scope VMExtensionScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile creates or updates the VM extension. func (s *Service) Reconcile(ctx context.Context) error { ctx, log, done := tele.StartSpanWithLogger(ctx, "vmextensions.Service.Reconcile") diff --git a/azure/services/vmssextensions/vmssextensions.go b/azure/services/vmssextensions/vmssextensions.go index 0c0c87c0454..cc34dc240f1 100644 --- a/azure/services/vmssextensions/vmssextensions.go +++ b/azure/services/vmssextensions/vmssextensions.go @@ -25,6 +25,8 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) +const serviceName = "vmssextensions" + // VMSSExtensionScope defines the scope interface for a vmss extension service. type VMSSExtensionScope interface { azure.ClusterDescriber @@ -46,6 +48,11 @@ func New(scope VMSSExtensionScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile creates or updates the VMSS extension. func (s *Service) Reconcile(ctx context.Context) error { _, _, done := tele.StartSpanWithLogger(ctx, "vmssextensions.Service.Reconcile") diff --git a/azure/services/vnetpeerings/vnetpeerings.go b/azure/services/vnetpeerings/vnetpeerings.go index 0aa995eda87..6321a203b9b 100644 --- a/azure/services/vnetpeerings/vnetpeerings.go +++ b/azure/services/vnetpeerings/vnetpeerings.go @@ -50,6 +50,11 @@ func New(scope VnetPeeringScope) *Service { } } +// Name returns the service name. +func (s *Service) Name() string { + return serviceName +} + // Reconcile gets/creates/updates a peering. func (s *Service) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "vnetpeerings.Service.Reconcile") diff --git a/controllers/azurecluster_reconciler.go b/controllers/azurecluster_reconciler.go index 8aa16141897..4ad6fd5a503 100644 --- a/controllers/azurecluster_reconciler.go +++ b/controllers/azurecluster_reconciler.go @@ -74,8 +74,6 @@ func newAzureClusterService(scope *scope.ClusterScope) (*azureClusterService, er }, nil } -var _ azure.Reconciler = (*azureClusterService)(nil) - // Reconcile reconciles all the services in a predetermined order. func (s *azureClusterService) Reconcile(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureClusterService.Reconcile") @@ -90,7 +88,7 @@ func (s *azureClusterService) Reconcile(ctx context.Context) error { for _, service := range s.services { if err := service.Reconcile(ctx); err != nil { - return errors.Wrap(err, "failed to reconcile AzureCluster service") + return errors.Wrapf(err, "failed to reconcile AzureCluster service %s", service.Name()) } } @@ -102,13 +100,17 @@ func (s *azureClusterService) Delete(ctx context.Context) error { ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.azureClusterService.Delete") defer done() - if err := s.services[0].Delete(ctx); err != nil { + groupSvc, err := s.getService(groups.ServiceName) + if err != nil { + return err + } + if err := groupSvc.Delete(ctx); err != nil { if errors.Is(err, azure.ErrNotOwned) { // If the resource group is not managed we need to delete resources inside the group one by one. // services are deleted in reverse order from the order in which they are reconciled. for i := len(s.services) - 1; i >= 1; i-- { if err := s.services[i].Delete(ctx); err != nil { - return errors.Wrap(err, "failed to delete AzureCluster service") + return errors.Wrapf(err, "failed to delete AzureCluster service %s", s.services[i].Name()) } } } else { @@ -119,6 +121,15 @@ func (s *azureClusterService) Delete(ctx context.Context) error { return nil } +func (s *azureClusterService) getService(name string) (azure.Reconciler, error) { + for _, service := range s.services { + if service.Name() == name { + return service, nil + } + } + return nil, errors.Errorf("service %s not found", name) +} + // setFailureDomainsForLocation sets the AzureCluster Status failure domains based on which Azure Availability Zones are available in the cluster location. // Note that this is not done in a webhook as it requires API calls to fetch the availability zones. func (s *azureClusterService) setFailureDomainsForLocation(ctx context.Context) error { diff --git a/controllers/azurecluster_reconciler_test.go b/controllers/azurecluster_reconciler_test.go index 0c19efea599..eb6155acdd1 100644 --- a/controllers/azurecluster_reconciler_test.go +++ b/controllers/azurecluster_reconciler_test.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/cluster-api-provider-azure/azure" "sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure" "sigs.k8s.io/cluster-api-provider-azure/azure/scope" + "sigs.k8s.io/cluster-api-provider-azure/azure/services/groups" "sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus" gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -48,11 +49,12 @@ func TestAzureClusterServiceReconcile(t *testing.T) { }, }, "service reconcile fails": { - expectedError: "failed to reconcile AzureCluster service: some error happened", + expectedError: "failed to reconcile AzureCluster service two: some error happened", expect: func(one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( one.Reconcile(gomockinternal.AContext()).Return(nil), - two.Reconcile(gomockinternal.AContext()).Return(errors.New("some error happened"))) + two.Reconcile(gomockinternal.AContext()).Return(errors.New("some error happened")), + two.Name().Return("two")) }, }, } @@ -104,6 +106,7 @@ func TestAzureClusterServiceDelete(t *testing.T) { expectedError: "", expect: func(grp *mock_azure.MockReconcilerMockRecorder, one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( + grp.Name().Return(groups.ServiceName), grp.Delete(gomockinternal.AContext()).Return(nil)) }, }, @@ -111,6 +114,7 @@ func TestAzureClusterServiceDelete(t *testing.T) { expectedError: "failed to delete resource group: internal error", expect: func(grp *mock_azure.MockReconcilerMockRecorder, one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( + grp.Name().Return(groups.ServiceName), grp.Delete(gomockinternal.AContext()).Return(errors.New("internal error"))) }, }, @@ -118,6 +122,7 @@ func TestAzureClusterServiceDelete(t *testing.T) { expectedError: "", expect: func(grp *mock_azure.MockReconcilerMockRecorder, one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( + grp.Name().Return(groups.ServiceName), grp.Delete(gomockinternal.AContext()).Return(azure.ErrNotOwned), three.Delete(gomockinternal.AContext()).Return(nil), two.Delete(gomockinternal.AContext()).Return(nil), @@ -125,12 +130,14 @@ func TestAzureClusterServiceDelete(t *testing.T) { }, }, "service delete fails": { - expectedError: "failed to delete AzureCluster service: some error happened", + expectedError: "failed to delete AzureCluster service two: some error happened", expect: func(grp *mock_azure.MockReconcilerMockRecorder, one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( + grp.Name().Return(groups.ServiceName), grp.Delete(gomockinternal.AContext()).Return(azure.ErrNotOwned), three.Delete(gomockinternal.AContext()).Return(nil), - two.Delete(gomockinternal.AContext()).Return(errors.New("some error happened"))) + two.Delete(gomockinternal.AContext()).Return(errors.New("some error happened")), + two.Name().Return("two")) }, }, } diff --git a/controllers/azuremachine_reconciler.go b/controllers/azuremachine_reconciler.go index 08c4ee1ec3d..474c5f98aae 100644 --- a/controllers/azuremachine_reconciler.go +++ b/controllers/azuremachine_reconciler.go @@ -44,8 +44,6 @@ type azureMachineService struct { skuCache *resourceskus.Cache } -var _ azure.Reconciler = (*azureMachineService)(nil) - // newAzureMachineService populates all the services based on input scope. func newAzureMachineService(machineScope *scope.MachineScope) (*azureMachineService, error) { cache, err := resourceskus.GetCache(machineScope, machineScope.Location()) @@ -81,7 +79,7 @@ func (s *azureMachineService) Reconcile(ctx context.Context) error { for _, service := range s.services { if err := service.Reconcile(ctx); err != nil { - return errors.Wrap(err, "failed to reconcile AzureMachine service") + return errors.Wrapf(err, "failed to reconcile AzureMachine service %s", service.Name()) } } @@ -96,7 +94,7 @@ func (s *azureMachineService) Delete(ctx context.Context) error { // Delete services in reverse order of creation. for i := len(s.services) - 1; i >= 0; i-- { if err := s.services[i].Delete(ctx); err != nil { - return errors.Wrap(err, "failed to delete AzureMachine service") + return errors.Wrapf(err, "failed to delete AzureMachine service %s", s.services[i].Name()) } } diff --git a/controllers/azuremachine_reconciler_test.go b/controllers/azuremachine_reconciler_test.go index 5725ceccefd..e13d851b93b 100644 --- a/controllers/azuremachine_reconciler_test.go +++ b/controllers/azuremachine_reconciler_test.go @@ -48,11 +48,12 @@ func TestAzureMachineServiceReconcile(t *testing.T) { }, }, "service reconcile fails": { - expectedError: "failed to reconcile AzureMachine service: some error happened", + expectedError: "failed to reconcile AzureMachine service foo: some error happened", expect: func(one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( one.Reconcile(gomockinternal.AContext()).Return(nil), - two.Reconcile(gomockinternal.AContext()).Return(errors.New("some error happened"))) + two.Reconcile(gomockinternal.AContext()).Return(errors.New("some error happened")), + two.Name().Return("foo")) }, }, } @@ -118,11 +119,12 @@ func TestAzureMachineServiceDelete(t *testing.T) { }, }, "service delete fails": { - expectedError: "failed to delete AzureMachine service: some error happened", + expectedError: "failed to delete AzureMachine service test-service-two: some error happened", expect: func(one *mock_azure.MockReconcilerMockRecorder, two *mock_azure.MockReconcilerMockRecorder, three *mock_azure.MockReconcilerMockRecorder) { gomock.InOrder( three.Delete(gomockinternal.AContext()).Return(nil), - two.Delete(gomockinternal.AContext()).Return(errors.New("some error happened"))) + two.Delete(gomockinternal.AContext()).Return(errors.New("some error happened")), + two.Name().Return("test-service-two")) }, }, } diff --git a/exp/controllers/azuremachinepool_reconciler.go b/exp/controllers/azuremachinepool_reconciler.go index 6793c45a394..5bd2e9f573e 100644 --- a/exp/controllers/azuremachinepool_reconciler.go +++ b/exp/controllers/azuremachinepool_reconciler.go @@ -38,8 +38,6 @@ type azureMachinePoolService struct { vmssExtensionSvc azure.Reconciler } -var _ azure.Reconciler = (*azureMachinePoolService)(nil) - // newAzureMachinePoolService populates all the services based on input scope. func newAzureMachinePoolService(machinePoolScope *scope.MachinePoolScope) (*azureMachinePoolService, error) { cache, err := resourceskus.GetCache(machinePoolScope, machinePoolScope.Location()) diff --git a/exp/controllers/azuremachinepoolmachine_controller.go b/exp/controllers/azuremachinepoolmachine_controller.go index 2cff54672cc..921773ee290 100644 --- a/exp/controllers/azuremachinepoolmachine_controller.go +++ b/exp/controllers/azuremachinepoolmachine_controller.go @@ -48,7 +48,7 @@ import ( ) type ( - azureMachinePoolMachineReconcilerFactory func(*scope.MachinePoolMachineScope) azure.Reconciler + azureMachinePoolMachineReconcilerFactory func(*scope.MachinePoolMachineScope) *azureMachinePoolMachineReconciler // AzureMachinePoolMachineController handles Kubernetes change events for AzureMachinePoolMachine resources. AzureMachinePoolMachineController struct { @@ -339,7 +339,7 @@ func (ampmr *AzureMachinePoolMachineController) reconcileDelete(ctx context.Cont return reconcile.Result{}, nil } -func newAzureMachinePoolMachineReconciler(scope *scope.MachinePoolMachineScope) azure.Reconciler { +func newAzureMachinePoolMachineReconciler(scope *scope.MachinePoolMachineScope) *azureMachinePoolMachineReconciler { return &azureMachinePoolMachineReconciler{ Scope: scope, scalesetVMsService: scalesetvms.NewService(scope),