Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add -json and -t flag for alloc checks command #16405

Merged
merged 13 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/16405.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added `-json` and `-t` flag to `alloc checks` command
```
29 changes: 27 additions & 2 deletions command/alloc_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Checks Specific Options:

-verbose
Show full information.

-json
Output the latest health check status information in a JSON format.
jrasell marked this conversation as resolved.
Show resolved Hide resolved

-t
Format and display latest health check status information using a Go template.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -42,6 +48,8 @@ func (c *AllocChecksCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-verbose": complete.PredictNothing,
"-json": complete.PredictNothing,
jrasell marked this conversation as resolved.
Show resolved Hide resolved
"-t": complete.PredictAnything,
})
}

Expand All @@ -64,17 +72,21 @@ func (c *AllocChecksCommand) Name() string {
}

func (c *AllocChecksCommand) Run(args []string) int {
var verbose bool
var json, verbose bool
var tmpl string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")

if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()

// Check that we got only one argument
args = flags.Args()
if numArgs := len(args); numArgs < 1 {
c.Ui.Error("An allocation ID is required")
c.Ui.Error(commandErrorText(c))
Expand Down Expand Up @@ -128,6 +140,17 @@ func (c *AllocChecksCommand) Run(args []string) int {
return 1
}

if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, checks)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

c.Ui.Output(out)
return 0
}

c.Ui.Output(fmt.Sprintf("Status of %d Nomad Service Checks", len(checks)))
c.Ui.Output("")

Expand All @@ -138,6 +161,7 @@ func (c *AllocChecksCommand) Run(args []string) int {
}
return s
}

for _, check := range checks {
list := []string{
pair("ID", check.ID),
Expand All @@ -155,6 +179,7 @@ func (c *AllocChecksCommand) Run(args []string) int {
pair("Timestamp", formatTaskTimes(time.Unix(check.Timestamp, 0))),
pair("Output", check.Output),
)

c.Ui.Output(formatList(list))
c.Ui.Output("")
}
Expand Down
25 changes: 25 additions & 0 deletions command/alloc_checks_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package command

import (
"encoding/json"
"testing"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
Expand Down Expand Up @@ -83,6 +85,7 @@ func TestAllocChecksCommand_AutocompleteArgs(t *testing.T) {
func TestAllocChecksCommand_Run(t *testing.T) {
ci.Parallel(t)
srv, client, url := testServer(t, true, nil)

defer srv.Shutdown()

// wait for nodes
Expand Down Expand Up @@ -117,4 +120,26 @@ func TestAllocChecksCommand_Run(t *testing.T) {
must.StrContains(t, out, `Task = (group)`)
must.StrContains(t, out, `Service = service1`)
must.StrContains(t, out, `Mode = healthiness`)

ui.OutputWriter.Reset()

// List json
cmd = &AllocChecksCommand{Meta: Meta{Ui: ui, flagAddress: url}}
must.Zero(t, cmd.Run([]string{"-address=" + url, "-json", allocID}))

outJson := api.AllocCheckStatuses{}
err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson)
must.NoError(t, err)

ui.OutputWriter.Reset()
jrasell marked this conversation as resolved.
Show resolved Hide resolved

// Go template to format the output
code = cmd.Run([]string{"-address=" + url, "-t", "{{range .}}{{ .Status }}{{end}}", allocID})
must.Zero(t, code)

out = ui.OutputWriter.String()
must.StrContains(t, out, "failure")

ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
}
86 changes: 86 additions & 0 deletions website/content/docs/commands/alloc/checks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
layout: docs
page_title: 'Commands: alloc checks'
description: |
Outputs service health check status information.
---

# Command: alloc checks


The `alloc checks` command outputs the latest health check status information for checks in the allocation.

## Usage

```plaintext
nomad alloc checks [options] <allocation>
```

Outputs the latest health check status information for services in the allocation
using the Nomad service discovery provider. This command accepts an allocation
ID or prefix as the sole argument.

When ACLs are enabled, this command requires a token with the 'read-job' capability
for the allocation's namespace. The 'list-jobs' capability is required to run the
command with an allocation ID prefix instead of the exact allocation ID.

## General Options

@include 'general_options.mdx'

## Checks Options

- `-verbose`: Display verbose output.

- `-json`: Output the allocation in its JSON format.
jrasell marked this conversation as resolved.
Show resolved Hide resolved

- `-t`: Format and display the health checks status using a Go template.

## Examples

Show checks of an alloc health check using its short ID:

```shell-session
$ nomad alloc checks e0fdbd85

Status of 1 Nomad Service Checks

ID = 9f4e18fd0867cebb19a8fac3d7a1cf27
Name = alive
Group = example.cache[0]
Task = (group)
Service = redis-cache
Status = success
Mode = healthiness
Timestamp = 2023-03-09T16:10:23+01:00
Output = nomad: tcp ok
```

jrasell marked this conversation as resolved.
Show resolved Hide resolved
The `-json` flag can be used to get the health checks status in json format:

```shell-session
nomad alloc checks -json 54fde4f8

{
"9810e90177a4c21ce3bfe04dc7da6131": {
"Check": "alive",
"Group": "example.cache[0]",
"ID": "9810e90177a4c21ce3bfe04dc7da6131",
"Mode": "healthiness",
"Output": "nomad: tcp ok",
"Service": "redis-cache",
"Status": "success",
"StatusCode": 0,
"Task": "",
"Timestamp": 1678444309
}
}
```

Or use the `-t` flag to format and display the health checks using a Go template:

```shell-session
$ nomad alloc checks -t '{{range .}}{{ printf "%s: %s\n" .ID .Status }}{{end}}' 54

9810e90177a4c21ce3bfe04dc7da6131: success
```
2 changes: 2 additions & 0 deletions website/content/docs/commands/alloc/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Usage: `nomad alloc <subcommand> [options]`
Run `nomad alloc <subcommand> -h` for help on that subcommand. The following
subcommands are available:

- [`alloc checks`][checks] - Outputs service health check status information.
- [`alloc exec`][exec] - Run a command in a running allocation
- [`alloc fs`][fs] - Inspect the contents of an allocation directory
- [`alloc logs`][logs] - Streams the logs of a task
Expand All @@ -24,6 +25,7 @@ subcommands are available:
- [`alloc status`][status] - Display allocation status information and metadata
- [`alloc stop`][stop] - Stop and reschedule a running allocation

[checks]: /nomad/docs/commands/alloc/checks 'Outputs service health check status information'
[exec]: /nomad/docs/commands/alloc/exec 'Run a command in a running allocation'
[fs]: /nomad/docs/commands/alloc/fs 'Inspect the contents of an allocation directory'
[logs]: /nomad/docs/commands/alloc/logs 'Streams the logs of a task'
Expand Down
4 changes: 4 additions & 0 deletions website/data/docs-nav-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@
"title": "Overview",
"path": "commands/alloc"
},
{
"title": "checks",
"path": "commands/alloc/checks"
},
{
"title": "exec",
"path": "commands/alloc/exec"
Expand Down