2015-09-08 22:37:07 +00:00
|
|
|
package api
|
|
|
|
|
2015-09-14 02:55:47 +00:00
|
|
|
import (
|
2016-04-29 20:03:02 +00:00
|
|
|
"fmt"
|
2015-09-17 19:40:51 +00:00
|
|
|
"sort"
|
2015-09-14 02:55:47 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-10-21 01:05:58 +00:00
|
|
|
var (
|
2016-10-25 18:31:09 +00:00
|
|
|
// NodeDownErr marks an operation as not able to complete since the node is
|
|
|
|
// down.
|
2016-10-21 01:05:58 +00:00
|
|
|
NodeDownErr = fmt.Errorf("node down")
|
|
|
|
)
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
// Allocations is used to query the alloc-related endpoints.
|
|
|
|
type Allocations struct {
|
2015-09-08 22:37:07 +00:00
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
// Allocations returns a handle on the allocs endpoints.
|
|
|
|
func (c *Client) Allocations() *Allocations {
|
|
|
|
return &Allocations{client: c}
|
2015-09-08 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of all of the allocations.
|
2015-09-14 02:55:47 +00:00
|
|
|
func (a *Allocations) List(q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
|
|
|
|
var resp []*AllocationListStub
|
2015-09-09 20:18:50 +00:00
|
|
|
qm, err := a.client.query("/v1/allocations", &resp, q)
|
2015-09-08 22:37:07 +00:00
|
|
|
if err != nil {
|
2015-09-08 23:45:16 +00:00
|
|
|
return nil, nil, err
|
2015-09-08 22:37:07 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
sort.Sort(AllocIndexSort(resp))
|
2015-09-08 23:45:16 +00:00
|
|
|
return resp, qm, nil
|
2015-09-08 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func (a *Allocations) PrefixList(prefix string) ([]*AllocationListStub, *QueryMeta, error) {
|
|
|
|
return a.List(&QueryOptions{Prefix: prefix})
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:18:50 +00:00
|
|
|
// Info is used to retrieve a single allocation.
|
|
|
|
func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *QueryMeta, error) {
|
|
|
|
var resp Allocation
|
|
|
|
qm, err := a.client.query("/v1/allocation/"+allocID, &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:15:50 +00:00
|
|
|
func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (*AllocResourceUsage, error) {
|
2016-05-22 03:05:51 +00:00
|
|
|
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
|
2016-04-29 20:03:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-21 01:05:58 +00:00
|
|
|
if node.Status == "down" {
|
|
|
|
return nil, NodeDownErr
|
|
|
|
}
|
2016-04-29 20:03:02 +00:00
|
|
|
if node.HTTPAddr == "" {
|
|
|
|
return nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
|
|
|
|
}
|
2016-10-26 18:13:53 +00:00
|
|
|
client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
|
2016-04-29 20:03:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-12 03:15:50 +00:00
|
|
|
var resp AllocResourceUsage
|
2016-06-08 16:28:41 +00:00
|
|
|
_, err = client.query("/v1/client/allocation/"+alloc.ID+"/stats", &resp, nil)
|
2016-06-12 03:15:50 +00:00
|
|
|
return &resp, err
|
2016-04-29 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 00:18:29 +00:00
|
|
|
func (a *Allocations) GC(alloc *Allocation, q *QueryOptions) error {
|
|
|
|
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node.Status == "down" {
|
|
|
|
return NodeDownErr
|
|
|
|
}
|
|
|
|
if node.HTTPAddr == "" {
|
|
|
|
return fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
|
|
|
|
}
|
|
|
|
client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct{}
|
|
|
|
_, err = client.query("/v1/client/allocation"+alloc.ID+"/gc", &resp, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
// Allocation is used for serialization of allocations.
|
|
|
|
type Allocation struct {
|
2015-09-14 02:55:47 +00:00
|
|
|
ID string
|
|
|
|
EvalID string
|
|
|
|
Name string
|
|
|
|
NodeID string
|
|
|
|
JobID string
|
|
|
|
Job *Job
|
|
|
|
TaskGroup string
|
|
|
|
Resources *Resources
|
|
|
|
TaskResources map[string]*Resources
|
2015-12-14 22:53:49 +00:00
|
|
|
Services map[string]string
|
2015-09-14 02:55:47 +00:00
|
|
|
Metrics *AllocationMetric
|
|
|
|
DesiredStatus string
|
|
|
|
DesiredDescription string
|
|
|
|
ClientStatus string
|
|
|
|
ClientDescription string
|
2015-11-12 23:28:22 +00:00
|
|
|
TaskStates map[string]*TaskState
|
2017-07-02 21:01:33 +00:00
|
|
|
DeploymentID string
|
2017-06-26 21:23:52 +00:00
|
|
|
DeploymentStatus *AllocDeploymentStatus
|
2016-10-28 22:50:35 +00:00
|
|
|
PreviousAllocation string
|
2015-09-14 02:55:47 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2017-01-07 21:41:09 +00:00
|
|
|
AllocModifyIndex uint64
|
2016-02-09 05:58:05 +00:00
|
|
|
CreateTime int64
|
2015-09-14 02:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AllocationMetric is used to deserialize allocation metrics.
|
|
|
|
type AllocationMetric struct {
|
|
|
|
NodesEvaluated int
|
|
|
|
NodesFiltered int
|
2016-01-04 22:23:06 +00:00
|
|
|
NodesAvailable map[string]int
|
2015-09-14 02:55:47 +00:00
|
|
|
ClassFiltered map[string]int
|
|
|
|
ConstraintFiltered map[string]int
|
|
|
|
NodesExhausted int
|
|
|
|
ClassExhausted map[string]int
|
2015-09-18 19:06:51 +00:00
|
|
|
DimensionExhausted map[string]int
|
2015-09-14 02:55:47 +00:00
|
|
|
Scores map[string]float64
|
|
|
|
AllocationTime time.Duration
|
|
|
|
CoalescedFailures int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocationListStub is used to return a subset of an allocation
|
|
|
|
// during list operations.
|
|
|
|
type AllocationListStub struct {
|
2015-09-08 22:37:07 +00:00
|
|
|
ID string
|
|
|
|
EvalID string
|
|
|
|
Name string
|
|
|
|
NodeID string
|
|
|
|
JobID string
|
2017-07-07 04:51:13 +00:00
|
|
|
JobVersion uint64
|
2015-09-08 22:37:07 +00:00
|
|
|
TaskGroup string
|
|
|
|
DesiredStatus string
|
|
|
|
DesiredDescription string
|
|
|
|
ClientStatus string
|
|
|
|
ClientDescription string
|
2015-11-12 23:28:22 +00:00
|
|
|
TaskStates map[string]*TaskState
|
2017-06-26 21:23:52 +00:00
|
|
|
DeploymentStatus *AllocDeploymentStatus
|
2015-09-14 02:55:47 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2016-02-09 05:58:05 +00:00
|
|
|
CreateTime int64
|
2015-09-08 22:37:07 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// AllocDeploymentStatus captures the status of the allocation as part of the
|
|
|
|
// deployment. This can include things like if the allocation has been marked as
|
|
|
|
// heatlhy.
|
|
|
|
type AllocDeploymentStatus struct {
|
|
|
|
Healthy *bool
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:40:51 +00:00
|
|
|
// AllocIndexSort reverse sorts allocs by CreateIndex.
|
|
|
|
type AllocIndexSort []*AllocationListStub
|
|
|
|
|
|
|
|
func (a AllocIndexSort) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AllocIndexSort) Less(i, j int) bool {
|
|
|
|
return a[i].CreateIndex > a[j].CreateIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AllocIndexSort) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|