2020-01-16 22:14:00 +00:00
|
|
|
package api
|
|
|
|
|
2020-09-29 21:57:46 +00:00
|
|
|
const (
|
|
|
|
// ScalingPolicyTypeHorizontal indicates a policy that does horizontal scaling.
|
|
|
|
ScalingPolicyTypeHorizontal = "horizontal"
|
|
|
|
)
|
|
|
|
|
2020-01-30 00:26:31 +00:00
|
|
|
// Scaling is used to query scaling-related API endpoints
|
|
|
|
type Scaling struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scaling returns a handle on the scaling endpoints.
|
|
|
|
func (c *Client) Scaling() *Scaling {
|
|
|
|
return &Scaling{client: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scaling) ListPolicies(q *QueryOptions) ([]*ScalingPolicyListStub, *QueryMeta, error) {
|
|
|
|
var resp []*ScalingPolicyListStub
|
|
|
|
qm, err := s.client.query("/v1/scaling/policies", &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scaling) GetPolicy(ID string, q *QueryOptions) (*ScalingPolicy, *QueryMeta, error) {
|
|
|
|
var policy ScalingPolicy
|
|
|
|
qm, err := s.client.query("/v1/scaling/policy/"+ID, &policy, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &policy, qm, nil
|
2020-01-16 22:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:29:34 +00:00
|
|
|
func (p *ScalingPolicy) Canonicalize(taskGroupCount int) {
|
2020-01-16 22:14:00 +00:00
|
|
|
if p.Enabled == nil {
|
|
|
|
p.Enabled = boolToPtr(true)
|
|
|
|
}
|
2020-03-22 11:54:04 +00:00
|
|
|
if p.Min == nil {
|
2020-03-24 19:29:34 +00:00
|
|
|
var m int64 = int64(taskGroupCount)
|
2020-03-22 11:54:04 +00:00
|
|
|
p.Min = &m
|
|
|
|
}
|
2020-09-29 21:57:46 +00:00
|
|
|
if p.Type == "" {
|
|
|
|
p.Type = ScalingPolicyTypeHorizontal
|
|
|
|
}
|
2020-01-16 22:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 21:19:15 +00:00
|
|
|
// ScalingRequest is the payload for a generic scaling action
|
2020-01-16 22:14:00 +00:00
|
|
|
type ScalingRequest struct {
|
2020-03-23 13:38:18 +00:00
|
|
|
Count *int64
|
|
|
|
Target map[string]string
|
|
|
|
Message string
|
|
|
|
Error bool
|
|
|
|
Meta map[string]interface{}
|
2020-01-16 22:14:00 +00:00
|
|
|
WriteRequest
|
2020-02-05 21:19:15 +00:00
|
|
|
// this is effectively a job update, so we need the ability to override policy.
|
|
|
|
PolicyOverride bool
|
2020-01-16 22:14:00 +00:00
|
|
|
}
|
2020-01-26 16:13:56 +00:00
|
|
|
|
2020-01-30 00:26:31 +00:00
|
|
|
// ScalingPolicy is the user-specified API object for an autoscaling policy
|
|
|
|
type ScalingPolicy struct {
|
2020-10-22 15:49:37 +00:00
|
|
|
/* fields set by user in HCL config */
|
|
|
|
|
|
|
|
Min *int64 `hcl:"min,optional"`
|
|
|
|
Max *int64 `hcl:"max,optional"`
|
|
|
|
Policy map[string]interface{} `hcl:"policy,block"`
|
|
|
|
Enabled *bool `hcl:"enabled,optional"`
|
|
|
|
|
|
|
|
/* fields set by server */
|
|
|
|
|
|
|
|
ID string
|
|
|
|
Namespace string
|
|
|
|
Type string
|
|
|
|
Target map[string]string
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2020-01-30 00:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScalingPolicyListStub is used to return a subset of scaling policy information
|
|
|
|
// for the scaling policy list
|
|
|
|
type ScalingPolicyListStub struct {
|
|
|
|
ID string
|
2020-02-21 21:23:30 +00:00
|
|
|
Enabled bool
|
2020-09-29 21:57:46 +00:00
|
|
|
Type string
|
2020-02-21 21:23:30 +00:00
|
|
|
Target map[string]string
|
2020-01-30 00:26:31 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2020-03-21 15:03:22 +00:00
|
|
|
// JobScaleStatusResponse is used to return information about job scaling status
|
2020-03-20 22:00:31 +00:00
|
|
|
type JobScaleStatusResponse struct {
|
2020-01-26 16:13:56 +00:00
|
|
|
JobID string
|
2020-07-24 09:19:25 +00:00
|
|
|
Namespace string
|
2020-03-18 14:32:59 +00:00
|
|
|
JobCreateIndex uint64
|
2020-01-26 16:13:56 +00:00
|
|
|
JobModifyIndex uint64
|
2020-03-20 22:00:31 +00:00
|
|
|
JobStopped bool
|
2020-03-18 14:32:59 +00:00
|
|
|
TaskGroups map[string]TaskGroupScaleStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskGroupScaleStatus struct {
|
2020-03-21 15:20:25 +00:00
|
|
|
Desired int
|
|
|
|
Placed int
|
|
|
|
Running int
|
|
|
|
Healthy int
|
|
|
|
Unhealthy int
|
|
|
|
Events []ScalingEvent
|
2020-03-18 14:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ScalingEvent struct {
|
2020-06-15 19:40:21 +00:00
|
|
|
Count *int64
|
|
|
|
PreviousCount int64
|
|
|
|
Error bool
|
|
|
|
Message string
|
|
|
|
Meta map[string]interface{}
|
|
|
|
EvalID *string
|
|
|
|
Time uint64
|
|
|
|
CreateIndex uint64
|
2020-01-26 16:13:56 +00:00
|
|
|
}
|