github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/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 // Allocation is used for serialization of allocations. 71 type Allocation struct { 72 ID string 73 EvalID string 74 Name string 75 NodeID string 76 JobID string 77 Job *Job 78 TaskGroup string 79 Resources *Resources 80 TaskResources map[string]*Resources 81 Services map[string]string 82 Metrics *AllocationMetric 83 DesiredStatus string 84 DesiredDescription string 85 ClientStatus string 86 ClientDescription string 87 TaskStates map[string]*TaskState 88 PreviousAllocation string 89 CreateIndex uint64 90 ModifyIndex uint64 91 CreateTime int64 92 } 93 94 // AllocationMetric is used to deserialize allocation metrics. 95 type AllocationMetric struct { 96 NodesEvaluated int 97 NodesFiltered int 98 NodesAvailable map[string]int 99 ClassFiltered map[string]int 100 ConstraintFiltered map[string]int 101 NodesExhausted int 102 ClassExhausted map[string]int 103 DimensionExhausted map[string]int 104 Scores map[string]float64 105 AllocationTime time.Duration 106 CoalescedFailures int 107 } 108 109 // AllocationListStub is used to return a subset of an allocation 110 // during list operations. 111 type AllocationListStub struct { 112 ID string 113 EvalID string 114 Name string 115 NodeID string 116 JobID string 117 TaskGroup string 118 DesiredStatus string 119 DesiredDescription string 120 ClientStatus string 121 ClientDescription string 122 TaskStates map[string]*TaskState 123 CreateIndex uint64 124 ModifyIndex uint64 125 CreateTime int64 126 } 127 128 // AllocIndexSort reverse sorts allocs by CreateIndex. 129 type AllocIndexSort []*AllocationListStub 130 131 func (a AllocIndexSort) Len() int { 132 return len(a) 133 } 134 135 func (a AllocIndexSort) Less(i, j int) bool { 136 return a[i].CreateIndex > a[j].CreateIndex 137 } 138 139 func (a AllocIndexSort) Swap(i, j int) { 140 a[i], a[j] = a[j], a[i] 141 }