From f8f8d7b18e8b9913cff9ba5446b403a27a7ad21e Mon Sep 17 00:00:00 2001 From: Yassine TIJANI Date: Mon, 8 May 2023 18:33:28 +0400 Subject: [PATCH 1/2] allow the defaulting of InstanceMetadataOptions to go through for AWSMachineTemplates Signed-off-by: Yassine TIJANI --- api/v1beta2/awsmachinetemplate_webhook.go | 10 ++++++++- .../awsmachinetemplate_webhook_test.go | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/api/v1beta2/awsmachinetemplate_webhook.go b/api/v1beta2/awsmachinetemplate_webhook.go index abe59f3b12..6d1ac3f7d5 100644 --- a/api/v1beta2/awsmachinetemplate_webhook.go +++ b/api/v1beta2/awsmachinetemplate_webhook.go @@ -227,7 +227,15 @@ func (r *AWSMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw r var allErrs field.ErrorList if !topology.ShouldSkipImmutabilityChecks(req, newAWSMachineTemplate) && !cmp.Equal(newAWSMachineTemplate.Spec, oldAWSMachineTemplate.Spec) { - allErrs = append(allErrs, field.Invalid(field.NewPath("spec"), newAWSMachineTemplate, "AWSMachineTemplate.Spec is immutable")) + if oldAWSMachineTemplate.Spec.Template.Spec.InstanceMetadataOptions == nil { + oldAWSMachineTemplate.Spec.Template.Spec.InstanceMetadataOptions = newAWSMachineTemplate.Spec.Template.Spec.InstanceMetadataOptions + } + + if !cmp.Equal(newAWSMachineTemplate.Spec.Template.Spec, oldAWSMachineTemplate.Spec.Template.Spec) { + allErrs = append(allErrs, + field.Invalid(field.NewPath("AWSMachineTemplate", "spec", "template", "spec"), newAWSMachineTemplate, "AWSMachineTemplate.Spec is immutable"), + ) + } } return aggregateObjErrors(newAWSMachineTemplate.GroupVersionKind().GroupKind(), newAWSMachineTemplate.Name, allErrs) diff --git a/api/v1beta2/awsmachinetemplate_webhook_test.go b/api/v1beta2/awsmachinetemplate_webhook_test.go index 7180633a05..5e9e9c13f6 100644 --- a/api/v1beta2/awsmachinetemplate_webhook_test.go +++ b/api/v1beta2/awsmachinetemplate_webhook_test.go @@ -116,6 +116,27 @@ func TestAWSMachineTemplateValidateUpdate(t *testing.T) { }, wantError: true, }, + { + name: "allow defaulted values to update", + modifiedTemplate: &AWSMachineTemplate{ + ObjectMeta: metav1.ObjectMeta{}, + Spec: AWSMachineTemplateSpec{ + Template: AWSMachineTemplateResource{ + Spec: AWSMachineSpec{ + CloudInit: CloudInit{}, + InstanceType: "test", + InstanceMetadataOptions: &InstanceMetadataOptions{ + HTTPEndpoint: InstanceMetadataEndpointStateEnabled, + HTTPPutResponseHopLimit: 1, + HTTPTokens: HTTPTokensStateRequired, + InstanceMetadataTags: InstanceMetadataEndpointStateDisabled, + }, + }, + }, + }, + }, + wantError: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 4b01746e2111fb49ce087c38df5f99b44f2ec6ee Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Tue, 9 May 2023 08:05:48 +0200 Subject: [PATCH 2/2] Removed extra field tag --- api/v1beta2/awsmachinetemplate_webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v1beta2/awsmachinetemplate_webhook.go b/api/v1beta2/awsmachinetemplate_webhook.go index 6d1ac3f7d5..98c3a5a7ec 100644 --- a/api/v1beta2/awsmachinetemplate_webhook.go +++ b/api/v1beta2/awsmachinetemplate_webhook.go @@ -233,7 +233,7 @@ func (r *AWSMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw r if !cmp.Equal(newAWSMachineTemplate.Spec.Template.Spec, oldAWSMachineTemplate.Spec.Template.Spec) { allErrs = append(allErrs, - field.Invalid(field.NewPath("AWSMachineTemplate", "spec", "template", "spec"), newAWSMachineTemplate, "AWSMachineTemplate.Spec is immutable"), + field.Invalid(field.NewPath("spec", "template", "spec"), newAWSMachineTemplate, "AWSMachineTemplate.Spec is immutable"), ) } }