Skip to content

Commit de34751

Browse files
authored
Merge pull request #3194 from willie-yao/fix-machinepool-default
MachinePools: Fix AzureMachinePool default for SystemAssignedIdentityRole
2 parents ebd399b + 3d73050 commit de34751

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

api/v1beta1/azuremachine_default_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ func TestAzureMachineSpec_SetIdentityDefaults(t *testing.T) {
7979
DefinitionID: fakeRoleDefinitionID,
8080
},
8181
}}}
82-
82+
deprecatedRoleAssignmentNameTest := test{machine: &AzureMachine{Spec: AzureMachineSpec{
83+
Identity: VMIdentitySystemAssigned,
84+
RoleAssignmentName: existingRoleAssignmentName,
85+
}}}
8386
emptyTest := test{machine: &AzureMachine{Spec: AzureMachineSpec{
8487
Identity: VMIdentitySystemAssigned,
8588
SystemAssignedIdentityRole: &SystemAssignedIdentityRole{},
@@ -95,6 +98,10 @@ func TestAzureMachineSpec_SetIdentityDefaults(t *testing.T) {
9598
g.Expect(systemAssignedIdentityRoleExistTest.machine.Spec.SystemAssignedIdentityRole.Scope).To(Equal(fakeScope))
9699
g.Expect(systemAssignedIdentityRoleExistTest.machine.Spec.SystemAssignedIdentityRole.DefinitionID).To(Equal(fakeRoleDefinitionID))
97100

101+
deprecatedRoleAssignmentNameTest.machine.Spec.SetIdentityDefaults(fakeSubscriptionID)
102+
g.Expect(deprecatedRoleAssignmentNameTest.machine.Spec.SystemAssignedIdentityRole.Name).To(Equal(existingRoleAssignmentName))
103+
g.Expect(deprecatedRoleAssignmentNameTest.machine.Spec.RoleAssignmentName).To(BeEmpty())
104+
98105
emptyTest.machine.Spec.SetIdentityDefaults(fakeSubscriptionID)
99106
g.Expect(emptyTest.machine.Spec.SystemAssignedIdentityRole.Name).To(Not(BeEmpty()))
100107
_, err := uuid.Parse(emptyTest.machine.Spec.SystemAssignedIdentityRole.Name)

exp/api/v1beta1/azuremachinepool_default.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ func (amp *AzureMachinePool) SetIdentityDefaults(subscriptionID string) {
7272
return
7373
}
7474
if amp.Spec.Identity == infrav1.VMIdentitySystemAssigned {
75-
if amp.Spec.RoleAssignmentName == "" {
76-
amp.Spec.RoleAssignmentName = string(uuid.NewUUID())
77-
}
7875
if amp.Spec.SystemAssignedIdentityRole == nil {
7976
amp.Spec.SystemAssignedIdentityRole = &infrav1.SystemAssignedIdentityRole{}
8077
}
81-
if amp.Spec.SystemAssignedIdentityRole.Name == "" {
78+
if amp.Spec.RoleAssignmentName != "" {
79+
amp.Spec.SystemAssignedIdentityRole.Name = amp.Spec.RoleAssignmentName
80+
amp.Spec.RoleAssignmentName = ""
81+
} else if amp.Spec.SystemAssignedIdentityRole.Name == "" {
8282
amp.Spec.SystemAssignedIdentityRole.Name = string(uuid.NewUUID())
83-
if amp.Spec.RoleAssignmentName != "" {
84-
amp.Spec.SystemAssignedIdentityRole.Name = amp.Spec.RoleAssignmentName
85-
amp.Spec.RoleAssignmentName = ""
86-
}
8783
}
8884
if amp.Spec.SystemAssignedIdentityRole.Scope == "" {
8985
// Default scope to the subscription.

exp/api/v1beta1/azuremachinepool_default_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func TestAzureMachinePool_SetIdentityDefaults(t *testing.T) {
7575
Scope: fakeScope,
7676
},
7777
}}}
78+
deprecatedRoleAssignmentNameTest := test{machinePool: &AzureMachinePool{Spec: AzureMachinePoolSpec{
79+
Identity: infrav1.VMIdentitySystemAssigned,
80+
RoleAssignmentName: existingRoleAssignmentName,
81+
}}}
7882
emptyTest := test{machinePool: &AzureMachinePool{Spec: AzureMachinePoolSpec{
7983
Identity: infrav1.VMIdentitySystemAssigned,
8084
SystemAssignedIdentityRole: &infrav1.SystemAssignedIdentityRole{},
@@ -90,6 +94,10 @@ func TestAzureMachinePool_SetIdentityDefaults(t *testing.T) {
9094
g.Expect(systemAssignedIdentityRoleExistTest.machinePool.Spec.SystemAssignedIdentityRole.Scope).To(Equal(fakeScope))
9195
g.Expect(systemAssignedIdentityRoleExistTest.machinePool.Spec.SystemAssignedIdentityRole.DefinitionID).To(Equal(fakeRoleDefinitionID))
9296

97+
deprecatedRoleAssignmentNameTest.machinePool.SetIdentityDefaults(fakeSubscriptionID)
98+
g.Expect(deprecatedRoleAssignmentNameTest.machinePool.Spec.SystemAssignedIdentityRole.Name).To(Equal(existingRoleAssignmentName))
99+
g.Expect(deprecatedRoleAssignmentNameTest.machinePool.Spec.RoleAssignmentName).To(BeEmpty())
100+
93101
emptyTest.machinePool.SetIdentityDefaults(fakeSubscriptionID)
94102
g.Expect(emptyTest.machinePool.Spec.SystemAssignedIdentityRole.Name).To(Not(BeEmpty()))
95103
_, err := uuid.Parse(emptyTest.machinePool.Spec.SystemAssignedIdentityRole.Name)

exp/api/v1beta1/azuremachinepool_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (amp *AzureMachinePool) ValidateSystemAssignedIdentity(old runtime.Object)
208208
// ValidateSystemAssignedIdentityRole validates the scope and roleDefinitionID for the system-assigned identity.
209209
func (amp *AzureMachinePool) ValidateSystemAssignedIdentityRole() error {
210210
var allErrs field.ErrorList
211-
if amp.Spec.RoleAssignmentName != "" && amp.Spec.SystemAssignedIdentityRole.Name != "" {
211+
if amp.Spec.RoleAssignmentName != "" && amp.Spec.SystemAssignedIdentityRole != nil && amp.Spec.SystemAssignedIdentityRole.Name != "" {
212212
allErrs = append(allErrs, field.Invalid(field.NewPath("systemAssignedIdentityRole"), amp.Spec.SystemAssignedIdentityRole.Name, "cannot set both roleAssignmentName and systemAssignedIdentityRole.name"))
213213
}
214214
if amp.Spec.Identity == infrav1.VMIdentitySystemAssigned {

0 commit comments

Comments
 (0)