Skip to content

Commit a0fc6b8

Browse files
authored
Merge pull request #2690 from k8s-infra-cherrypick-robot/cherry-pick-2672-to-release-1.4
[release-1.4] Fix AzureMachineTemplate roleAssignmentName validation
2 parents dc2829c + 0f1ddec commit a0fc6b8

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

api/v1beta1/azuremachine_validation.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ func ValidateAzureMachineSpec(spec AzureMachineSpec) field.ErrorList {
4242
allErrs = append(allErrs, errs...)
4343
}
4444

45-
if errs := ValidateSystemAssignedIdentity(spec.Identity, "", spec.RoleAssignmentName, field.NewPath("roleAssignmentName")); len(errs) > 0 {
46-
allErrs = append(allErrs, errs...)
47-
}
48-
4945
if errs := ValidateUserAssignedIdentity(spec.Identity, spec.UserAssignedIdentities, field.NewPath("userAssignedIdentities")); len(errs) > 0 {
5046
allErrs = append(allErrs, errs...)
5147
}

api/v1beta1/azuremachine_webhook.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ var _ webhook.Validator = &AzureMachine{}
4040

4141
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
4242
func (m *AzureMachine) ValidateCreate() error {
43-
if allErrs := ValidateAzureMachineSpec(m.Spec); len(allErrs) > 0 {
44-
return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
43+
spec := m.Spec
44+
45+
allErrs := ValidateAzureMachineSpec(spec)
46+
47+
if errs := ValidateSystemAssignedIdentity(spec.Identity, "", spec.RoleAssignmentName, field.NewPath("roleAssignmentName")); len(errs) > 0 {
48+
allErrs = append(allErrs, errs...)
4549
}
4650

47-
return nil
51+
if len(allErrs) == 0 {
52+
return nil
53+
}
54+
55+
return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
4856
}
4957

5058
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.

api/v1beta1/azuremachine_webhook_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,14 @@ func createMachineWithRoleAssignmentName() *AzureMachine {
627627
}
628628
return machine
629629
}
630+
631+
func createMachineWithoutRoleAssignmentName() *AzureMachine {
632+
machine := &AzureMachine{
633+
Spec: AzureMachineSpec{
634+
SSHPublicKey: validSSHPublicKey,
635+
OSDisk: validOSDisk,
636+
Identity: VMIdentitySystemAssigned,
637+
},
638+
}
639+
return machine
640+
}

api/v1beta1/azuremachinetemplate_webhook_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func TestAzureMachineTemplate_ValidateCreate(t *testing.T) {
129129
machineTemplate: createAzureMachineTemplateFromMachine(createMachineWithRoleAssignmentName()),
130130
wantErr: true,
131131
},
132+
{
133+
name: "azuremachinetemplate without RoleAssignmentName",
134+
machineTemplate: createAzureMachineTemplateFromMachine(createMachineWithoutRoleAssignmentName()),
135+
wantErr: false,
136+
},
132137
}
133138

134139
for _, test := range tests {

0 commit comments

Comments
 (0)