Skip to content

Commit

Permalink
Fix AzureMachineTemplate roleAssignmentName validation
Browse files Browse the repository at this point in the history
  • Loading branch information
majimenez-stratio committed Sep 30, 2022
1 parent d28ff9a commit 311bf50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 0 additions & 4 deletions api/v1beta1/azuremachine_validation.go
Expand Up @@ -42,10 +42,6 @@ func ValidateAzureMachineSpec(spec AzureMachineSpec) field.ErrorList {
allErrs = append(allErrs, errs...)
}

if errs := ValidateSystemAssignedIdentity(spec.Identity, "", spec.RoleAssignmentName, field.NewPath("roleAssignmentName")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}

if errs := ValidateUserAssignedIdentity(spec.Identity, spec.UserAssignedIdentities, field.NewPath("userAssignedIdentities")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
Expand Down
14 changes: 11 additions & 3 deletions api/v1beta1/azuremachine_webhook.go
Expand Up @@ -40,11 +40,19 @@ var _ webhook.Validator = &AzureMachine{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (m *AzureMachine) ValidateCreate() error {
if allErrs := ValidateAzureMachineSpec(m.Spec); len(allErrs) > 0 {
return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
spec := m.Spec

allErrs := ValidateAzureMachineSpec(spec)

if errs := ValidateSystemAssignedIdentity(spec.Identity, "", spec.RoleAssignmentName, field.NewPath("roleAssignmentName")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}

return nil
if len(allErrs) == 0 {
return nil
}

return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
Expand Down
11 changes: 11 additions & 0 deletions api/v1beta1/azuremachine_webhook_test.go
Expand Up @@ -627,3 +627,14 @@ func createMachineWithRoleAssignmentName() *AzureMachine {
}
return machine
}

func createMachineWithoutRoleAssignmentName() *AzureMachine {
machine := &AzureMachine{
Spec: AzureMachineSpec{
SSHPublicKey: validSSHPublicKey,
OSDisk: validOSDisk,
Identity: VMIdentitySystemAssigned,
},
}
return machine
}
5 changes: 5 additions & 0 deletions api/v1beta1/azuremachinetemplate_webhook_test.go
Expand Up @@ -133,6 +133,11 @@ func TestAzureMachineTemplate_ValidateCreate(t *testing.T) {
machineTemplate: createAzureMachineTemplateFromMachine(createMachineWithRoleAssignmentName()),
wantErr: true,
},
{
name: "azuremachinetemplate without RoleAssignmentName",
machineTemplate: createAzureMachineTemplateFromMachine(createMachineWithoutRoleAssignmentName()),
wantErr: false,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 311bf50

Please sign in to comment.