Skip to content

Commit

Permalink
fix: release sort order
Browse files Browse the repository at this point in the history
The OrderReleaseList returns the latest release last, which is not what
the comment on the function says. Because of this, exec sometimes fails
if there are two releases available (can happen when switching to a new
release).
  • Loading branch information
ctrox committed Oct 10, 2024
1 parent a5f4f5b commit 278043e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions api/util/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
apps "github.com/ninech/apis/apps/v1alpha1"
)

// OrderReleaseList orders the given list of releases, moving the latest
// release to the beginning of the list
func OrderReleaseList(releaseList *apps.ReleaseList) {
// OrderReleaseList orders the given list of releases first by name and then by
// creation timestamp latest to oldest. Reverse reverses the order by creation
// timestamp to oldest to latest.
func OrderReleaseList(releaseList *apps.ReleaseList, reverse bool) {
if len(releaseList.Items) <= 1 {
return
}
Expand All @@ -21,6 +22,9 @@ func OrderReleaseList(releaseList *apps.ReleaseList) {
return applicationNameI < applicationNameJ
}

return releaseList.Items[i].CreationTimestampNano < releaseList.Items[j].CreationTimestampNano
if reverse {
return releaseList.Items[i].CreationTimestampNano < releaseList.Items[j].CreationTimestampNano
}
return releaseList.Items[i].CreationTimestampNano > releaseList.Items[j].CreationTimestampNano
})
}
2 changes: 1 addition & 1 deletion exec/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client, exec *Cm
}

func latestAvailableRelease(releases *apps.ReleaseList) *apps.Release {
util.OrderReleaseList(releases)
util.OrderReleaseList(releases, false)
for _, release := range releases.Items {
if release.Status.AtProvider.ReleaseStatus == apps.ReleaseProcessStatusAvailable {
return &release
Expand Down
2 changes: 1 addition & 1 deletion get/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (cmd *releasesCmd) Run(ctx context.Context, client *api.Client, get *Cmd) e
return nil
}

util.OrderReleaseList(releaseList)
util.OrderReleaseList(releaseList, true)

switch get.Output {
case full:
Expand Down

0 comments on commit 278043e

Please sign in to comment.