github.com/hooklift/nomad@v0.5.7-0.20170407200202-db11e7dd7b55/api/allocations.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"time"
     7  )
     8  
     9  var (
    10  	// NodeDownErr marks an operation as not able to complete since the node is
    11  	// down.
    12  	NodeDownErr = fmt.Errorf("node down")
    13  )
    14  
    15  // Allocations is used to query the alloc-related endpoints.
    16  type Allocations struct {
    17  	client *Client
    18  }
    19  
    20  // Allocations returns a handle on the allocs endpoints.
    21  func (c *Client) Allocations() *Allocations {
    22  	return &Allocations{client: c}
    23  }
    24  
    25  // List returns a list of all of the allocations.
    26  func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
    27  	var resp []*AllocationListStub
    28  	qm, err := a.client.query("/v1/allocations", &resp, q)
    29  	if err != nil {
    30  		return nil, nil, err
    31  	}
    32  	sort.Sort(AllocIndexSort(resp))
    33  	return resp, qm, nil
    34  }
    35  
    36  func (a *Allocations) PrefixList(prefix string) ([]*AllocationListStub, *QueryMeta, error) {
    37  	return a.List(&QueryOptions{Prefix: prefix})
    38  }
    39  
    40  // Info is used to retrieve a single allocation.
    41  func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *QueryMeta, error) {
    42  	var resp Allocation
    43  	qm, err := a.client.query("/v1/allocation/"+allocID, &resp, q)
    44  	if err != nil {
    45  		return nil, nil, err
    46  	}
    47  	return &resp, qm, nil
    48  }
    49  
    50  func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (*AllocResourceUsage, error) {
    51  	node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	if node.Status == "down" {
    56  		return nil, NodeDownErr
    57  	}
    58  	if node.HTTPAddr == "" {
    59  		return nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
    60  	}
    61  	client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	var resp AllocResourceUsage
    66  	_, err = client.query("/v1/client/allocation/"+alloc.ID+"/stats", &resp, nil)
    67  	return &resp, err
    68  }
    69  
    70  func (a *Allocations) GC(alloc *Allocation, q *QueryOptions) error {
    71  	node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if node.Status == "down" {
    76  		return NodeDownErr
    77  	}
    78  	if node.HTTPAddr == "" {
    79  		return fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
    80  	}
    81  	client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	var resp struct{}
    87  	_, err = client.query("/v1/client/allocation"+alloc.ID+"/gc", &resp, nil)
    88  	return err
    89  }
    90  
    91  // Allocation is used for serialization of allocations.
    92  type Allocation struct {
    93  	ID                 string
    94  	EvalID             string
    95  	Name               string
    96  	NodeID             string
    97  	JobID              string
    98  	Job                *Job
    99  	TaskGroup          string
   100  	Resources          *Resources
   101  	TaskResources      map[string]*Resources
   102  	Services           map[string]string
   103  	Metrics            *AllocationMetric
   104  	DesiredStatus      string
   105  	DesiredDescription string
   106  	ClientStatus       string
   107  	ClientDescription  string
   108  	TaskStates         map[string]*TaskState
   109  	PreviousAllocation string
   110  	CreateIndex        uint64
   111  	ModifyIndex        uint64
   112  	AllocModifyIndex   uint64
   113  	CreateTime         int64
   114  }
   115  
   116  // AllocationMetric is used to deserialize allocation metrics.
   117  type AllocationMetric struct {
   118  	NodesEvaluated     int
   119  	NodesFiltered      int
   120  	NodesAvailable     map[string]int
   121  	ClassFiltered      map[string]int
   122  	ConstraintFiltered map[string]int
   123  	NodesExhausted     int
   124  	ClassExhausted     map[string]int
   125  	DimensionExhausted map[string]int
   126  	Scores             map[string]float64
   127  	AllocationTime     time.Duration
   128  	CoalescedFailures  int
   129  }
   130  
   131  // AllocationListStub is used to return a subset of an allocation
   132  // during list operations.
   133  type AllocationListStub struct {
   134  	ID                 string
   135  	EvalID             string
   136  	Name               string
   137  	NodeID             string
   138  	JobID              string
   139  	TaskGroup          string
   140  	DesiredStatus      string
   141  	DesiredDescription string
   142  	ClientStatus       string
   143  	ClientDescription  string
   144  	TaskStates         map[string]*TaskState
   145  	CreateIndex        uint64
   146  	ModifyIndex        uint64
   147  	CreateTime         int64
   148  }
   149  
   150  // AllocIndexSort reverse sorts allocs by CreateIndex.
   151  type AllocIndexSort []*AllocationListStub
   152  
   153  func (a AllocIndexSort) Len() int {
   154  	return len(a)
   155  }
   156  
   157  func (a AllocIndexSort) Less(i, j int) bool {
   158  	return a[i].CreateIndex > a[j].CreateIndex
   159  }
   160  
   161  func (a AllocIndexSort) Swap(i, j int) {
   162  	a[i], a[j] = a[j], a[i]
   163  }