Skip to content

Commit

Permalink
stack status cmd - add columns for image build date and VCS reference
Browse files Browse the repository at this point in the history
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)   │                   │            │
╰──────────────────┴─────────────────┴─────────────────────┴───────────────────┴────────────╯
```
  • Loading branch information
andrewkroh committed Aug 29, 2024
1 parent 0d3c215 commit 4eceda3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"fmt"
"strings"
"time"

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

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

for _, service := range servicesStatus {
t.AppendRow(table.Row{service.Name, service.Version, service.Status})
t.AppendRow(table.Row{service.Name, service.Version, service.Status, formatTime(service.Labels.BuildDate), truncate(service.Labels.VCSRef, 10)})
}
t.SetStyle(table.StyleRounded)
cmd.Println(t.Render())
}

// formatTime returns the given RFC3389 time formated as 2006-01-02T15:04Z.
func formatTime(maybeRFC3389Time string) string {
if t, err := time.Parse(time.RFC3339, maybeRFC3389Time); err == nil {
return t.UTC().Format("2006-01-02T15:04Z")
}
return maybeRFC3389Time
}

// truncate truncates text if it longer than maxLength.
func truncate(text string, maxLength int) string {
if len(text) > maxLength {
return text[:maxLength]
}
return text
}
4 changes: 4 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type ConfigLabels struct {
ComposeProject string `json:"com.docker.compose.project"`
ComposeService string `json:"com.docker.compose.service"`
ComposeVersion string `json:"com.docker.compose.version"`

// http://label-schema.org/rc1/ Labels
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.
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.
}

// String function dumps string representation of the container description.
Expand Down
2 changes: 2 additions & 0 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ServiceStatus struct {
Name string
Status string
Version string
Labels *docker.ConfigLabels // Container labels.
}

const readyServicesSuffix = "is_ready"
Expand Down Expand Up @@ -214,6 +215,7 @@ func newServiceStatus(description *docker.ContainerDescription) (*ServiceStatus,
Name: description.Config.Labels.ComposeService,
Status: description.State.Status,
Version: getVersionFromDockerImage(description.Config.Image),
Labels: &description.Config.Labels,
}
if description.State.Status == "running" {
healthStatus := "unknown health"
Expand Down
3 changes: 3 additions & 0 deletions internal/stack/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestNewServiceStatus(t *testing.T) {
Name: "myservice",
Status: "running (healthy)",
Version: "1.42.0",
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
},
},
{
Expand Down Expand Up @@ -112,6 +113,7 @@ func TestNewServiceStatus(t *testing.T) {
Name: "myservice",
Status: "exited (128)",
Version: "1.42.0",
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
},
},
{
Expand Down Expand Up @@ -155,6 +157,7 @@ func TestNewServiceStatus(t *testing.T) {
Name: "myservice",
Status: "running (starting)",
Version: "1.42.0",
Labels: &docker.ConfigLabels{ComposeService: "myservice"},
},
},
}
Expand Down

0 comments on commit 4eceda3

Please sign in to comment.