github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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 nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q) 52 if err != nil { 53 return nil, err 54 } 55 56 var resp AllocResourceUsage 57 _, err = nodeClient.query("/v1/client/allocation/"+alloc.ID+"/stats", &resp, nil) 58 return &resp, err 59 } 60 61 func (a *Allocations) GC(alloc *Allocation, q *QueryOptions) error { 62 nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q) 63 if err != nil { 64 return err 65 } 66 67 var resp struct{} 68 _, err = nodeClient.query("/v1/client/allocation/"+alloc.ID+"/gc", &resp, nil) 69 return err 70 } 71 72 // Allocation is used for serialization of allocations. 73 type Allocation struct { 74 ID string 75 Namespace string 76 EvalID string 77 Name string 78 NodeID string 79 JobID string 80 Job *Job 81 TaskGroup string 82 Resources *Resources 83 TaskResources map[string]*Resources 84 Services map[string]string 85 Metrics *AllocationMetric 86 DesiredStatus string 87 DesiredDescription string 88 ClientStatus string 89 ClientDescription string 90 TaskStates map[string]*TaskState 91 DeploymentID string 92 DeploymentStatus *AllocDeploymentStatus 93 PreviousAllocation string 94 CreateIndex uint64 95 ModifyIndex uint64 96 AllocModifyIndex uint64 97 CreateTime int64 98 ModifyTime int64 99 } 100 101 // AllocationMetric is used to deserialize allocation metrics. 102 type AllocationMetric struct { 103 NodesEvaluated int 104 NodesFiltered int 105 NodesAvailable map[string]int 106 ClassFiltered map[string]int 107 ConstraintFiltered map[string]int 108 NodesExhausted int 109 ClassExhausted map[string]int 110 DimensionExhausted map[string]int 111 QuotaExhausted []string 112 Scores map[string]float64 113 AllocationTime time.Duration 114 CoalescedFailures int 115 } 116 117 // AllocationListStub is used to return a subset of an allocation 118 // during list operations. 119 type AllocationListStub struct { 120 ID string 121 EvalID string 122 Name string 123 NodeID string 124 JobID string 125 JobVersion uint64 126 TaskGroup string 127 DesiredStatus string 128 DesiredDescription string 129 ClientStatus string 130 ClientDescription string 131 TaskStates map[string]*TaskState 132 DeploymentStatus *AllocDeploymentStatus 133 CreateIndex uint64 134 ModifyIndex uint64 135 CreateTime int64 136 ModifyTime int64 137 } 138 139 // AllocDeploymentStatus captures the status of the allocation as part of the 140 // deployment. This can include things like if the allocation has been marked as 141 // healthy. 142 type AllocDeploymentStatus struct { 143 Healthy *bool 144 ModifyIndex uint64 145 } 146 147 // AllocIndexSort reverse sorts allocs by CreateIndex. 148 type AllocIndexSort []*AllocationListStub 149 150 func (a AllocIndexSort) Len() int { 151 return len(a) 152 } 153 154 func (a AllocIndexSort) Less(i, j int) bool { 155 return a[i].CreateIndex > a[j].CreateIndex 156 } 157 158 func (a AllocIndexSort) Swap(i, j int) { 159 a[i], a[j] = a[j], a[i] 160 }