From 883fbc08336d358fa661afdf7b077bf3b3963135 Mon Sep 17 00:00:00 2001 From: Jason Deal Date: Tue, 22 Oct 2024 15:20:04 -0700 Subject: [PATCH] fix: discover deprecated EKS optimized AMIs (#7248) --- .../templates/karpenter.k8s.aws_ec2nodeclasses.yaml | 2 +- pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml | 2 +- pkg/providers/amifamily/ami.go | 11 ++++++++--- pkg/providers/amifamily/types.go | 9 +++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/charts/karpenter-crd/templates/karpenter.k8s.aws_ec2nodeclasses.yaml b/charts/karpenter-crd/templates/karpenter.k8s.aws_ec2nodeclasses.yaml index 6aec8a6f5d08..5b1b6f8eadce 100644 --- a/charts/karpenter-crd/templates/karpenter.k8s.aws_ec2nodeclasses.yaml +++ b/charts/karpenter-crd/templates/karpenter.k8s.aws_ec2nodeclasses.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.16.3 + controller-gen.kubebuilder.io/version: v0.16.4 name: ec2nodeclasses.karpenter.k8s.aws spec: group: karpenter.k8s.aws diff --git a/pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml b/pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml index 685003f1a477..c942189eb616 100644 --- a/pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml +++ b/pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.16.3 + controller-gen.kubebuilder.io/version: v0.16.4 name: ec2nodeclasses.karpenter.k8s.aws spec: group: karpenter.k8s.aws diff --git a/pkg/providers/amifamily/ami.go b/pkg/providers/amifamily/ami.go index 7fae92e8b086..871559d71e07 100644 --- a/pkg/providers/amifamily/ami.go +++ b/pkg/providers/amifamily/ami.go @@ -69,7 +69,12 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) if err != nil { return nil, fmt.Errorf("getting AMI queries, %w", err) } - amis, err := p.amis(ctx, queries) + // Discover deprecated AMIs if automatic AMI discovery and upgrade is enabled. This ensures we'll be able to + // provision in the event of an EKS optimized AMI being deprecated. + includeDeprecated := lo.ContainsBy(nodeClass.Spec.AMISelectorTerms, func(term v1.AMISelectorTerm) bool { + return term.Alias == "latest" + }) + amis, err := p.amis(ctx, queries, includeDeprecated) if err != nil { return nil, err } @@ -145,7 +150,7 @@ func (p *DefaultProvider) DescribeImageQueries(ctx context.Context, nodeClass *v } //nolint:gocyclo -func (p *DefaultProvider) amis(ctx context.Context, queries []DescribeImageQuery) (AMIs, error) { +func (p *DefaultProvider) amis(ctx context.Context, queries []DescribeImageQuery, includeDeprecated bool) (AMIs, error) { hash, err := hashstructure.Hash(queries, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}) if err != nil { return nil, err @@ -157,7 +162,7 @@ func (p *DefaultProvider) amis(ctx context.Context, queries []DescribeImageQuery } images := map[uint64]AMI{} for _, query := range queries { - if err = p.ec2api.DescribeImagesPagesWithContext(ctx, query.DescribeImagesInput(), func(page *ec2.DescribeImagesOutput, _ bool) bool { + if err = p.ec2api.DescribeImagesPagesWithContext(ctx, query.DescribeImagesInput(includeDeprecated), func(page *ec2.DescribeImagesOutput, _ bool) bool { for _, image := range page.Images { arch, ok := v1.AWSToKubeArchitectures[lo.FromPtr(image.Architecture)] if !ok { diff --git a/pkg/providers/amifamily/types.go b/pkg/providers/amifamily/types.go index 5dae3150a1cc..95caa67985a2 100644 --- a/pkg/providers/amifamily/types.go +++ b/pkg/providers/amifamily/types.go @@ -99,12 +99,13 @@ type DescribeImageQuery struct { KnownRequirements map[string][]scheduling.Requirements } -func (q DescribeImageQuery) DescribeImagesInput() *ec2.DescribeImagesInput { +func (q DescribeImageQuery) DescribeImagesInput(includeDeprecated bool) *ec2.DescribeImagesInput { return &ec2.DescribeImagesInput{ // Don't include filters in the Describe Images call as EC2 API doesn't allow empty filters. - Filters: lo.Ternary(len(q.Filters) > 0, q.Filters, nil), - Owners: lo.Ternary(len(q.Owners) > 0, lo.ToSlicePtr(q.Owners), nil), - MaxResults: aws.Int64(1000), + Filters: lo.Ternary(len(q.Filters) > 0, q.Filters, nil), + Owners: lo.Ternary(len(q.Owners) > 0, lo.ToSlicePtr(q.Owners), nil), + IncludeDeprecated: lo.ToPtr(includeDeprecated), + MaxResults: aws.Int64(1000), } }