Skip to content

Commit

Permalink
feat: deprecated AMIs should be discoverable when specifying ami id o…
Browse files Browse the repository at this point in the history
…n the amiSelectorTerms
  • Loading branch information
skagalwala committed Jul 15, 2024
1 parent fd7e3e4 commit be11695
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 9 deletions.
39 changes: 30 additions & 9 deletions pkg/providers/amifamily/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package amifamily
import (
"context"
"fmt"
"math"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -54,18 +55,37 @@ type DefaultProvider struct {
}

type AMI struct {
Name string
AmiID string
CreationDate string
Requirements scheduling.Requirements
Name string
AmiID string
CreationDate string
DeprecationTime string
Requirements scheduling.Requirements
}

type AMIs []AMI

// Sort orders the AMIs by creation date in descending order.
// If creation date is nil or two AMIs have the same creation date, the AMIs will be sorted by ID, which is guaranteed to be unique, in ascending order.
// If there are any amis with deprecation time, they will be sorted by deprecation time in descending order
// If there deprecation time is nil or two AMIs have the same deprecation time, the AMIs will be sorted by ID, which is guaranteed to be unique, in ascending order.
// finally, if creation date is nil or two AMIs have the same creation date, the AMIs will be sorted by ID, which is guaranteed to be unique, in ascending order.
func (a AMIs) Sort() {
maxDepTime := time.Unix(math.MaxInt64, 0)
sort.Slice(a, func(i, j int) bool {
ideptime := lo.TernaryF(
a[i].DeprecationTime != "",
func() time.Time { t := lo.Must(time.Parse(time.RFC3339, a[i].DeprecationTime)); return t },
func() time.Time { return maxDepTime },
)
jdeptime := lo.TernaryF(
a[j].DeprecationTime != "",
func() time.Time { t := lo.Must(time.Parse(time.RFC3339, a[j].DeprecationTime)); return t },
func() time.Time { return maxDepTime },
)

if ideptime.Unix() != jdeptime.Unix() {
return ideptime.Unix() > jdeptime.Unix()
}

itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
if itime.Unix() != jtime.Unix() {
Expand Down Expand Up @@ -213,10 +233,11 @@ func (p *DefaultProvider) getAMIs(ctx context.Context, terms []v1.AMISelectorTer
}
}
images[reqsHash] = AMI{
Name: lo.FromPtr(page.Images[i].Name),
AmiID: lo.FromPtr(page.Images[i].ImageId),
CreationDate: lo.FromPtr(page.Images[i].CreationDate),
Requirements: reqs,
Name: lo.FromPtr(page.Images[i].Name),
AmiID: lo.FromPtr(page.Images[i].ImageId),
CreationDate: lo.FromPtr(page.Images[i].CreationDate),
DeprecationTime: lo.FromPtr(page.Images[i].DeprecationTime),
Requirements: reqs,
}
}
return true
Expand Down
206 changes: 206 additions & 0 deletions pkg/providers/amifamily/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,212 @@ var _ = Describe("AMIProvider", func() {
},
))
})
It("should sort amis with deprecation time and creation date consistently", func() {
amis := amifamily.AMIs{
{
Name: "test-ami-5",
AmiID: "test-ami-5-id",
CreationDate: "2021-08-31T00:04:42.000Z",
DeprecationTime: "2021-09-01T00:06:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-4",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:06:42.000Z",
DeprecationTime: "2021-09-01T00:08:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-3",
AmiID: "test-ami-3-id",
CreationDate: "",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-2",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:08:42.000Z",
DeprecationTime: "",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
}

amis.Sort()
Expect(amis).To(Equal(
amifamily.AMIs{
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-2",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:08:42.000Z",
DeprecationTime: "",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-3",
AmiID: "test-ami-3-id",
CreationDate: "",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-4",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:06:42.000Z",
DeprecationTime: "2021-09-01T00:08:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-5",
AmiID: "test-ami-5-id",
CreationDate: "2021-08-31T00:04:42.000Z",
DeprecationTime: "2021-09-01T00:06:42.000Z",
Requirements: scheduling.NewRequirements(),
},
},
))
})
It("should sort non deprecated amis over deprecated amis with same creation time consistently", func() {
// If there are 2 AMIs with same creation date and one with a greater id is deprecated
// sort should priortize the non deprecated ami first
amis := amifamily.AMIs{
{
Name: "test-ami-3",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:12:42.000Z",
DeprecationTime: time.Now().Add(-1 * time.Minute).Format(time.RFC3339),
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-2",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-4",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: time.Now().Add(-2 * time.Minute).Format(time.RFC3339),
Requirements: scheduling.NewRequirements(),
},
}

amis.Sort()
Expect(amis).To(Equal(
amifamily.AMIs{
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-2",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-3",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:12:42.000Z",
DeprecationTime: time.Now().Add(-1 * time.Minute).Format(time.RFC3339),
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-4",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: time.Now().Add(-2 * time.Minute).Format(time.RFC3339),
Requirements: scheduling.NewRequirements(),
},
},
))
})
It("should sort only deprecated amis with the same name and deprecation time consistently", func() {
amis := amifamily.AMIs{
{
Name: "test-ami-1",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
}

amis.Sort()
Expect(amis).To(Equal(
amifamily.AMIs{
{
Name: "test-ami-1",
AmiID: "test-ami-1-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
DeprecationTime: "2021-08-31T00:12:42.000Z",
Requirements: scheduling.NewRequirements(),
},
},
))
})
})
})

Expand Down

0 comments on commit be11695

Please sign in to comment.