Skip to content

Commit

Permalink
Merge pull request #5224 from hashicorp/jrasell-gh_2359
Browse files Browse the repository at this point in the history
Add Node Name to the output of nomad job status (verbose only) and nomad alloc status
  • Loading branch information
arshjohar committed Jan 23, 2019
2 parents 316a4f2 + de5386e commit ffd82df
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 3 deletions.
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
5 changes: 5 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,9 @@ func TestAllocStatusCommand_Run(t *testing.T) {
t.Fatalf("expected to have 'Modified' but saw: %s", out)
}

nodeNameRegexpStr := fmt.Sprintf(`\nNode Name\s+= %s\n`, regexp.QuoteMeta(nodeName))
require.Regexp(t, regexp.MustCompile(nodeNameRegexpStr), 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
18 changes: 18 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,15 @@ 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))
require.Regexp(t, regexp.MustCompile(nodeNameRegexpStr), 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

0 comments on commit ffd82df

Please sign in to comment.