From 758e1cb7f39797f0ad2f22eac5a21746eb201add Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Wed, 2 May 2018 16:19:15 -0700 Subject: [PATCH] cli: expand drain info for node status --- CHANGELOG.md | 1 + command/node_status.go | 23 ++++++++++++++++++++++- command/node_status_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fde262d2ded..4866b2fea18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.8.4 (Unreleased) IMPROVEMENTS: + * cli: Add node drain details to node status [[GH-4247](https://github.com/hashicorp/nomad/issues/4247)] * command: add -short option to init command that emits a minimal jobspec [[GH-4239](https://github.com/hashicorp/nomad/issues/4239)] diff --git a/command/node_status.go b/command/node_status.go index 226c054fcd0..b1ffee1d857 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "sort" + "strconv" "strings" "time" @@ -299,6 +300,26 @@ func nodeDrivers(n *api.Node) []string { return drivers } +func formatDrain(n *api.Node) string { + if n.DrainStrategy != nil { + b := new(strings.Builder) + b.WriteString("true") + + if n.DrainStrategy.ForceDeadline.IsZero() { + b.WriteString("; no deadline") + } else { + fmt.Fprintf(b, "; %s deadline", formatTime(n.DrainStrategy.ForceDeadline)) + } + + if n.DrainStrategy.IgnoreSystemJobs { + b.WriteString("; ignoring system jobs") + } + return b.String() + } + + return strconv.FormatBool(n.Drain) +} + func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int { // Format the header output basic := []string{ @@ -306,7 +327,7 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int { fmt.Sprintf("Name|%s", node.Name), fmt.Sprintf("Class|%s", node.NodeClass), fmt.Sprintf("DC|%s", node.Datacenter), - fmt.Sprintf("Drain|%v", node.Drain), + fmt.Sprintf("Drain|%v", formatDrain(node)), fmt.Sprintf("Eligibility|%s", node.SchedulingEligibility), fmt.Sprintf("Status|%s", node.Status), } diff --git a/command/node_status_test.go b/command/node_status_test.go index 0740317dc54..30942aa40c9 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -4,7 +4,9 @@ import ( "fmt" "strings" "testing" + "time" + "github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/command/agent" "github.com/hashicorp/nomad/testutil" "github.com/mitchellh/cli" @@ -250,3 +252,26 @@ func TestNodeStatusCommand_AutocompleteArgs(t *testing.T) { assert.Equal(1, len(res)) assert.Equal(nodeID, res[0]) } + +func TestNodeStatusCommand_FormatDrain(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + node := &api.Node{} + + assert.Equal("false", formatDrain(node)) + + node.DrainStrategy = &api.DrainStrategy{} + + assert.Equal("true; no deadline", formatDrain(node)) + + // formatTime special cases Unix(0, 0), so increment by 1 + node.DrainStrategy.ForceDeadline = time.Unix(1, 0) + t.Logf(node.DrainStrategy.ForceDeadline.String()) + + assert.Equal("true; 1970-01-01T00:00:01Z deadline", formatDrain(node)) + + node.DrainStrategy.IgnoreSystemJobs = true + + assert.Equal("true; 1970-01-01T00:00:01Z deadline; ignoring system jobs", formatDrain(node)) +}