Skip to content

Commit

Permalink
fix: correct AMI ordering function (#6164)
Browse files Browse the repository at this point in the history
  • Loading branch information
njtran authored and jonathan-innis committed May 15, 2024
1 parent 8b08944 commit cf4b13d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
9 changes: 2 additions & 7 deletions pkg/providers/amifamily/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,15 @@ type AMI struct {
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 name in ascending 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.
func (a AMIs) Sort() {
sort.Slice(a, func(i, j int) bool {
itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
if itime.Unix() != jtime.Unix() {
return itime.Unix() > jtime.Unix()
}
if a[i].Name != a[j].Name {
return a[i].Name < a[j].Name
}
iHash, _ := hashstructure.Hash(a[i].Requirements, hashstructure.FormatV2, &hashstructure.HashOptions{})
jHash, _ := hashstructure.Hash(a[i].Requirements, hashstructure.FormatV2, &hashstructure.HashOptions{})
return iHash < jHash
return a[i].AmiID < a[j].AmiID
})
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/providers/amifamily/ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,64 @@ var _ = Describe("AMIProvider", func() {
},
))
})
It("should sort amis with the same name and creation date consistently", func() {
amis := amifamily.AMIs{
{
Name: "test-ami-1",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
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:10: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",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-2-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-3-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
{
Name: "test-ami-1",
AmiID: "test-ami-4-id",
CreationDate: "2021-08-31T00:10:42.000Z",
Requirements: scheduling.NewRequirements(),
},
},
))
})
})
})

Expand Down

0 comments on commit cf4b13d

Please sign in to comment.