diff --git a/azure/services/roleassignments/roleassignments.go b/azure/services/roleassignments/roleassignments.go index ef250fc0980..79c5c7e508f 100644 --- a/azure/services/roleassignments/roleassignments.go +++ b/azure/services/roleassignments/roleassignments.go @@ -20,6 +20,7 @@ import ( "context" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-04-01/compute" + "github.com/Azure/go-autorest/autorest/to" "github.com/pkg/errors" "sigs.k8s.io/cluster-api-provider-azure/azure" "sigs.k8s.io/cluster-api-provider-azure/azure/services/async" @@ -73,6 +74,12 @@ func (s *Service) Reconcile(ctx context.Context) error { defer cancel() log.V(2).Info("reconciling role assignment") + // Return early if there is no roleassignment spec to reconcile. + if len(s.Scope.RoleAssignmentSpecs(to.StringPtr(""))) < 1 { + log.V(2).Info("no role assignment spec to reconcile") + return nil + } + var principalID *string resourceType := s.Scope.RoleAssignmentResourceType() switch resourceType { diff --git a/azure/services/roleassignments/roleassignments_test.go b/azure/services/roleassignments/roleassignments_test.go index 92419b068e2..7e72d0a5bae 100644 --- a/azure/services/roleassignments/roleassignments_test.go +++ b/azure/services/roleassignments/roleassignments_test.go @@ -72,7 +72,8 @@ func TestReconcileRoleAssignmentsVM(t *testing.T) { s.ResourceGroup().Return("my-rg") s.Name().Return(fakeRoleAssignment1.MachineName) s.RoleAssignmentResourceType().Return("VirtualMachine") - s.RoleAssignmentSpecs(&fakePrincipalID).AnyTimes().Return(fakeRoleAssignmentSpecs[:1]) + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[:1]) + s.RoleAssignmentSpecs(&fakePrincipalID).Return(fakeRoleAssignmentSpecs[:1]) m.Get(gomockinternal.AContext(), &fakeVMSpec).Return(compute.VirtualMachine{ Identity: &compute.VirtualMachineIdentity{ PrincipalID: &fakePrincipalID, @@ -91,7 +92,7 @@ func TestReconcileRoleAssignmentsVM(t *testing.T) { s.ResourceGroup().Return("my-rg") s.Name().Return(fakeRoleAssignment1.MachineName) s.RoleAssignmentResourceType().Return("VirtualMachine") - s.RoleAssignmentSpecs(&fakePrincipalID).AnyTimes().Return(fakeRoleAssignmentSpecs[:1]) + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[:1]) m.Get(gomockinternal.AContext(), &fakeVMSpec).Return(compute.VirtualMachine{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")) }, }, @@ -105,6 +106,7 @@ func TestReconcileRoleAssignmentsVM(t *testing.T) { s.ResourceGroup().Return("my-rg") s.Name().Return(fakeRoleAssignment1.MachineName) s.RoleAssignmentResourceType().Return("VirtualMachine") + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[0:1]) s.RoleAssignmentSpecs(&fakePrincipalID).Return(fakeRoleAssignmentSpecs[0:1]) m.Get(gomockinternal.AContext(), &fakeVMSpec).Return(compute.VirtualMachine{ Identity: &compute.VirtualMachineIdentity{ @@ -160,6 +162,7 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) { expect: func(s *mock_roleassignments.MockRoleAssignmentScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, mvmss *mock_scalesets.MockClientMockRecorder) { + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[1:2]) s.RoleAssignmentSpecs(&fakePrincipalID).Return(fakeRoleAssignmentSpecs[1:2]) s.RoleAssignmentResourceType().Return(azure.VirtualMachineScaleSet) s.ResourceGroup().Return("my-rg") @@ -181,6 +184,7 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) { s.RoleAssignmentResourceType().Return(azure.VirtualMachineScaleSet) s.ResourceGroup().Return("my-rg") s.Name().Return("test-vmss") + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[1:2]) mvmss.Get(gomockinternal.AContext(), "my-rg", "test-vmss").Return(compute.VirtualMachineScaleSet{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 500}, "Internal Server Error")) }, @@ -191,6 +195,7 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) { expect: func(s *mock_roleassignments.MockRoleAssignmentScopeMockRecorder, r *mock_async.MockReconcilerMockRecorder, mvmss *mock_scalesets.MockClientMockRecorder) { + s.RoleAssignmentSpecs(to.StringPtr("")).Return(fakeRoleAssignmentSpecs[1:2]) s.RoleAssignmentSpecs(&fakePrincipalID).Return(fakeRoleAssignmentSpecs[1:2]) s.RoleAssignmentResourceType().Return(azure.VirtualMachineScaleSet) s.ResourceGroup().Return("my-rg")