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

Add Node Name to the output of nomad job status (verbose only) and nomad alloc status #5224

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions api/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Allocation struct {
EvalID string
Name string
NodeID string
NodeName string
JobID string
Job *Job
TaskGroup string
Expand Down Expand Up @@ -149,6 +150,7 @@ type AllocationListStub struct {
Name string
Namespace string
NodeID string
NodeName string
JobID string
JobType string
JobVersion uint64
Expand Down
1 change: 1 addition & 0 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func formatAllocBasicInfo(alloc *api.Allocation, client *api.Client, uuidLength
fmt.Sprintf("Eval ID|%s", limit(alloc.EvalID, uuidLength)),
fmt.Sprintf("Name|%s", alloc.Name),
fmt.Sprintf("Node ID|%s", limit(alloc.NodeID, uuidLength)),
fmt.Sprintf("Node Name|%s", alloc.NodeName),
fmt.Sprintf("Job ID|%s", alloc.JobID),
fmt.Sprintf("Job Version|%d", getVersion(alloc.Job)),
fmt.Sprintf("Client Status|%s", alloc.ClientStatus),
Expand Down
12 changes: 12 additions & 0 deletions command/alloc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ func TestAllocStatusCommand_Run(t *testing.T) {
}
// get an alloc id
allocId1 := ""
nodeName := ""
if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
if len(allocs) > 0 {
allocId1 = allocs[0].ID
nodeName = allocs[0].NodeName
}
}
if allocId1 == "" {
Expand All @@ -141,6 +143,16 @@ func TestAllocStatusCommand_Run(t *testing.T) {
t.Fatalf("expected to have 'Modified' but saw: %s", out)
}

if !strings.Contains(out, "Modified") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental duplicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for pointing it out.

t.Fatalf("expected to have 'Modified' but saw: %s", out)
}

nodeNameRegexpStr := fmt.Sprintf(`\nNode Name\s+= %s\n`, regexp.QuoteMeta(nodeName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you migrate this test to using require, you can use require.Regexp(regexp.MustCompile(regexpStr), out), which provides a nicer error message here. (example in this test:

require.Contains(out, "Replacement Alloc ID")
require.Regexp(regexp.MustCompile(".*Reschedule Attempts\\s*=\\s*1/2"), out)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

nodeNameRegexp := regexp.MustCompile(nodeNameRegexpStr)
if !nodeNameRegexp.MatchString(out) {
t.Fatalf("expected to have 'Node Name' but saw: %s", out)
}

ui.OutputWriter.Reset()

if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
Expand Down
5 changes: 3 additions & 2 deletions command/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,13 @@ func formatAllocListStubs(stubs []*api.AllocationListStub, verbose bool, uuidLen

allocs := make([]string, len(stubs)+1)
if verbose {
allocs[0] = "ID|Eval ID|Node ID|Task Group|Version|Desired|Status|Created|Modified"
allocs[0] = "ID|Eval ID|Node ID|Node Name|Task Group|Version|Desired|Status|Created|Modified"
for i, alloc := range stubs {
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s|%s",
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%d|%s|%s|%s|%s",
limit(alloc.ID, uuidLength),
limit(alloc.EvalID, uuidLength),
limit(alloc.NodeID, uuidLength),
alloc.NodeName,
alloc.TaskGroup,
alloc.JobVersion,
alloc.DesiredStatus,
Expand Down
21 changes: 21 additions & 0 deletions command/job_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -123,6 +124,14 @@ func TestJobStatusCommand_Run(t *testing.T) {
if code := cmd.Run([]string{"-address=" + url, "-verbose", "job2_sfx"}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}

nodeName := ""
if allocs, _, err := client.Jobs().Allocations("job2_sfx", false, nil); err == nil {
if len(allocs) > 0 {
nodeName = allocs[0].NodeName
}
}

out = ui.OutputWriter.String()
if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
t.Fatalf("expected only job2_sfx, got: %s", out)
Expand All @@ -139,6 +148,18 @@ func TestJobStatusCommand_Run(t *testing.T) {
if !strings.Contains(out, "Modified") {
t.Fatal("should have modified header")
}

// string calculations based on 1-byte chars, not using runes
allocationsTableName := "Allocations\n"
allocationsTableStr := strings.Split(out, allocationsTableName)[1]
nodeNameHeaderStr := "Node Name"
nodeNameHeaderIndex := strings.Index(allocationsTableStr, nodeNameHeaderStr)
nodeNameRegexpStr := fmt.Sprintf(`.*%s.*\n.{%d}%s`, nodeNameHeaderStr, nodeNameHeaderIndex, regexp.QuoteMeta(nodeName))
nodeNameRegexp := regexp.MustCompile(nodeNameRegexpStr)
if !nodeNameRegexp.MatchString(out) {
t.Fatalf("expected to have 'Node Name' but saw: %s", out)
}

ui.ErrorWriter.Reset()
ui.OutputWriter.Reset()

Expand Down
5 changes: 5 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7125,6 +7125,9 @@ type Allocation struct {
// NodeID is the node this is being placed on
NodeID string

// NodeName is the name of the node this is being placed on.
NodeName string

// Job is the parent job of the task group being allocated.
// This is copied at allocation time to avoid issues if the job
// definition is updated.
Expand Down Expand Up @@ -7582,6 +7585,7 @@ func (a *Allocation) Stub() *AllocListStub {
Name: a.Name,
Namespace: a.Namespace,
NodeID: a.NodeID,
NodeName: a.NodeName,
JobID: a.JobID,
JobType: a.Job.Type,
JobVersion: a.Job.Version,
Expand Down Expand Up @@ -7609,6 +7613,7 @@ type AllocListStub struct {
Name string
Namespace string
NodeID string
NodeName string
JobID string
JobType string
JobVersion uint64
Expand Down
3 changes: 2 additions & 1 deletion scheduler/generic_sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-multierror"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand Down Expand Up @@ -495,6 +495,7 @@ func (s *GenericScheduler) computePlacements(destructive, place []placementResul
TaskGroup: tg.Name,
Metrics: s.ctx.Metrics(),
NodeID: option.Node.ID,
NodeName: option.Node.Name,
DeploymentID: deploymentID,
TaskResources: resources.OldTaskResources(),
AllocatedResources: resources,
Expand Down
1 change: 1 addition & 0 deletions scheduler/system_sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func (s *SystemScheduler) computePlacements(place []allocTuple) error {
TaskGroup: missing.TaskGroup.Name,
Metrics: s.ctx.Metrics(),
NodeID: option.Node.ID,
NodeName: option.Node.Name,
TaskResources: resources.OldTaskResources(),
AllocatedResources: resources,
DesiredStatus: structs.AllocDesiredStatusRun,
Expand Down