2015-09-11 23:51:18 +00:00
|
|
|
|
package api
|
|
|
|
|
|
2015-09-12 00:20:33 +00:00
|
|
|
|
import (
|
2018-02-07 02:51:34 +00:00
|
|
|
|
"fmt"
|
2015-09-17 19:40:51 +00:00
|
|
|
|
"sort"
|
2018-01-25 16:30:15 +00:00
|
|
|
|
"time"
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-12 00:20:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
|
// Nodes is used to query node-related API endpoints
|
|
|
|
|
type Nodes struct {
|
|
|
|
|
client *Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nodes returns a handle on the node endpoints.
|
|
|
|
|
func (c *Client) Nodes() *Nodes {
|
|
|
|
|
return &Nodes{client: c}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List is used to list out all of the nodes
|
2015-09-14 02:55:47 +00:00
|
|
|
|
func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
|
2015-09-17 19:40:51 +00:00
|
|
|
|
var resp NodeIndexSort
|
2015-09-11 23:51:18 +00:00
|
|
|
|
qm, err := n.client.query("/v1/nodes", &resp, q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
2017-09-26 22:26:33 +00:00
|
|
|
|
sort.Sort(resp)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
return resp, qm, nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
|
func (n *Nodes) PrefixList(prefix string) ([]*NodeListStub, *QueryMeta, error) {
|
|
|
|
|
return n.List(&QueryOptions{Prefix: prefix})
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
|
// Info is used to query a specific node by its ID.
|
|
|
|
|
func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) {
|
|
|
|
|
var resp Node
|
|
|
|
|
qm, err := n.client.query("/v1/node/"+nodeID, &resp, q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
return &resp, qm, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 23:56:36 +00:00
|
|
|
|
// 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
|
2018-03-08 19:06:30 +00:00
|
|
|
|
|
2018-03-21 17:41:06 +00:00
|
|
|
|
// MarkEligible marks the node as eligible for scheduling if removing
|
|
|
|
|
// the drain strategy.
|
2018-03-08 19:06:30 +00:00
|
|
|
|
MarkEligible bool
|
2018-02-23 23:56:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 19:06:30 +00:00
|
|
|
|
// UpdateDrain is used to update the drain strategy for a given node. If
|
|
|
|
|
// markEligible is true and the drain is being removed, the node will be marked
|
|
|
|
|
// as having its scheduling being elibile
|
|
|
|
|
func (n *Nodes) UpdateDrain(nodeID string, spec *DrainSpec, markEligible bool, q *WriteOptions) (*WriteMeta, error) {
|
2018-02-23 23:56:36 +00:00
|
|
|
|
req := &NodeUpdateDrainRequest{
|
2018-03-08 19:06:30 +00:00
|
|
|
|
NodeID: nodeID,
|
|
|
|
|
DrainSpec: spec,
|
|
|
|
|
MarkEligible: markEligible,
|
2018-02-23 23:56:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wm, err := n.client.write("/v1/node/"+nodeID+"/drain", req, nil, q)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return wm, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 20:59:27 +00:00
|
|
|
|
// NodeUpdateEligibilityRequest is used to update the drain specification for a node.
|
|
|
|
|
type NodeUpdateEligibilityRequest struct {
|
|
|
|
|
// NodeID is the node to update the drain specification for.
|
|
|
|
|
NodeID string
|
|
|
|
|
Eligibility string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToggleEligibility is used to update the scheduling eligibility of the node
|
|
|
|
|
func (n *Nodes) ToggleEligibility(nodeID string, eligible bool, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
|
e := structs.NodeSchedulingEligible
|
|
|
|
|
if !eligible {
|
|
|
|
|
e = structs.NodeSchedulingIneligible
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &NodeUpdateEligibilityRequest{
|
|
|
|
|
NodeID: nodeID,
|
|
|
|
|
Eligibility: e,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wm, err := n.client.write("/v1/node/"+nodeID+"/eligibility", req, nil, q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return wm, nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
|
// Allocations is used to return the allocations associated with a node.
|
2015-11-09 16:54:18 +00:00
|
|
|
|
func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
|
|
|
|
|
var resp []*Allocation
|
2015-09-11 23:51:18 +00:00
|
|
|
|
qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
2015-11-09 16:54:18 +00:00
|
|
|
|
sort.Sort(AllocationSort(resp))
|
2015-09-11 23:51:18 +00:00
|
|
|
|
return resp, qm, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ForceEvaluate is used to force-evaluate an existing node.
|
|
|
|
|
func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
|
var resp nodeEvalResponse
|
|
|
|
|
wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
|
|
|
|
}
|
|
|
|
|
return resp.EvalID, wm, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 03:05:51 +00:00
|
|
|
|
func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
|
2016-06-12 16:32:38 +00:00
|
|
|
|
var resp HostStats
|
2018-02-07 02:51:34 +00:00
|
|
|
|
path := fmt.Sprintf("/v1/client/stats?node_id=%s", nodeID)
|
|
|
|
|
if _, err := n.client.query(path, &resp, q); err != nil {
|
2016-05-22 03:05:51 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-06-12 16:32:38 +00:00
|
|
|
|
return &resp, nil
|
2016-05-22 03:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 00:18:29 +00:00
|
|
|
|
func (n *Nodes) GC(nodeID string, q *QueryOptions) error {
|
2018-02-07 02:51:34 +00:00
|
|
|
|
var resp struct{}
|
|
|
|
|
path := fmt.Sprintf("/v1/client/gc?node_id=%s", nodeID)
|
|
|
|
|
_, err := n.client.query(path, &resp, q)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-08-28 18:32:52 +00:00
|
|
|
|
|
2018-02-07 02:51:34 +00:00
|
|
|
|
// TODO Add tests
|
|
|
|
|
func (n *Nodes) GcAlloc(allocID string, q *QueryOptions) error {
|
2017-01-13 00:18:29 +00:00
|
|
|
|
var resp struct{}
|
2018-02-07 02:51:34 +00:00
|
|
|
|
path := fmt.Sprintf("/v1/client/allocation/%s/gc", allocID)
|
|
|
|
|
_, err := n.client.query(path, &resp, q)
|
2017-01-13 00:18:29 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 16:30:15 +00:00
|
|
|
|
// DriverInfo is used to deserialize a DriverInfo entry
|
|
|
|
|
type DriverInfo struct {
|
|
|
|
|
Attributes map[string]string
|
|
|
|
|
Detected bool
|
|
|
|
|
Healthy bool
|
|
|
|
|
HealthDescription string
|
|
|
|
|
UpdateTime time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
|
// Node is used to deserialize a node entry.
|
|
|
|
|
type Node struct {
|
2018-02-23 23:56:36 +00:00
|
|
|
|
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
|
2018-03-01 00:25:56 +00:00
|
|
|
|
|
2018-03-12 20:44:33 +00:00
|
|
|
|
// ForceDeadline is the deadline time for the drain after which drains will
|
|
|
|
|
// be forced
|
|
|
|
|
ForceDeadline time.Time
|
2018-02-23 23:56:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2015-09-14 02:55:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 14:34:08 +00:00
|
|
|
|
const (
|
2018-03-14 00:59:37 +00:00
|
|
|
|
NodeEventSubsystemDrain = "Drain"
|
|
|
|
|
NodeEventSubsystemDriver = "Driver"
|
|
|
|
|
NodeEventSubsystemHeartbeat = "Heartbeat"
|
|
|
|
|
NodeEventSubsystemCluster = "Cluster"
|
2018-03-08 14:34:08 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NodeEvent is a single unit representing a node’s state change
|
|
|
|
|
type NodeEvent struct {
|
2018-03-14 01:04:55 +00:00
|
|
|
|
Message string
|
|
|
|
|
Subsystem string
|
|
|
|
|
Details map[string]string
|
|
|
|
|
Timestamp int64
|
2018-03-08 14:34:08 +00:00
|
|
|
|
CreateIndex uint64
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 03:05:51 +00:00
|
|
|
|
// HostStats represents resource usage stats of the host running a Nomad client
|
|
|
|
|
type HostStats struct {
|
2016-06-10 21:14:33 +00:00
|
|
|
|
Memory *HostMemoryStats
|
|
|
|
|
CPU []*HostCPUStats
|
|
|
|
|
DiskStats []*HostDiskStats
|
|
|
|
|
Uptime uint64
|
|
|
|
|
CPUTicksConsumed float64
|
2016-05-22 03:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type HostMemoryStats struct {
|
|
|
|
|
Total uint64
|
|
|
|
|
Available uint64
|
|
|
|
|
Used uint64
|
|
|
|
|
Free uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type HostCPUStats struct {
|
|
|
|
|
CPU string
|
|
|
|
|
User float64
|
|
|
|
|
System float64
|
|
|
|
|
Idle float64
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 10:46:49 +00:00
|
|
|
|
type HostDiskStats struct {
|
|
|
|
|
Device string
|
|
|
|
|
Mountpoint string
|
|
|
|
|
Size uint64
|
|
|
|
|
Used uint64
|
|
|
|
|
Available uint64
|
|
|
|
|
UsedPercent float64
|
|
|
|
|
InodesUsedPercent float64
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-14 02:55:47 +00:00
|
|
|
|
// NodeListStub is a subset of information returned during
|
|
|
|
|
// node list operations.
|
|
|
|
|
type NodeListStub struct {
|
2018-02-27 22:00:55 +00:00
|
|
|
|
Address string
|
|
|
|
|
ID string
|
|
|
|
|
Datacenter string
|
|
|
|
|
Name string
|
|
|
|
|
NodeClass string
|
|
|
|
|
Version string
|
|
|
|
|
Drain bool
|
|
|
|
|
SchedulingEligibility string
|
|
|
|
|
Status string
|
|
|
|
|
StatusDescription string
|
|
|
|
|
CreateIndex uint64
|
|
|
|
|
ModifyIndex uint64
|
2015-09-11 23:51:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 19:40:51 +00:00
|
|
|
|
// NodeIndexSort reverse sorts nodes by CreateIndex
|
|
|
|
|
type NodeIndexSort []*NodeListStub
|
|
|
|
|
|
|
|
|
|
func (n NodeIndexSort) Len() int {
|
|
|
|
|
return len(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NodeIndexSort) Less(i, j int) bool {
|
|
|
|
|
return n[i].CreateIndex > n[j].CreateIndex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n NodeIndexSort) Swap(i, j int) {
|
|
|
|
|
n[i], n[j] = n[j], n[i]
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
|
// nodeEvalResponse is used to decode a force-eval.
|
|
|
|
|
type nodeEvalResponse struct {
|
|
|
|
|
EvalID string
|
|
|
|
|
}
|
2015-11-09 16:54:18 +00:00
|
|
|
|
|
|
|
|
|
// AllocationSort reverse sorts allocs by CreateIndex.
|
|
|
|
|
type AllocationSort []*Allocation
|
|
|
|
|
|
|
|
|
|
func (a AllocationSort) Len() int {
|
|
|
|
|
return len(a)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AllocationSort) Less(i, j int) bool {
|
|
|
|
|
return a[i].CreateIndex > a[j].CreateIndex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a AllocationSort) Swap(i, j int) {
|
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
|
}
|