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: extend coverage of operator client-state command #18996

Merged
merged 1 commit into from
Nov 3, 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
7 changes: 7 additions & 0 deletions .changelog/18996.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:improvement
cli: Added identities, networks, and volumes to the output of the `operator client-state` command
```

```release-note:bug
cli: Fixed a bug where the `operator client-state` command would crash if it reads an allocation without a task state
```
33 changes: 33 additions & 0 deletions command/operator_client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,37 @@ func (c *OperatorClientStateCommand) Run(args []string) int {
return 1
}

identities, err := db.GetAllocIdentities(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to get identities for %s: %v", allocID, err))
return 1
}

networks, err := db.GetNetworkStatus(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to get networks for %s: %v", allocID, err))
return 1
}

volumes, err := db.GetAllocVolumes(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to get volumes for %s: %v", allocID, err))
return 1
}

tasks := map[string]*taskState{}
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
for _, jt := range tg.Tasks {
ls, rs, err := db.GetTaskRunnerState(allocID, jt.Name)
if ls == nil {
c.Ui.Warn(fmt.Sprintf("no task runner state for %s (%s)", allocID, jt.Name))
tasks[jt.Name] = &taskState{
LocalState: ls,
RemoteState: rs,
DriverState: nil,
}
continue
}
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to get task runner state %s: %v", allocID, err))
return 1
Expand All @@ -99,6 +126,9 @@ func (c *OperatorClientStateCommand) Run(args []string) int {
data[allocID] = &clientStateAlloc{
Alloc: alloc,
DeployStatus: deployState,
Identities: identities,
Networks: networks,
Volumes: volumes,
Tasks: tasks,
}
}
Expand All @@ -122,6 +152,9 @@ type debugOutput struct {
type clientStateAlloc struct {
Alloc any
DeployStatus any
Identities any
Networks any
Volumes any
Tasks map[string]*taskState
}

Expand Down
31 changes: 23 additions & 8 deletions command/operator_client_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package command

import (
"strings"
"testing"

"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/state"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
"github.com/shoenig/test/must"
)

func TestOperatorClientStateCommand(t *testing.T) {
Expand All @@ -18,15 +20,28 @@ func TestOperatorClientStateCommand(t *testing.T) {
cmd := &OperatorClientStateCommand{Meta: Meta{Ui: ui}}

failedCode := cmd.Run([]string{"some", "bad", "args"})
require.Equal(t, 1, failedCode)
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
must.Eq(t, 1, failedCode)
out := ui.ErrorWriter.String()
must.StrContains(t, out, commandErrorText(cmd), must.Sprint("expected help output"))
ui.ErrorWriter.Reset()

dir := t.TempDir()

// run against an empty client state directory
code := cmd.Run([]string{dir})
must.Eq(t, 0, code)
must.StrContains(t, ui.OutputWriter.String(), "{}")

// create a minimal client state db
db, err := state.NewBoltStateDB(testlog.HCLogger(t), dir)
must.NoError(t, err)
alloc := structs.MockAlloc()
err = db.PutAllocation(alloc)
must.NoError(t, err)
must.NoError(t, db.Close())

require.Equal(t, 0, code)
require.Contains(t, ui.OutputWriter.String(), "{}")
// run against an incomplete client state directory
code = cmd.Run([]string{dir})
must.Eq(t, 0, code)
must.StrContains(t, ui.OutputWriter.String(), alloc.ID)
}