Drain cli, api, http

This commit is contained in:
Alex Dadgar
2018-02-23 15:56:36 -08:00
committed by Michael Schurter
parent 1773de9e30
commit 2bdeacebff
11 changed files with 298 additions and 83 deletions

View File

@@ -3,7 +3,6 @@ package api
import (
"fmt"
"sort"
"strconv"
"time"
)
@@ -42,10 +41,24 @@ func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error)
return &resp, qm, nil
}
// ToggleDrain is used to toggle drain mode on/off for a given node.
func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) {
drainArg := strconv.FormatBool(drain)
wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q)
// NodeUpdateDrainRequest is used to update the drain specification for a node.
type NodeUpdateDrainRequest struct {
// NodeID is the node to update the drain specification for.
NodeID string
// DrainSpec is the drain specification to set for the node. A nil DrainSpec
// will disable draining.
DrainSpec *DrainSpec
}
// UpdateDrain is used to update the drain strategy for a given node.
func (n *Nodes) UpdateDrain(nodeID string, spec *DrainSpec, q *WriteOptions) (*WriteMeta, error) {
req := &NodeUpdateDrainRequest{
NodeID: nodeID,
DrainSpec: spec,
}
wm, err := n.client.write("/v1/node/"+nodeID+"/drain", req, nil, q)
if err != nil {
return nil, err
}
@@ -108,25 +121,44 @@ type DriverInfo struct {
// Node is used to deserialize a node entry.
type Node struct {
ID string
Datacenter string
Name string
HTTPAddr string
TLSEnabled bool
Attributes map[string]string
Resources *Resources
Reserved *Resources
Links map[string]string
Meta map[string]string
NodeClass string
Drain bool
Status string
StatusDescription string
StatusUpdatedAt int64
Events []*NodeEvent
Drivers map[string]*DriverInfo
CreateIndex uint64
ModifyIndex uint64
ID string
Datacenter string
Name string
HTTPAddr string
TLSEnabled bool
Attributes map[string]string
Resources *Resources
Reserved *Resources
Links map[string]string
Meta map[string]string
NodeClass string
Drain bool
DrainStrategy *DrainStrategy
SchedulingEligibility string
Status string
StatusDescription string
StatusUpdatedAt int64
Events []*NodeEvent
Drivers map[string]*DriverInfo
CreateIndex uint64
ModifyIndex uint64
}
// DrainStrategy describes a Node's drain behavior.
type DrainStrategy struct {
// DrainSpec is the user declared drain specification
DrainSpec
}
// DrainSpec describes a Node's drain behavior.
type DrainSpec struct {
// Deadline is the duration after StartTime when the remaining
// allocations on a draining Node should be told to stop.
Deadline time.Duration
// IgnoreSystemJobs allows systems jobs to remain on the node even though it
// has been marked for draining.
IgnoreSystemJobs bool
}
const (

View File

@@ -174,7 +174,10 @@ func TestNodes_ToggleDrain(t *testing.T) {
}
// Toggle it on
wm, err := nodes.ToggleDrain(nodeID, true, nil)
spec := &DrainSpec{
Deadline: 10 * time.Second,
}
wm, err := nodes.UpdateDrain(nodeID, spec, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -185,12 +188,12 @@ func TestNodes_ToggleDrain(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
if !out.Drain {
t.Fatalf("drain mode should be on")
if out.SchedulingEligibility != structs.NodeSchedulingIneligible {
t.Fatalf("bad eligibility: %v vs %v", out.SchedulingEligibility, structs.NodeSchedulingIneligible)
}
// Toggle off again
wm, err = nodes.ToggleDrain(nodeID, false, nil)
wm, err = nodes.UpdateDrain(nodeID, nil, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@@ -204,6 +207,9 @@ func TestNodes_ToggleDrain(t *testing.T) {
if out.Drain {
t.Fatalf("drain mode should be off")
}
if out.DrainStrategy != nil {
t.Fatalf("drain strategy should be unset")
}
}
func TestNodes_Allocations(t *testing.T) {