Skip to content

Commit

Permalink
fix: app stats not showing replicas of failing releases
Browse files Browse the repository at this point in the history
If an app has nothing but failing releases (or even just the first
release fails), the stats output should still show the failing replica
to simplify troubleshooting.
  • Loading branch information
ctrox committed Nov 22, 2024
1 parent 12064e1 commit 0d7fbe7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
42 changes: 36 additions & 6 deletions api/util/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,41 @@ func ValidatePEM(content string) (*string, error) {
}

func ApplicationLatestAvailableRelease(ctx context.Context, client *api.Client, app types.NamespacedName) (*apps.Release, error) {
releases, err := appReleases(ctx, client, app)
if err != nil {
return nil, err
}

release := latestAvailableRelease(releases)
if release == nil {
return nil, fmt.Errorf("no ready release found for application %s", app.Name)
}

return release, nil
}

// ApplicationLatestRelease returns the latest release of an app, prioritizing
// available releases and if no available release is found just the latest
// progressing or failed release.
func ApplicationLatestRelease(ctx context.Context, client *api.Client, app types.NamespacedName) (*apps.Release, error) {
releases, err := appReleases(ctx, client, app)
if err != nil {
return nil, err
}

release := latestAvailableRelease(releases)
if release == nil {
// in case no release is available, we just return the latest release in
// the list.
return &releases.Items[0], nil
}

return release, nil
}

// appReleases returns a release list of an app. If the returned error is nil,
// the release list is guaranteed to have at least one item.
func appReleases(ctx context.Context, client *api.Client, app types.NamespacedName) (*apps.ReleaseList, error) {
releases := &apps.ReleaseList{}
if err := client.List(
ctx,
Expand All @@ -317,12 +352,7 @@ func ApplicationLatestAvailableRelease(ctx context.Context, client *api.Client,
if len(releases.Items) == 0 {
return nil, fmt.Errorf("no releases found for application %s", app.Name)
}
release := latestAvailableRelease(releases)
if release == nil {
return nil, fmt.Errorf("no ready release found for application %s", app.Name)
}

return release, nil
return releases, nil
}

func latestAvailableRelease(releases *apps.ReleaseList) *apps.Release {
Expand Down
2 changes: 1 addition & 1 deletion get/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (cmd *applicationsCmd) printStats(ctx context.Context, c *api.Client, appLi
get.writeHeader(w, "NAME", "REPLICA", "STATUS", "CPU", "CPU%", "MEMORY", "MEMORY%", "RESTARTS", "LASTEXITCODE")

for _, app := range appList {
rel, err := util.ApplicationLatestAvailableRelease(ctx, c, api.ObjectName(&app))
rel, err := util.ApplicationLatestRelease(ctx, c, api.ObjectName(&app))
if err != nil {
format.PrintWarningf("unable to get replicas for app %s\n", c.Name(app.Name))
continue
Expand Down

0 comments on commit 0d7fbe7

Please sign in to comment.