Skip to content

Commit 4eceda3

Browse files
committed
stack status cmd - add columns for image build date and VCS reference
When dealing with snapshot builds that are constantly updated, it's difficult to know what build of a service is really running. When viewing historic build logs from CI this information is important to know. This adds information about the service to the output of `elastic-package stack status`. The data is read from http://label-schema.org/rc1/ labels. It uses the org.label-schema.build-date and org.label-schema.vcs-ref labels. ``` BEFORE: ╭──────────────────┬─────────────────┬─────────────────────╮ │ SERVICE │ VERSION │ STATUS │ ├──────────────────┼─────────────────┼─────────────────────┤ │ elastic-agent │ 8.16.0-SNAPSHOT │ running (unhealthy) │ │ elasticsearch │ 8.16.0-SNAPSHOT │ running (healthy) │ │ fleet-server │ 8.16.0-SNAPSHOT │ running (healthy) │ │ kibana │ 8.16.0-SNAPSHOT │ running (healthy) │ │ package-registry │ latest │ running (healthy) │ ╰──────────────────┴─────────────────┴─────────────────────╯ ``` ``` AFTER: ╭──────────────────┬─────────────────┬─────────────────────┬───────────────────┬────────────╮ │ SERVICE │ VERSION │ STATUS │ IMAGE BUILD DATE │ VCS REF │ ├──────────────────┼─────────────────┼─────────────────────┼───────────────────┼────────────┤ │ elastic-agent │ 8.16.0-SNAPSHOT │ running (unhealthy) │ 2024-08-22T02:44Z │ b96a4ca8fa │ │ elasticsearch │ 8.16.0-SNAPSHOT │ running (healthy) │ 2024-08-22T13:26Z │ 1362d56865 │ │ fleet-server │ 8.16.0-SNAPSHOT │ running (healthy) │ 2024-08-22T02:44Z │ b96a4ca8fa │ │ kibana │ 8.16.0-SNAPSHOT │ running (healthy) │ 2024-08-22T11:09Z │ cdcdfddd3f │ │ package-registry │ latest │ running (healthy) │ │ │ ╰──────────────────┴─────────────────┴─────────────────────┴───────────────────┴────────────╯ ```
1 parent 0d3c215 commit 4eceda3

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

cmd/stack.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cmd
77
import (
88
"fmt"
99
"strings"
10+
"time"
1011

1112
"github.com/jedib0t/go-pretty/table"
1213

@@ -350,11 +351,27 @@ func printStatus(cmd *cobra.Command, servicesStatus []stack.ServiceStatus) {
350351
return
351352
}
352353
t := table.NewWriter()
353-
t.AppendHeader(table.Row{"Service", "Version", "Status"})
354+
t.AppendHeader(table.Row{"Service", "Version", "Status", "Image Build Date", "VCS Ref"})
354355

355356
for _, service := range servicesStatus {
356-
t.AppendRow(table.Row{service.Name, service.Version, service.Status})
357+
t.AppendRow(table.Row{service.Name, service.Version, service.Status, formatTime(service.Labels.BuildDate), truncate(service.Labels.VCSRef, 10)})
357358
}
358359
t.SetStyle(table.StyleRounded)
359360
cmd.Println(t.Render())
360361
}
362+
363+
// formatTime returns the given RFC3389 time formated as 2006-01-02T15:04Z.
364+
func formatTime(maybeRFC3389Time string) string {
365+
if t, err := time.Parse(time.RFC3339, maybeRFC3389Time); err == nil {
366+
return t.UTC().Format("2006-01-02T15:04Z")
367+
}
368+
return maybeRFC3389Time
369+
}
370+
371+
// truncate truncates text if it longer than maxLength.
372+
func truncate(text string, maxLength int) string {
373+
if len(text) > maxLength {
374+
return text[:maxLength]
375+
}
376+
return text
377+
}

internal/docker/docker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type ConfigLabels struct {
4949
ComposeProject string `json:"com.docker.compose.project"`
5050
ComposeService string `json:"com.docker.compose.service"`
5151
ComposeVersion string `json:"com.docker.compose.version"`
52+
53+
// http://label-schema.org/rc1/ Labels
54+
BuildDate string `json:"org.label-schema.build-date,omitempty"` // This label contains the Date/Time the image was built. The value SHOULD be formatted according to RFC 3339.
55+
VCSRef string `json:"org.label-schema.vcs-ref,omitempty"` // Identifier for the version of the source code from which this image was built. For example if the version control system is git this is the SHA.
5256
}
5357

5458
// String function dumps string representation of the container description.

internal/stack/compose.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ServiceStatus struct {
1818
Name string
1919
Status string
2020
Version string
21+
Labels *docker.ConfigLabels // Container labels.
2122
}
2223

2324
const readyServicesSuffix = "is_ready"
@@ -214,6 +215,7 @@ func newServiceStatus(description *docker.ContainerDescription) (*ServiceStatus,
214215
Name: description.Config.Labels.ComposeService,
215216
Status: description.State.Status,
216217
Version: getVersionFromDockerImage(description.Config.Image),
218+
Labels: &description.Config.Labels,
217219
}
218220
if description.State.Status == "running" {
219221
healthStatus := "unknown health"

internal/stack/compose_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func TestNewServiceStatus(t *testing.T) {
7878
Name: "myservice",
7979
Status: "running (healthy)",
8080
Version: "1.42.0",
81+
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
8182
},
8283
},
8384
{
@@ -112,6 +113,7 @@ func TestNewServiceStatus(t *testing.T) {
112113
Name: "myservice",
113114
Status: "exited (128)",
114115
Version: "1.42.0",
116+
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
115117
},
116118
},
117119
{
@@ -155,6 +157,7 @@ func TestNewServiceStatus(t *testing.T) {
155157
Name: "myservice",
156158
Status: "running (starting)",
157159
Version: "1.42.0",
160+
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
158161
},
159162
},
160163
}

0 commit comments

Comments
 (0)