Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't default RoleAssignmentName on machine templates #2111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/v1beta1/azuremachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,15 @@ func createMachineWithOsDiskCacheType(cacheType string) *AzureMachine {
machine.Spec.OSDisk.CachingType = cacheType
return machine
}

func createMachineWithRoleAssignmentName() *AzureMachine {
machine := &AzureMachine{
Spec: AzureMachineSpec{
SSHPublicKey: validSSHPublicKey,
OSDisk: validOSDisk,
Identity: VMIdentitySystemAssigned,
RoleAssignmentName: "c6e3443d-bc11-4335-8819-ab6637b10586",
},
}
return machine
}
26 changes: 21 additions & 5 deletions api/v1beta1/azuremachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
)

// AzureMachineTemplateImmutableMsg ...
const AzureMachineTemplateImmutableMsg = "AzureMachineTemplate spec.template.spec field is immutable. Please create new resource instead. ref doc: https://cluster-api.sigs.k8s.io/tasks/change-machine-template.html"
const (
AzureMachineTemplateImmutableMsg = "AzureMachineTemplate spec.template.spec field is immutable. Please create new resource instead. ref doc: https://cluster-api.sigs.k8s.io/tasks/change-machine-template.html"
AzureMachineTemplateRoleAssignmentNameMsg = "AzureMachineTemplate spec.template.spec.roleAssignmentName field can't be set"
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (r *AzureMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -46,10 +49,19 @@ var _ webhook.Validator = &AzureMachineTemplate{}
func (r *AzureMachineTemplate) ValidateCreate() error {
spec := r.Spec.Template.Spec

if allErrs := ValidateAzureMachineSpec(spec); len(allErrs) > 0 {
return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), r.Name, allErrs)
allErrs := ValidateAzureMachineSpec(spec)

if r.Spec.Template.Spec.RoleAssignmentName != "" {
allErrs = append(allErrs,
field.Invalid(field.NewPath("AzureMachineTemplate", "spec", "template", "spec", "roleAssignmentName"), r, AzureMachineTemplateRoleAssignmentNameMsg),
)
}
return nil

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

return apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
Expand Down Expand Up @@ -92,5 +104,9 @@ func (r *AzureMachineTemplate) ValidateDelete() error {

// Default implements webhookutil.defaulter so a webhook will be registered for the type.
func (r *AzureMachineTemplate) Default() {
r.Spec.Template.Spec.SetDefaults()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than changing this here, shouldn't we update the func (s *AzureMachineSpec) SetDefaults() method directly in api/v1beta1/azuremachine_default.go? That SetDefaults() method is also invoked in api/v1beta1/azuremachine_webhook.go (in the func (m *AzureMachine) Default() method)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to default the RoleAssignmentName to a random UUID in the AzureMachine object, that's fine because all VMs would have a different one. It only doesn't make sense at the AzureMachineTemplate level.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, makes sense

if err := r.Spec.Template.Spec.SetDefaultSSHPublicKey(); err != nil {
ctrl.Log.WithName("SetDefault").Error(err, "SetDefaultSSHPublicKey failed")
}
r.Spec.Template.Spec.SetDefaultCachingType()
r.Spec.Template.Spec.SetDataDisksDefaults()
}
5 changes: 5 additions & 0 deletions api/v1beta1/azuremachinetemplate_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func TestAzureMachineTemplate_ValidateCreate(t *testing.T) {
),
wantErr: true,
},
{
name: "azuremachinetemplate with RoleAssignmentName",
machineTemplate: createAzureMachineTemplateFromMachine(createMachineWithRoleAssignmentName()),
wantErr: true,
},
}

for _, test := range tests {
Expand Down