github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/api/nodes.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strconv"
     7  )
     8  
     9  // Nodes is used to query node-related API endpoints
    10  type Nodes struct {
    11  	client *Client
    12  }
    13  
    14  // Nodes returns a handle on the node endpoints.
    15  func (c *Client) Nodes() *Nodes {
    16  	return &Nodes{client: c}
    17  }
    18  
    19  // List is used to list out all of the nodes
    20  func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
    21  	var resp NodeIndexSort
    22  	qm, err := n.client.query("/v1/nodes", &resp, q)
    23  	if err != nil {
    24  		return nil, nil, err
    25  	}
    26  	sort.Sort(resp)
    27  	return resp, qm, nil
    28  }
    29  
    30  func (n *Nodes) PrefixList(prefix string) ([]*NodeListStub, *QueryMeta, error) {
    31  	return n.List(&QueryOptions{Prefix: prefix})
    32  }
    33  
    34  // Info is used to query a specific node by its ID.
    35  func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) {
    36  	var resp Node
    37  	qm, err := n.client.query("/v1/node/"+nodeID, &resp, q)
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  	return &resp, qm, nil
    42  }
    43  
    44  // ToggleDrain is used to toggle drain mode on/off for a given node.
    45  func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) {
    46  	drainArg := strconv.FormatBool(drain)
    47  	wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return wm, nil
    52  }
    53  
    54  // Allocations is used to return the allocations associated with a node.
    55  func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
    56  	var resp []*Allocation
    57  	qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  	sort.Sort(AllocationSort(resp))
    62  	return resp, qm, nil
    63  }
    64  
    65  // ForceEvaluate is used to force-evaluate an existing node.
    66  func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) {
    67  	var resp nodeEvalResponse
    68  	wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q)
    69  	if err != nil {
    70  		return "", nil, err
    71  	}
    72  	return resp.EvalID, wm, nil
    73  }
    74  
    75  func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
    76  	var resp HostStats
    77  	path := fmt.Sprintf("/v1/client/stats?node_id=%s", nodeID)
    78  	if _, err := n.client.query(path, &resp, q); err != nil {
    79  		return nil, err
    80  	}
    81  	return &resp, nil
    82  }
    83  
    84  func (n *Nodes) GC(nodeID string, q *QueryOptions) error {
    85  	var resp struct{}
    86  	path := fmt.Sprintf("/v1/client/gc?node_id=%s", nodeID)
    87  	_, err := n.client.query(path, &resp, q)
    88  	return err
    89  }
    90  
    91  // TODO Add tests
    92  func (n *Nodes) GcAlloc(allocID string, q *QueryOptions) error {
    93  	var resp struct{}
    94  	path := fmt.Sprintf("/v1/client/allocation/%s/gc", allocID)
    95  	_, err := n.client.query(path, &resp, q)
    96  	return err
    97  }
    98  
    99  // Node is used to deserialize a node entry.
   100  type Node struct {
   101  	ID                string
   102  	Datacenter        string
   103  	Name              string
   104  	HTTPAddr          string
   105  	TLSEnabled        bool
   106  	Attributes        map[string]string
   107  	Resources         *Resources
   108  	Reserved          *Resources
   109  	Links             map[string]string
   110  	Meta              map[string]string
   111  	NodeClass         string
   112  	Drain             bool
   113  	Status            string
   114  	StatusDescription string
   115  	StatusUpdatedAt   int64
   116  	CreateIndex       uint64
   117  	ModifyIndex       uint64
   118  }
   119  
   120  // HostStats represents resource usage stats of the host running a Nomad client
   121  type HostStats struct {
   122  	Memory           *HostMemoryStats
   123  	CPU              []*HostCPUStats
   124  	DiskStats        []*HostDiskStats
   125  	Uptime           uint64
   126  	CPUTicksConsumed float64
   127  }
   128  
   129  type HostMemoryStats struct {
   130  	Total     uint64
   131  	Available uint64
   132  	Used      uint64
   133  	Free      uint64
   134  }
   135  
   136  type HostCPUStats struct {
   137  	CPU    string
   138  	User   float64
   139  	System float64
   140  	Idle   float64
   141  }
   142  
   143  type HostDiskStats struct {
   144  	Device            string
   145  	Mountpoint        string
   146  	Size              uint64
   147  	Used              uint64
   148  	Available         uint64
   149  	UsedPercent       float64
   150  	InodesUsedPercent float64
   151  }
   152  
   153  // NodeListStub is a subset of information returned during
   154  // node list operations.
   155  type NodeListStub struct {
   156  	Address           string
   157  	ID                string
   158  	Datacenter        string
   159  	Name              string
   160  	NodeClass         string
   161  	Version           string
   162  	Drain             bool
   163  	Status            string
   164  	StatusDescription string
   165  	CreateIndex       uint64
   166  	ModifyIndex       uint64
   167  }
   168  
   169  // NodeIndexSort reverse sorts nodes by CreateIndex
   170  type NodeIndexSort []*NodeListStub
   171  
   172  func (n NodeIndexSort) Len() int {
   173  	return len(n)
   174  }
   175  
   176  func (n NodeIndexSort) Less(i, j int) bool {
   177  	return n[i].CreateIndex > n[j].CreateIndex
   178  }
   179  
   180  func (n NodeIndexSort) Swap(i, j int) {
   181  	n[i], n[j] = n[j], n[i]
   182  }
   183  
   184  // nodeEvalResponse is used to decode a force-eval.
   185  type nodeEvalResponse struct {
   186  	EvalID string
   187  }
   188  
   189  // AllocationSort reverse sorts allocs by CreateIndex.
   190  type AllocationSort []*Allocation
   191  
   192  func (a AllocationSort) Len() int {
   193  	return len(a)
   194  }
   195  
   196  func (a AllocationSort) Less(i, j int) bool {
   197  	return a[i].CreateIndex > a[j].CreateIndex
   198  }
   199  
   200  func (a AllocationSort) Swap(i, j int) {
   201  	a[i], a[j] = a[j], a[i]
   202  }