Skip to content

Commit

Permalink
fix nil exception
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh Kumar <[email protected]>
  • Loading branch information
sonasingh46 committed Mar 24, 2022
1 parent 2488e92 commit 1ad6487
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions azure/services/roleassignments/roleassignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions azure/services/roleassignments/roleassignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"))
},
},
Expand All @@ -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{
Expand Down Expand Up @@ -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")
Expand All @@ -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"))
},
Expand All @@ -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")
Expand Down

0 comments on commit 1ad6487

Please sign in to comment.