github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/api/allocations.go (about)

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