Skip to content

Commit

Permalink
fix: discover deprecated EKS optimized AMIs (#7248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdeal authored Oct 22, 2024
1 parent 6174c75 commit 883fbc0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions pkg/providers/amifamily/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions pkg/providers/amifamily/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down

0 comments on commit 883fbc0

Please sign in to comment.