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

Expand drain info for node status command #4247

Merged
merged 1 commit into from
May 4, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)]

Expand Down
23 changes: 22 additions & 1 deletion command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -299,14 +300,34 @@ 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{
fmt.Sprintf("ID|%s", limit(node.ID, c.length)),
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),
}
Expand Down
25 changes: 25 additions & 0 deletions command/node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}