github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/scaling.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  const (
     7  	// ScalingPolicyTypeHorizontal indicates a policy that does horizontal scaling.
     8  	ScalingPolicyTypeHorizontal = "horizontal"
     9  )
    10  
    11  // Scaling is used to query scaling-related API endpoints
    12  type Scaling struct {
    13  	client *Client
    14  }
    15  
    16  // Scaling returns a handle on the scaling endpoints.
    17  func (c *Client) Scaling() *Scaling {
    18  	return &Scaling{client: c}
    19  }
    20  
    21  func (s *Scaling) ListPolicies(q *QueryOptions) ([]*ScalingPolicyListStub, *QueryMeta, error) {
    22  	var resp []*ScalingPolicyListStub
    23  	qm, err := s.client.query("/v1/scaling/policies", &resp, q)
    24  	if err != nil {
    25  		return nil, nil, err
    26  	}
    27  	return resp, qm, nil
    28  }
    29  
    30  func (s *Scaling) GetPolicy(id string, q *QueryOptions) (*ScalingPolicy, *QueryMeta, error) {
    31  	var policy ScalingPolicy
    32  	qm, err := s.client.query("/v1/scaling/policy/"+id, &policy, q)
    33  	if err != nil {
    34  		return nil, nil, err
    35  	}
    36  	return &policy, qm, nil
    37  }
    38  
    39  func (p *ScalingPolicy) Canonicalize(taskGroupCount int) {
    40  	if p.Enabled == nil {
    41  		p.Enabled = pointerOf(true)
    42  	}
    43  	if p.Min == nil {
    44  		var m int64 = int64(taskGroupCount)
    45  		p.Min = &m
    46  	}
    47  	if p.Type == "" {
    48  		p.Type = ScalingPolicyTypeHorizontal
    49  	}
    50  }
    51  
    52  // ScalingRequest is the payload for a generic scaling action
    53  type ScalingRequest struct {
    54  	Count   *int64
    55  	Target  map[string]string
    56  	Message string
    57  	Error   bool
    58  	Meta    map[string]interface{}
    59  	WriteRequest
    60  	// this is effectively a job update, so we need the ability to override policy.
    61  	PolicyOverride bool
    62  }
    63  
    64  // ScalingPolicy is the user-specified API object for an autoscaling policy
    65  type ScalingPolicy struct {
    66  	/* fields set by user in HCL config */
    67  
    68  	Min     *int64                 `hcl:"min,optional"`
    69  	Max     *int64                 `hcl:"max,optional"`
    70  	Policy  map[string]interface{} `hcl:"policy,block"`
    71  	Enabled *bool                  `hcl:"enabled,optional"`
    72  	Type    string                 `hcl:"type,optional"`
    73  
    74  	/* fields set by server */
    75  
    76  	ID          string
    77  	Namespace   string
    78  	Target      map[string]string
    79  	CreateIndex uint64
    80  	ModifyIndex uint64
    81  }
    82  
    83  // ScalingPolicyListStub is used to return a subset of scaling policy information
    84  // for the scaling policy list
    85  type ScalingPolicyListStub struct {
    86  	ID          string
    87  	Enabled     bool
    88  	Type        string
    89  	Target      map[string]string
    90  	CreateIndex uint64
    91  	ModifyIndex uint64
    92  }
    93  
    94  // JobScaleStatusResponse is used to return information about job scaling status
    95  type JobScaleStatusResponse struct {
    96  	JobID          string
    97  	Namespace      string
    98  	JobCreateIndex uint64
    99  	JobModifyIndex uint64
   100  	JobStopped     bool
   101  	TaskGroups     map[string]TaskGroupScaleStatus
   102  }
   103  
   104  type TaskGroupScaleStatus struct {
   105  	Desired   int
   106  	Placed    int
   107  	Running   int
   108  	Healthy   int
   109  	Unhealthy int
   110  	Events    []ScalingEvent
   111  }
   112  
   113  type ScalingEvent struct {
   114  	Count         *int64
   115  	PreviousCount int64
   116  	Error         bool
   117  	Message       string
   118  	Meta          map[string]interface{}
   119  	EvalID        *string
   120  	Time          uint64
   121  	CreateIndex   uint64
   122  }