From 67e9e5ff35452ee3f052bb4149b52570174d52fb Mon Sep 17 00:00:00 2001 From: Jason Deal Date: Tue, 20 Aug 2024 22:48:09 -0700 Subject: [PATCH] fix: ensure alias version isn't dropped round-trip (#6777) --- pkg/apis/v1/ec2nodeclass_conversion.go | 18 +++++++++++++++++- pkg/apis/v1/ec2nodeclass_conversion_test.go | 14 ++++++++++++++ pkg/apis/v1/labels.go | 8 +++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/pkg/apis/v1/ec2nodeclass_conversion.go b/pkg/apis/v1/ec2nodeclass_conversion.go index 555e294c3f89..443e7c46c1e4 100644 --- a/pkg/apis/v1/ec2nodeclass_conversion.go +++ b/pkg/apis/v1/ec2nodeclass_conversion.go @@ -54,6 +54,17 @@ func (in *EC2NodeClass) ConvertTo(ctx context.Context, to apis.Convertible) erro v1beta1enc.Spec.AMIFamily = lo.ToPtr(in.AMIFamily()) } + if term, ok := lo.Find(in.Spec.AMISelectorTerms, func(term AMISelectorTerm) bool { + return term.Alias != "" + }); ok { + version := AMIVersionFromAlias(term.Alias) + if version != "latest" { + v1beta1enc.Annotations = lo.Assign(v1beta1enc.Annotations, map[string]string{ + AnnotationAliasVersionCompatibilityKey: version, + }) + } + } + in.Spec.convertTo(&v1beta1enc.Spec) in.Status.convertTo((&v1beta1enc.Status)) return nil @@ -132,6 +143,7 @@ func (in *EC2NodeClassStatus) convertTo(v1beta1enc *v1beta1.EC2NodeClassStatus) func (in *EC2NodeClass) ConvertFrom(ctx context.Context, from apis.Convertible) error { v1beta1enc := from.(*v1beta1.EC2NodeClass) in.ObjectMeta = v1beta1enc.ObjectMeta + in.Annotations = lo.OmitByKeys(in.Annotations, []string{AnnotationAliasVersionCompatibilityKey}) switch lo.FromPtr(v1beta1enc.Spec.AMIFamily) { case AMIFamilyAL2, AMIFamilyAL2023, AMIFamilyBottlerocket, AMIFamilyWindows2019, AMIFamilyWindows2022: @@ -140,7 +152,11 @@ func (in *EC2NodeClass) ConvertFrom(ctx context.Context, from apis.Convertible) if len(v1beta1enc.Spec.AMISelectorTerms) == 0 { in.Spec.AMIFamily = nil in.Spec.AMISelectorTerms = []AMISelectorTerm{{ - Alias: fmt.Sprintf("%s@latest", strings.ToLower(lo.FromPtr(v1beta1enc.Spec.AMIFamily))), + Alias: fmt.Sprintf( + "%s@%s", + strings.ToLower(lo.FromPtr(v1beta1enc.Spec.AMIFamily)), + lo.ValueOr(v1beta1enc.Annotations, AnnotationAliasVersionCompatibilityKey, "latest"), + ), }} } else { in.Spec.AMIFamily = v1beta1enc.Spec.AMIFamily diff --git a/pkg/apis/v1/ec2nodeclass_conversion_test.go b/pkg/apis/v1/ec2nodeclass_conversion_test.go index 772aa1aa3df1..ea7e97e60aab 100644 --- a/pkg/apis/v1/ec2nodeclass_conversion_test.go +++ b/pkg/apis/v1/ec2nodeclass_conversion_test.go @@ -120,6 +120,12 @@ var _ = Describe("Convert v1 to v1beta1 EC2NodeClass API", func() { Expect(v1ec2nodeclass.ConvertTo(ctx, v1beta1ec2nodeclass)).To(Succeed()) Expect(lo.FromPtr(v1beta1ec2nodeclass.Spec.AMIFamily)).To(Equal(v1beta1.AMIFamilyAL2023)) }) + It("should convert v1 ec2nodeclass alias (pinned)", func() { + v1ec2nodeclass.Spec.AMISelectorTerms = []AMISelectorTerm{{Alias: "al2023@v20240807"}} + Expect(v1ec2nodeclass.ConvertTo(ctx, v1beta1ec2nodeclass)).To(Succeed()) + Expect(lo.FromPtr(v1beta1ec2nodeclass.Spec.AMIFamily)).To(Equal(v1beta1.AMIFamilyAL2023)) + Expect(v1beta1ec2nodeclass.Annotations).To(HaveKeyWithValue(AnnotationAliasVersionCompatibilityKey, "v20240807")) + }) It("should convert v1 ec2nodeclass ami selector terms with the Ubuntu compatibility annotation", func() { v1ec2nodeclass.Annotations = lo.Assign(v1ec2nodeclass.Annotations, map[string]string{ AnnotationUbuntuCompatibilityKey: fmt.Sprintf("%s,%s", AnnotationUbuntuCompatibilityAMIFamily, AnnotationUbuntuCompatibilityBlockDeviceMappings), @@ -390,6 +396,14 @@ var _ = Describe("Convert v1beta1 to v1 EC2NodeClass API", func() { Expect(v1ec2nodeclass.ConvertFrom(ctx, v1beta1ec2nodeclass)).To(Succeed()) Expect(v1ec2nodeclass.Spec.AMISelectorTerms).To(ContainElement(AMISelectorTerm{Alias: "al2023@latest"})) }) + It("should convert v1beta1 ec2nodeclass ami family (alias version annotation)", func() { + v1beta1ec2nodeclass.Spec.AMIFamily = &v1beta1.AMIFamilyAL2023 + v1beta1ec2nodeclass.Annotations = lo.Assign(v1beta1ec2nodeclass.Annotations, map[string]string{ + AnnotationAliasVersionCompatibilityKey: "v20240807", + }) + Expect(v1ec2nodeclass.ConvertFrom(ctx, v1beta1ec2nodeclass)).To(Succeed()) + Expect(v1ec2nodeclass.Spec.AMISelectorTerms).To(ContainElement(AMISelectorTerm{Alias: "al2023@v20240807"})) + }) It("should convert v1beta1 ec2nodeclass ami family with non-custom ami family and ami selector terms", func() { v1beta1ec2nodeclass.Spec.AMIFamily = &v1beta1.AMIFamilyAL2023 v1beta1ec2nodeclass.Spec.AMISelectorTerms = []v1beta1.AMISelectorTerm{{ diff --git a/pkg/apis/v1/labels.go b/pkg/apis/v1/labels.go index fbdd6f65022e..63d6a6113ff5 100644 --- a/pkg/apis/v1/labels.go +++ b/pkg/apis/v1/labels.go @@ -127,7 +127,9 @@ var ( AnnotationUbuntuCompatibilityAMIFamily = "amiFamily" AnnotationUbuntuCompatibilityBlockDeviceMappings = "blockDeviceMappings" - TagNodeClaim = karpv1.Group + "/nodeclaim" - TagManagedLaunchTemplate = Group + "/cluster" - TagName = "Name" + TagNodeClaim = karpv1.Group + "/nodeclaim" + TagManagedLaunchTemplate = Group + "/cluster" + AnnotationAliasVersionCompatibilityKey = CompatibilityGroup + "/v1-alias-version" + + TagName = "Name" )