2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
package api
|
|
|
|
|
2015-09-17 19:40:51 +00:00
|
|
|
import (
|
2022-08-05 15:05:47 +00:00
|
|
|
"errors"
|
2016-05-12 01:51:48 +00:00
|
|
|
"fmt"
|
2016-11-24 12:20:52 +00:00
|
|
|
"net/url"
|
2015-09-17 19:40:51 +00:00
|
|
|
"sort"
|
2016-11-24 12:20:52 +00:00
|
|
|
"strconv"
|
2015-10-20 23:42:53 +00:00
|
|
|
"time"
|
2017-02-06 19:48:28 +00:00
|
|
|
|
2020-05-07 21:50:45 +00:00
|
|
|
"github.com/hashicorp/cronexpr"
|
2023-04-11 13:45:08 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2015-09-17 19:40:51 +00:00
|
|
|
)
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
const (
|
|
|
|
// JobTypeService indicates a long-running processes
|
|
|
|
JobTypeService = "service"
|
|
|
|
|
|
|
|
// JobTypeBatch indicates a short-lived process
|
|
|
|
JobTypeBatch = "batch"
|
2017-02-06 19:48:28 +00:00
|
|
|
|
2019-01-18 17:48:15 +00:00
|
|
|
// JobTypeSystem indicates a system process that should run on all clients
|
|
|
|
JobTypeSystem = "system"
|
|
|
|
|
2022-06-16 09:53:04 +00:00
|
|
|
// JobTypeSysbatch indicates a short-lived system process that should run
|
|
|
|
// on all clients.
|
|
|
|
JobTypeSysbatch = "sysbatch"
|
|
|
|
|
2023-05-30 07:05:32 +00:00
|
|
|
// JobDefaultPriority is the default priority if not specified.
|
|
|
|
JobDefaultPriority = 50
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
// PeriodicSpecCron is used for a cron spec.
|
|
|
|
PeriodicSpecCron = "cron"
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// DefaultNamespace is the default namespace.
|
|
|
|
DefaultNamespace = "default"
|
2019-08-05 21:28:02 +00:00
|
|
|
|
2023-06-01 20:08:55 +00:00
|
|
|
// NodePoolDefault is the default node pool.
|
|
|
|
NodePoolDefault = "default"
|
|
|
|
|
2019-08-05 21:28:02 +00:00
|
|
|
// For Job configuration, GlobalRegion is a sentinel region value
|
|
|
|
// that users may specify to indicate the job should be run on
|
|
|
|
// the region of the node that the job was submitted to.
|
|
|
|
// For Client configuration, if no region information is given,
|
|
|
|
// the client node will default to be part of the GlobalRegion.
|
|
|
|
GlobalRegion = "global"
|
2015-09-10 00:29:43 +00:00
|
|
|
)
|
|
|
|
|
2016-06-08 23:48:02 +00:00
|
|
|
const (
|
|
|
|
// RegisterEnforceIndexErrPrefix is the prefix to use in errors caused by
|
|
|
|
// enforcing the job modify index during registers.
|
|
|
|
RegisterEnforceIndexErrPrefix = "Enforcing job modify index"
|
|
|
|
)
|
|
|
|
|
2022-08-02 14:33:08 +00:00
|
|
|
const (
|
|
|
|
// JobPeriodicLaunchSuffix is the string appended to the periodic jobs ID
|
|
|
|
// when launching derived instances of it.
|
|
|
|
JobPeriodicLaunchSuffix = "/periodic-"
|
|
|
|
|
|
|
|
// JobDispatchLaunchSuffix is the string appended to the parameterized job's ID
|
|
|
|
// when dispatching instances of it.
|
|
|
|
JobDispatchLaunchSuffix = "/dispatch-"
|
|
|
|
)
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
// Jobs is used to access the job-specific endpoints.
|
|
|
|
type Jobs struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2020-04-27 08:25:29 +00:00
|
|
|
// JobsParseRequest is used for arguments of the /v1/jobs/parse endpoint
|
2018-04-11 18:57:12 +00:00
|
|
|
type JobsParseRequest struct {
|
2018-04-17 14:18:36 +00:00
|
|
|
// JobHCL is an hcl jobspec
|
2018-04-11 18:57:12 +00:00
|
|
|
JobHCL string
|
2018-04-16 23:17:58 +00:00
|
|
|
|
2020-10-21 14:22:56 +00:00
|
|
|
// HCLv1 indicates whether the JobHCL should be parsed with the hcl v1 parser
|
|
|
|
HCLv1 bool `json:"hclv1,omitempty"`
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// Variables are HCL2 variables associated with the job. Only works with hcl2.
|
|
|
|
//
|
|
|
|
// Interpreted as if it were the content of a variables file.
|
|
|
|
Variables string
|
|
|
|
|
2018-04-17 14:18:36 +00:00
|
|
|
// Canonicalize is a flag as to if the server should return default values
|
|
|
|
// for unset fields
|
2018-04-12 18:46:15 +00:00
|
|
|
Canonicalize bool
|
2018-04-11 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
// Jobs returns a handle on the jobs endpoints.
|
|
|
|
func (c *Client) Jobs() *Jobs {
|
|
|
|
return &Jobs{client: c}
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// ParseHCL is used to convert the HCL representation of a Job to JSON server side.
|
2018-04-11 18:57:12 +00:00
|
|
|
// To parse the HCL client side see package github.com/hashicorp/nomad/jobspec
|
2022-04-25 18:51:52 +00:00
|
|
|
// Use ParseHCLOpts if you need to customize JobsParseRequest.
|
2018-04-17 14:18:36 +00:00
|
|
|
func (j *Jobs) ParseHCL(jobHCL string, canonicalize bool) (*Job, error) {
|
2018-04-16 23:17:58 +00:00
|
|
|
req := &JobsParseRequest{
|
|
|
|
JobHCL: jobHCL,
|
|
|
|
Canonicalize: canonicalize,
|
|
|
|
}
|
2022-04-25 18:51:52 +00:00
|
|
|
return j.ParseHCLOpts(req)
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// ParseHCLOpts is used to request the server convert the HCL representation of a
|
|
|
|
// Job to JSON on our behalf. Accepts HCL1 or HCL2 jobs as input.
|
2022-04-25 18:51:52 +00:00
|
|
|
func (j *Jobs) ParseHCLOpts(req *JobsParseRequest) (*Job, error) {
|
|
|
|
var job Job
|
2023-02-07 22:42:25 +00:00
|
|
|
_, err := j.client.put("/v1/jobs/parse", req, &job, nil)
|
2018-04-16 23:17:58 +00:00
|
|
|
return &job, err
|
2018-04-11 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
func (j *Jobs) Validate(job *Job, q *WriteOptions) (*JobValidateResponse, *WriteMeta, error) {
|
|
|
|
var resp JobValidateResponse
|
|
|
|
req := &JobValidateRequest{Job: job}
|
|
|
|
if q != nil {
|
|
|
|
req.WriteRequest = WriteRequest{Region: q.Region}
|
|
|
|
}
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/validate/job", req, &resp, q)
|
2017-02-06 19:48:28 +00:00
|
|
|
return &resp, wm, err
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// RegisterOptions is used to pass through job registration parameters
|
|
|
|
type RegisterOptions struct {
|
|
|
|
EnforceIndex bool
|
|
|
|
ModifyIndex uint64
|
|
|
|
PolicyOverride bool
|
2020-06-16 02:57:50 +00:00
|
|
|
PreserveCounts bool
|
2021-11-23 08:23:31 +00:00
|
|
|
EvalPriority int
|
2023-04-11 13:45:08 +00:00
|
|
|
Submission *JobSubmission
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
// Register is used to register a new job. It returns the ID
|
|
|
|
// of the evaluation, along with any errors encountered.
|
2017-05-10 03:52:47 +00:00
|
|
|
func (j *Jobs) Register(job *Job, q *WriteOptions) (*JobRegisterResponse, *WriteMeta, error) {
|
2017-09-19 14:47:10 +00:00
|
|
|
return j.RegisterOpts(job, nil, q)
|
2016-06-08 23:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnforceRegister is used to register a job enforcing its job modify index.
|
2017-05-10 03:52:47 +00:00
|
|
|
func (j *Jobs) EnforceRegister(job *Job, modifyIndex uint64, q *WriteOptions) (*JobRegisterResponse, *WriteMeta, error) {
|
2017-09-19 14:47:10 +00:00
|
|
|
opts := RegisterOptions{EnforceIndex: true, ModifyIndex: modifyIndex}
|
|
|
|
return j.RegisterOpts(job, &opts, q)
|
|
|
|
}
|
2016-06-08 23:48:02 +00:00
|
|
|
|
2021-11-23 08:23:31 +00:00
|
|
|
// RegisterOpts is used to register a new job with the passed RegisterOpts. It
|
|
|
|
// returns the ID of the evaluation, along with any errors encountered.
|
2017-09-19 14:47:10 +00:00
|
|
|
func (j *Jobs) RegisterOpts(job *Job, opts *RegisterOptions, q *WriteOptions) (*JobRegisterResponse, *WriteMeta, error) {
|
|
|
|
// Format the request
|
2023-04-11 13:45:08 +00:00
|
|
|
req := &JobRegisterRequest{Job: job}
|
2017-09-19 14:47:10 +00:00
|
|
|
if opts != nil {
|
|
|
|
if opts.EnforceIndex {
|
|
|
|
req.EnforceIndex = true
|
|
|
|
req.JobModifyIndex = opts.ModifyIndex
|
|
|
|
}
|
2020-06-16 17:52:47 +00:00
|
|
|
req.PolicyOverride = opts.PolicyOverride
|
|
|
|
req.PreserveCounts = opts.PreserveCounts
|
2021-11-23 08:23:31 +00:00
|
|
|
req.EvalPriority = opts.EvalPriority
|
2023-04-11 13:45:08 +00:00
|
|
|
req.Submission = opts.Submission
|
2017-09-19 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp JobRegisterResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/jobs", req, &resp, q)
|
2015-09-08 23:24:26 +00:00
|
|
|
if err != nil {
|
2017-05-10 03:52:47 +00:00
|
|
|
return nil, nil, err
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
2017-05-10 03:52:47 +00:00
|
|
|
return &resp, wm, nil
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 20:58:24 +00:00
|
|
|
type JobListFields struct {
|
|
|
|
Meta bool
|
|
|
|
}
|
|
|
|
type JobListOptions struct {
|
|
|
|
Fields *JobListFields
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
// List is used to list all of the existing jobs.
|
2015-09-14 02:55:47 +00:00
|
|
|
func (j *Jobs) List(q *QueryOptions) ([]*JobListStub, *QueryMeta, error) {
|
2022-11-02 20:58:24 +00:00
|
|
|
return j.ListOptions(nil, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all of the existing jobs.
|
|
|
|
func (j *Jobs) ListOptions(opts *JobListOptions, q *QueryOptions) ([]*JobListStub, *QueryMeta, error) {
|
2015-09-14 02:55:47 +00:00
|
|
|
var resp []*JobListStub
|
2022-11-02 20:58:24 +00:00
|
|
|
|
|
|
|
destinationURL := "/v1/jobs"
|
|
|
|
|
|
|
|
if opts != nil && opts.Fields != nil {
|
|
|
|
qp := url.Values{}
|
|
|
|
qp.Add("meta", fmt.Sprint(opts.Fields.Meta))
|
|
|
|
destinationURL = destinationURL + "?" + qp.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
qm, err := j.client.query(destinationURL, &resp, q)
|
2015-09-08 23:24:26 +00:00
|
|
|
if err != nil {
|
2015-09-08 23:45:16 +00:00
|
|
|
return nil, qm, err
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
sort.Sort(JobIDSort(resp))
|
2015-09-08 23:45:16 +00:00
|
|
|
return resp, qm, nil
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
// PrefixList is used to list all existing jobs that match the prefix.
|
|
|
|
func (j *Jobs) PrefixList(prefix string) ([]*JobListStub, *QueryMeta, error) {
|
|
|
|
return j.List(&QueryOptions{Prefix: prefix})
|
|
|
|
}
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
// Info is used to retrieve information about a particular
|
2015-09-09 00:20:52 +00:00
|
|
|
// job given its unique ID.
|
2015-09-09 20:18:50 +00:00
|
|
|
func (j *Jobs) Info(jobID string, q *QueryOptions) (*Job, *QueryMeta, error) {
|
2015-09-09 00:20:52 +00:00
|
|
|
var resp Job
|
2019-11-07 13:35:39 +00:00
|
|
|
qm, err := j.client.query("/v1/job/"+url.PathEscape(jobID), &resp, q)
|
2015-09-09 00:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:15:49 +00:00
|
|
|
// Scale is used to retrieve information about a particular
|
|
|
|
// job given its unique ID.
|
2020-04-01 16:14:54 +00:00
|
|
|
func (j *Jobs) Scale(jobID, group string, count *int, message string, error bool, meta map[string]interface{},
|
|
|
|
q *WriteOptions) (*JobRegisterResponse, *WriteMeta, error) {
|
|
|
|
|
2020-03-22 21:49:09 +00:00
|
|
|
var count64 *int64
|
|
|
|
if count != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
count64 = pointerOf(int64(*count))
|
2020-03-22 21:49:09 +00:00
|
|
|
}
|
2020-01-25 01:15:49 +00:00
|
|
|
req := &ScalingRequest{
|
2020-03-22 21:49:09 +00:00
|
|
|
Count: count64,
|
2020-03-18 14:32:59 +00:00
|
|
|
Target: map[string]string{
|
|
|
|
"Job": jobID,
|
|
|
|
"Group": group,
|
|
|
|
},
|
2020-03-23 13:38:18 +00:00
|
|
|
Error: error,
|
|
|
|
Message: message,
|
|
|
|
Meta: meta,
|
2020-01-25 01:15:49 +00:00
|
|
|
}
|
|
|
|
var resp JobRegisterResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
qm, err := j.client.put(fmt.Sprintf("/v1/job/%s/scale", url.PathEscape(jobID)), req, &resp, q)
|
2020-01-25 01:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:14:28 +00:00
|
|
|
// ScaleStatus is used to retrieve information about a particular
|
|
|
|
// job given its unique ID.
|
2020-03-20 22:00:31 +00:00
|
|
|
func (j *Jobs) ScaleStatus(jobID string, q *QueryOptions) (*JobScaleStatusResponse, *QueryMeta, error) {
|
|
|
|
var resp JobScaleStatusResponse
|
2020-03-18 14:32:59 +00:00
|
|
|
qm, err := j.client.query(fmt.Sprintf("/v1/job/%s/scale", url.PathEscape(jobID)), &resp, q)
|
2020-01-27 22:14:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:16:20 +00:00
|
|
|
// Versions is used to retrieve all versions of a particular job given its
|
|
|
|
// unique ID.
|
|
|
|
func (j *Jobs) Versions(jobID string, diffs bool, q *QueryOptions) ([]*Job, []*JobDiff, *QueryMeta, error) {
|
|
|
|
var resp JobVersionsResponse
|
2019-11-07 13:35:39 +00:00
|
|
|
qm, err := j.client.query(fmt.Sprintf("/v1/job/%s/versions?diffs=%v", url.PathEscape(jobID), diffs), &resp, q)
|
2017-04-13 23:55:21 +00:00
|
|
|
if err != nil {
|
2017-06-30 00:16:20 +00:00
|
|
|
return nil, nil, nil, err
|
2017-04-13 23:55:21 +00:00
|
|
|
}
|
2017-06-30 00:16:20 +00:00
|
|
|
return resp.Versions, resp.Diffs, qm, nil
|
2017-04-13 23:55:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// Submission is used to retrieve the original submitted source of a job given its
|
|
|
|
// namespace, jobID, and version number. The original source might not be available,
|
|
|
|
// which case nil is returned with no error.
|
|
|
|
func (j *Jobs) Submission(jobID string, version int, q *QueryOptions) (*JobSubmission, *QueryMeta, error) {
|
|
|
|
var sub JobSubmission
|
|
|
|
s := fmt.Sprintf("/v1/job/%s/submission?version=%d", url.PathEscape(jobID), version)
|
|
|
|
qm, err := j.client.query(s, &sub, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &sub, qm, nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
// Allocations is used to return the allocs for a given job ID.
|
2016-11-24 12:20:52 +00:00
|
|
|
func (j *Jobs) Allocations(jobID string, allAllocs bool, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
|
2015-09-14 02:55:47 +00:00
|
|
|
var resp []*AllocationListStub
|
2019-11-07 13:35:39 +00:00
|
|
|
u, err := url.Parse("/v1/job/" + url.PathEscape(jobID) + "/allocations")
|
2016-11-24 12:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := u.Query()
|
|
|
|
v.Add("all", strconv.FormatBool(allAllocs))
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
|
|
|
|
qm, err := j.client.query(u.String(), &resp, q)
|
2015-09-09 00:49:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
sort.Sort(AllocIndexSort(resp))
|
2015-09-09 00:49:31 +00:00
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:23:34 +00:00
|
|
|
// Deployments is used to query the deployments associated with the given job
|
|
|
|
// ID.
|
2019-05-10 22:15:27 +00:00
|
|
|
func (j *Jobs) Deployments(jobID string, all bool, q *QueryOptions) ([]*Deployment, *QueryMeta, error) {
|
2017-07-01 00:23:34 +00:00
|
|
|
var resp []*Deployment
|
2019-11-07 13:35:39 +00:00
|
|
|
u, err := url.Parse("/v1/job/" + url.PathEscape(jobID) + "/deployments")
|
2019-05-10 22:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := u.Query()
|
|
|
|
v.Add("all", strconv.FormatBool(all))
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
qm, err := j.client.query(u.String(), &resp, q)
|
2017-07-01 00:23:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
sort.Sort(DeploymentIndexSort(resp))
|
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LatestDeployment is used to query for the latest deployment associated with
|
2015-09-09 01:42:34 +00:00
|
|
|
// the given job ID.
|
2017-07-01 00:23:34 +00:00
|
|
|
func (j *Jobs) LatestDeployment(jobID string, q *QueryOptions) (*Deployment, *QueryMeta, error) {
|
|
|
|
var resp *Deployment
|
2019-11-07 13:35:39 +00:00
|
|
|
qm, err := j.client.query("/v1/job/"+url.PathEscape(jobID)+"/deployment", &resp, q)
|
2017-07-01 00:23:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluations is used to query the evaluations associated with the given job
|
|
|
|
// ID.
|
2015-09-09 20:18:50 +00:00
|
|
|
func (j *Jobs) Evaluations(jobID string, q *QueryOptions) ([]*Evaluation, *QueryMeta, error) {
|
2015-09-09 01:42:34 +00:00
|
|
|
var resp []*Evaluation
|
2019-11-07 13:35:39 +00:00
|
|
|
qm, err := j.client.query("/v1/job/"+url.PathEscape(jobID)+"/evaluations", &resp, q)
|
2015-09-09 01:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
sort.Sort(EvalIndexSort(resp))
|
2015-09-09 01:42:34 +00:00
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2017-04-15 03:54:30 +00:00
|
|
|
// Deregister is used to remove an existing job. If purge is set to true, the job
|
|
|
|
// is deregistered and purged from the system versus still being queryable and
|
|
|
|
// eventually GC'ed from the system. Most callers should not specify purge.
|
|
|
|
func (j *Jobs) Deregister(jobID string, purge bool, q *WriteOptions) (string, *WriteMeta, error) {
|
2017-04-19 21:57:28 +00:00
|
|
|
var resp JobDeregisterResponse
|
2022-07-06 14:30:11 +00:00
|
|
|
wm, err := j.client.delete(fmt.Sprintf("/v1/job/%v?purge=%t", url.PathEscape(jobID), purge), nil, &resp, q)
|
2015-09-09 01:42:34 +00:00
|
|
|
if err != nil {
|
2015-09-17 00:12:48 +00:00
|
|
|
return "", nil, err
|
2020-08-28 18:28:13 +00:00
|
|
|
}
|
|
|
|
return resp.EvalID, wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterOptions is used to pass through job deregistration parameters
|
|
|
|
type DeregisterOptions struct {
|
|
|
|
// If Purge is set to true, the job is deregistered and purged from the
|
|
|
|
// system versus still being queryable and eventually GC'ed from the
|
|
|
|
// system. Most callers should not specify purge.
|
|
|
|
Purge bool
|
|
|
|
|
|
|
|
// If Global is set to true, all regions of a multiregion job will be
|
|
|
|
// stopped.
|
|
|
|
Global bool
|
2021-11-23 08:23:31 +00:00
|
|
|
|
|
|
|
// EvalPriority is an optional priority to use on any evaluation created as
|
|
|
|
// a result on this job deregistration. This value must be between 1-100
|
|
|
|
// inclusively, where a larger value corresponds to a higher priority. This
|
|
|
|
// is useful when an operator wishes to push through a job deregistration
|
|
|
|
// in busy clusters with a large evaluation backlog.
|
|
|
|
EvalPriority int
|
2021-12-13 19:54:53 +00:00
|
|
|
|
|
|
|
// NoShutdownDelay, if set to true, will override the group and
|
|
|
|
// task shutdown_delay configuration and ignore the delay for any
|
|
|
|
// allocations stopped as a result of this Deregister call.
|
|
|
|
NoShutdownDelay bool
|
2020-08-28 18:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterOpts is used to remove an existing job. See DeregisterOptions
|
|
|
|
// for parameters.
|
|
|
|
func (j *Jobs) DeregisterOpts(jobID string, opts *DeregisterOptions, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
var resp JobDeregisterResponse
|
2021-11-23 08:23:31 +00:00
|
|
|
|
|
|
|
// The base endpoint to add query params to.
|
|
|
|
endpoint := "/v1/job/" + url.PathEscape(jobID)
|
|
|
|
|
|
|
|
// Protect against nil opts. url.Values expects a string, and so using
|
|
|
|
// fmt.Sprintf is the best way to do this.
|
|
|
|
if opts != nil {
|
2021-12-13 19:54:53 +00:00
|
|
|
endpoint += fmt.Sprintf("?purge=%t&global=%t&eval_priority=%v&no_shutdown_delay=%t",
|
|
|
|
opts.Purge, opts.Global, opts.EvalPriority, opts.NoShutdownDelay)
|
2021-11-23 08:23:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:30:11 +00:00
|
|
|
wm, err := j.client.delete(endpoint, nil, &resp, q)
|
2020-08-28 18:28:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2015-09-09 01:42:34 +00:00
|
|
|
}
|
2015-09-17 00:12:48 +00:00
|
|
|
return resp.EvalID, wm, nil
|
2015-09-09 01:42:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:39:24 +00:00
|
|
|
// ForceEvaluate is used to force-evaluate an existing job.
|
2018-05-11 19:18:53 +00:00
|
|
|
func (j *Jobs) ForceEvaluate(jobID string, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
var resp JobRegisterResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/evaluate", nil, &resp, q)
|
2018-05-11 19:18:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return resp.EvalID, wm, nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 22:20:59 +00:00
|
|
|
// EvaluateWithOpts is used to force-evaluate an existing job and takes additional options
|
|
|
|
// for whether to force reschedule failed allocations
|
2018-05-11 19:18:53 +00:00
|
|
|
func (j *Jobs) EvaluateWithOpts(jobID string, opts EvalOptions, q *WriteOptions) (string, *WriteMeta, error) {
|
2018-05-09 20:04:27 +00:00
|
|
|
req := &JobEvaluateRequest{
|
|
|
|
JobID: jobID,
|
|
|
|
EvalOptions: opts,
|
|
|
|
}
|
|
|
|
|
2017-04-19 21:57:28 +00:00
|
|
|
var resp JobRegisterResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/evaluate", req, &resp, q)
|
2015-09-10 01:39:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return resp.EvalID, wm, nil
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:09:36 +00:00
|
|
|
// PeriodicForce spawns a new instance of the periodic job and returns the eval ID
|
|
|
|
func (j *Jobs) PeriodicForce(jobID string, q *WriteOptions) (string, *WriteMeta, error) {
|
|
|
|
var resp periodicForceResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/periodic/force", nil, &resp, q)
|
2016-01-19 19:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return resp.EvalID, wm, nil
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// PlanOptions is used to pass through job planning parameters
|
|
|
|
type PlanOptions struct {
|
|
|
|
Diff bool
|
|
|
|
PolicyOverride bool
|
|
|
|
}
|
|
|
|
|
2016-05-12 01:51:48 +00:00
|
|
|
func (j *Jobs) Plan(job *Job, diff bool, q *WriteOptions) (*JobPlanResponse, *WriteMeta, error) {
|
2017-09-19 14:47:10 +00:00
|
|
|
opts := PlanOptions{Diff: diff}
|
|
|
|
return j.PlanOpts(job, &opts, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Jobs) PlanOpts(job *Job, opts *PlanOptions, q *WriteOptions) (*JobPlanResponse, *WriteMeta, error) {
|
2016-05-12 01:51:48 +00:00
|
|
|
if job == nil {
|
2022-08-05 15:05:47 +00:00
|
|
|
return nil, nil, errors.New("must pass non-nil job")
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// Setup the request
|
2016-05-12 01:51:48 +00:00
|
|
|
req := &JobPlanRequest{
|
2017-09-19 14:47:10 +00:00
|
|
|
Job: job,
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
if opts != nil {
|
|
|
|
req.Diff = opts.Diff
|
|
|
|
req.PolicyOverride = opts.PolicyOverride
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp JobPlanResponse
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(*job.ID)+"/plan", req, &resp, q)
|
2016-05-12 01:51:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, wm, nil
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:34:19 +00:00
|
|
|
func (j *Jobs) Summary(jobID string, q *QueryOptions) (*JobSummary, *QueryMeta, error) {
|
|
|
|
var resp JobSummary
|
2019-11-07 13:35:39 +00:00
|
|
|
qm, err := j.client.query("/v1/job/"+url.PathEscape(jobID)+"/summary", &resp, q)
|
2016-07-21 20:34:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:22:13 +00:00
|
|
|
func (j *Jobs) Dispatch(jobID string, meta map[string]string,
|
2022-10-06 20:21:40 +00:00
|
|
|
payload []byte, idPrefixTemplate string, q *WriteOptions) (*JobDispatchResponse, *WriteMeta, error) {
|
2016-12-05 05:22:13 +00:00
|
|
|
var resp JobDispatchResponse
|
|
|
|
req := &JobDispatchRequest{
|
2022-10-06 20:21:40 +00:00
|
|
|
JobID: jobID,
|
|
|
|
Meta: meta,
|
|
|
|
Payload: payload,
|
|
|
|
IdPrefixTemplate: idPrefixTemplate,
|
2016-12-05 05:22:13 +00:00
|
|
|
}
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/dispatch", req, &resp, q)
|
2016-12-05 05:22:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, wm, nil
|
|
|
|
}
|
|
|
|
|
2017-04-19 21:57:28 +00:00
|
|
|
// Revert is used to revert the given job to the passed version. If
|
|
|
|
// enforceVersion is set, the job is only reverted if the current version is at
|
|
|
|
// the passed version.
|
|
|
|
func (j *Jobs) Revert(jobID string, version uint64, enforcePriorVersion *uint64,
|
2019-11-18 21:05:06 +00:00
|
|
|
q *WriteOptions, consulToken, vaultToken string) (*JobRegisterResponse, *WriteMeta, error) {
|
2017-04-19 21:57:28 +00:00
|
|
|
|
|
|
|
var resp JobRegisterResponse
|
|
|
|
req := &JobRevertRequest{
|
|
|
|
JobID: jobID,
|
|
|
|
JobVersion: version,
|
|
|
|
EnforcePriorVersion: enforcePriorVersion,
|
2022-05-19 15:30:29 +00:00
|
|
|
ConsulToken: consulToken,
|
|
|
|
VaultToken: vaultToken,
|
2017-04-19 21:57:28 +00:00
|
|
|
}
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/revert", req, &resp, q)
|
2017-04-19 21:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, wm, nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:49:13 +00:00
|
|
|
// Stable is used to mark a job version's stability.
|
|
|
|
func (j *Jobs) Stable(jobID string, version uint64, stable bool,
|
|
|
|
q *WriteOptions) (*JobStabilityResponse, *WriteMeta, error) {
|
|
|
|
|
|
|
|
var resp JobStabilityResponse
|
|
|
|
req := &JobStabilityRequest{
|
|
|
|
JobID: jobID,
|
|
|
|
JobVersion: version,
|
|
|
|
Stable: stable,
|
|
|
|
}
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := j.client.put("/v1/job/"+url.PathEscape(jobID)+"/stable", req, &resp, q)
|
2017-07-06 19:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, wm, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:14:00 +00:00
|
|
|
// Services is used to return a list of service registrations associated to the
|
|
|
|
// specified jobID.
|
|
|
|
func (j *Jobs) Services(jobID string, q *QueryOptions) ([]*ServiceRegistration, *QueryMeta, error) {
|
|
|
|
var resp []*ServiceRegistration
|
|
|
|
qm, err := j.client.query("/v1/job/"+jobID+"/services", &resp, q)
|
|
|
|
return resp, qm, err
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:09:36 +00:00
|
|
|
// periodicForceResponse is used to deserialize a force response
|
|
|
|
type periodicForceResponse struct {
|
|
|
|
EvalID string
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
// UpdateStrategy defines a task groups update strategy.
|
2015-10-20 23:16:05 +00:00
|
|
|
type UpdateStrategy struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
Stagger *time.Duration `mapstructure:"stagger" hcl:"stagger,optional"`
|
|
|
|
MaxParallel *int `mapstructure:"max_parallel" hcl:"max_parallel,optional"`
|
|
|
|
HealthCheck *string `mapstructure:"health_check" hcl:"health_check,optional"`
|
|
|
|
MinHealthyTime *time.Duration `mapstructure:"min_healthy_time" hcl:"min_healthy_time,optional"`
|
|
|
|
HealthyDeadline *time.Duration `mapstructure:"healthy_deadline" hcl:"healthy_deadline,optional"`
|
|
|
|
ProgressDeadline *time.Duration `mapstructure:"progress_deadline" hcl:"progress_deadline,optional"`
|
|
|
|
Canary *int `mapstructure:"canary" hcl:"canary,optional"`
|
|
|
|
AutoRevert *bool `mapstructure:"auto_revert" hcl:"auto_revert,optional"`
|
|
|
|
AutoPromote *bool `mapstructure:"auto_promote" hcl:"auto_promote,optional"`
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 16:58:48 +00:00
|
|
|
// DefaultUpdateStrategy provides a baseline that can be used to upgrade
|
|
|
|
// jobs with the old policy or for populating field defaults.
|
|
|
|
func DefaultUpdateStrategy() *UpdateStrategy {
|
|
|
|
return &UpdateStrategy{
|
2022-08-17 16:26:34 +00:00
|
|
|
Stagger: pointerOf(30 * time.Second),
|
|
|
|
MaxParallel: pointerOf(1),
|
|
|
|
HealthCheck: pointerOf("checks"),
|
|
|
|
MinHealthyTime: pointerOf(10 * time.Second),
|
|
|
|
HealthyDeadline: pointerOf(5 * time.Minute),
|
|
|
|
ProgressDeadline: pointerOf(10 * time.Minute),
|
|
|
|
AutoRevert: pointerOf(false),
|
|
|
|
Canary: pointerOf(0),
|
|
|
|
AutoPromote: pointerOf(false),
|
2017-09-29 16:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
func (u *UpdateStrategy) Copy() *UpdateStrategy {
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
copy := new(UpdateStrategy)
|
|
|
|
|
2017-07-07 02:08:51 +00:00
|
|
|
if u.Stagger != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.Stagger = pointerOf(*u.Stagger)
|
2017-07-07 02:08:51 +00:00
|
|
|
}
|
2017-05-09 00:44:26 +00:00
|
|
|
|
|
|
|
if u.MaxParallel != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.MaxParallel = pointerOf(*u.MaxParallel)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.HealthCheck != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.HealthCheck = pointerOf(*u.HealthCheck)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.MinHealthyTime != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.MinHealthyTime = pointerOf(*u.MinHealthyTime)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.HealthyDeadline != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.HealthyDeadline = pointerOf(*u.HealthyDeadline)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 17:56:00 +00:00
|
|
|
if u.ProgressDeadline != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.ProgressDeadline = pointerOf(*u.ProgressDeadline)
|
2018-03-23 17:56:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if u.AutoRevert != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.AutoRevert = pointerOf(*u.AutoRevert)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Canary != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.Canary = pointerOf(*u.Canary)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 13:42:18 +00:00
|
|
|
if u.AutoPromote != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.AutoPromote = pointerOf(*u.AutoPromote)
|
2019-05-09 13:42:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UpdateStrategy) Merge(o *UpdateStrategy) {
|
|
|
|
if o == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-07 02:08:51 +00:00
|
|
|
if o.Stagger != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.Stagger = pointerOf(*o.Stagger)
|
2017-07-07 02:08:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if o.MaxParallel != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.MaxParallel = pointerOf(*o.MaxParallel)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if o.HealthCheck != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.HealthCheck = pointerOf(*o.HealthCheck)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if o.MinHealthyTime != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.MinHealthyTime = pointerOf(*o.MinHealthyTime)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if o.HealthyDeadline != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.HealthyDeadline = pointerOf(*o.HealthyDeadline)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 17:56:00 +00:00
|
|
|
if o.ProgressDeadline != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.ProgressDeadline = pointerOf(*o.ProgressDeadline)
|
2018-03-23 17:56:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if o.AutoRevert != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.AutoRevert = pointerOf(*o.AutoRevert)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if o.Canary != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.Canary = pointerOf(*o.Canary)
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
2019-05-09 13:42:18 +00:00
|
|
|
|
|
|
|
if o.AutoPromote != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
u.AutoPromote = pointerOf(*o.AutoPromote)
|
2019-05-09 13:42:18 +00:00
|
|
|
}
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UpdateStrategy) Canonicalize() {
|
2017-09-29 16:58:48 +00:00
|
|
|
d := DefaultUpdateStrategy()
|
2017-08-23 13:30:28 +00:00
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if u.MaxParallel == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.MaxParallel = d.MaxParallel
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 02:08:51 +00:00
|
|
|
if u.Stagger == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.Stagger = d.Stagger
|
2017-07-07 02:08:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if u.HealthCheck == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.HealthCheck = d.HealthCheck
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.HealthyDeadline == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.HealthyDeadline = d.HealthyDeadline
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 17:56:00 +00:00
|
|
|
if u.ProgressDeadline == nil {
|
|
|
|
u.ProgressDeadline = d.ProgressDeadline
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:44:26 +00:00
|
|
|
if u.MinHealthyTime == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.MinHealthyTime = d.MinHealthyTime
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.AutoRevert == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.AutoRevert = d.AutoRevert
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.Canary == nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
u.Canary = d.Canary
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
2019-07-11 21:01:32 +00:00
|
|
|
|
|
|
|
if u.AutoPromote == nil {
|
|
|
|
u.AutoPromote = d.AutoPromote
|
|
|
|
}
|
2015-10-20 23:16:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 18:35:19 +00:00
|
|
|
// Empty returns whether the UpdateStrategy is empty or has user defined values.
|
|
|
|
func (u *UpdateStrategy) Empty() bool {
|
|
|
|
if u == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Stagger != nil && *u.Stagger != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.MaxParallel != nil && *u.MaxParallel != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.HealthCheck != nil && *u.HealthCheck != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.MinHealthyTime != nil && *u.MinHealthyTime != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.HealthyDeadline != nil && *u.HealthyDeadline != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:56:00 +00:00
|
|
|
if u.ProgressDeadline != nil && *u.ProgressDeadline != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:35:19 +00:00
|
|
|
if u.AutoRevert != nil && *u.AutoRevert {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:01:32 +00:00
|
|
|
if u.AutoPromote != nil && *u.AutoPromote {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:35:19 +00:00
|
|
|
if u.Canary != nil && *u.Canary != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-09 17:56:58 +00:00
|
|
|
type Multiregion struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
Strategy *MultiregionStrategy `hcl:"strategy,block"`
|
|
|
|
Regions []*MultiregionRegion `hcl:"region,block"`
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multiregion) Canonicalize() {
|
|
|
|
if m.Strategy == nil {
|
|
|
|
m.Strategy = &MultiregionStrategy{
|
2022-08-17 16:26:34 +00:00
|
|
|
MaxParallel: pointerOf(0),
|
|
|
|
OnFailure: pointerOf(""),
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if m.Strategy.MaxParallel == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
m.Strategy.MaxParallel = pointerOf(0)
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
2020-06-16 19:17:53 +00:00
|
|
|
if m.Strategy.OnFailure == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
m.Strategy.OnFailure = pointerOf("")
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.Regions == nil {
|
|
|
|
m.Regions = []*MultiregionRegion{}
|
|
|
|
}
|
|
|
|
for _, region := range m.Regions {
|
|
|
|
if region.Count == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
region.Count = pointerOf(1)
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
if region.Datacenters == nil {
|
|
|
|
region.Datacenters = []string{}
|
|
|
|
}
|
|
|
|
if region.Meta == nil {
|
|
|
|
region.Meta = map[string]string{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multiregion) Copy() *Multiregion {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
copy := new(Multiregion)
|
|
|
|
if m.Strategy != nil {
|
|
|
|
copy.Strategy = new(MultiregionStrategy)
|
2022-08-17 16:26:34 +00:00
|
|
|
copy.Strategy.MaxParallel = pointerOf(*m.Strategy.MaxParallel)
|
|
|
|
copy.Strategy.OnFailure = pointerOf(*m.Strategy.OnFailure)
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
for _, region := range m.Regions {
|
|
|
|
copyRegion := new(MultiregionRegion)
|
|
|
|
copyRegion.Name = region.Name
|
2022-08-17 16:26:34 +00:00
|
|
|
copyRegion.Count = pointerOf(*region.Count)
|
2021-04-03 07:50:23 +00:00
|
|
|
copyRegion.Datacenters = append(copyRegion.Datacenters, region.Datacenters...)
|
2023-06-01 20:08:55 +00:00
|
|
|
copyRegion.NodePool = region.NodePool
|
2020-06-09 17:56:58 +00:00
|
|
|
for k, v := range region.Meta {
|
|
|
|
copyRegion.Meta[k] = v
|
|
|
|
}
|
2023-06-01 20:08:55 +00:00
|
|
|
|
2020-06-09 17:56:58 +00:00
|
|
|
copy.Regions = append(copy.Regions, copyRegion)
|
|
|
|
}
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiregionStrategy struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
MaxParallel *int `mapstructure:"max_parallel" hcl:"max_parallel,optional"`
|
|
|
|
OnFailure *string `mapstructure:"on_failure" hcl:"on_failure,optional"`
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MultiregionRegion struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
Name string `hcl:",label"`
|
|
|
|
Count *int `hcl:"count,optional"`
|
|
|
|
Datacenters []string `hcl:"datacenters,optional"`
|
2023-06-01 20:08:55 +00:00
|
|
|
NodePool string `hcl:"node_pool,optional"`
|
2020-10-21 13:58:45 +00:00
|
|
|
Meta map[string]string `hcl:"meta,block"`
|
2020-06-09 17:56:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 23:10:08 +00:00
|
|
|
// PeriodicConfig is for serializing periodic config for a job.
|
|
|
|
type PeriodicConfig struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
Enabled *bool `hcl:"enabled,optional"`
|
|
|
|
Spec *string `hcl:"cron,optional"`
|
2017-02-06 19:48:28 +00:00
|
|
|
SpecType *string
|
2020-10-21 13:58:45 +00:00
|
|
|
ProhibitOverlap *bool `mapstructure:"prohibit_overlap" hcl:"prohibit_overlap,optional"`
|
|
|
|
TimeZone *string `mapstructure:"time_zone" hcl:"time_zone,optional"`
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeriodicConfig) Canonicalize() {
|
|
|
|
if p.Enabled == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
p.Enabled = pointerOf(true)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2017-02-24 22:18:09 +00:00
|
|
|
if p.Spec == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
p.Spec = pointerOf("")
|
2017-02-24 22:18:09 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if p.SpecType == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
p.SpecType = pointerOf(PeriodicSpecCron)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if p.ProhibitOverlap == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
p.ProhibitOverlap = pointerOf(false)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2017-02-20 23:13:10 +00:00
|
|
|
if p.TimeZone == nil || *p.TimeZone == "" {
|
2022-08-17 16:26:34 +00:00
|
|
|
p.TimeZone = pointerOf("UTC")
|
2017-02-20 23:13:10 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the closest time instant matching the spec that is after the
|
|
|
|
// passed time. If no matching instance exists, the zero value of time.Time is
|
|
|
|
// returned. The `time.Location` of the returned value matches that of the
|
|
|
|
// passed time.
|
2018-04-26 22:46:22 +00:00
|
|
|
func (p *PeriodicConfig) Next(fromTime time.Time) (time.Time, error) {
|
2023-02-01 21:01:28 +00:00
|
|
|
if p != nil && *p.SpecType == PeriodicSpecCron {
|
2020-05-07 22:33:48 +00:00
|
|
|
e, err := cronexpr.Parse(*p.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("failed parsing cron expression %q: %v", *p.Spec, err)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2020-05-07 22:33:48 +00:00
|
|
|
return cronParseNext(e, fromTime, *p.Spec)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 22:46:22 +00:00
|
|
|
return time.Time{}, nil
|
2015-12-04 23:10:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:36:16 +00:00
|
|
|
// cronParseNext is a helper that parses the next time for the given expression
|
|
|
|
// but captures any panic that may occur in the underlying library.
|
|
|
|
// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
|
|
|
|
// and should be kept in sync.
|
|
|
|
func cronParseNext(e *cronexpr.Expression, fromTime time.Time, spec string) (t time.Time, err error) {
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
t = time.Time{}
|
|
|
|
err = fmt.Errorf("failed parsing cron expression: %q", spec)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return e.Next(fromTime), nil
|
|
|
|
}
|
2020-05-07 22:33:48 +00:00
|
|
|
|
2017-02-15 22:37:06 +00:00
|
|
|
func (p *PeriodicConfig) GetLocation() (*time.Location, error) {
|
|
|
|
if p.TimeZone == nil || *p.TimeZone == "" {
|
|
|
|
return time.UTC, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.LoadLocation(*p.TimeZone)
|
2015-12-04 23:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 18:33:52 +00:00
|
|
|
// ParameterizedJobConfig is used to configure the parameterized job.
|
|
|
|
type ParameterizedJobConfig struct {
|
2020-10-21 13:58:45 +00:00
|
|
|
Payload string `hcl:"payload,optional"`
|
|
|
|
MetaRequired []string `mapstructure:"meta_required" hcl:"meta_required,optional"`
|
|
|
|
MetaOptional []string `mapstructure:"meta_optional" hcl:"meta_optional,optional"`
|
2016-11-23 22:56:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// JobSubmission is used to hold information about the original content of a job
|
|
|
|
// specification being submitted to Nomad.
|
|
|
|
//
|
|
|
|
// At any time a JobSubmission may be nil, indicating no information is known about
|
|
|
|
// the job submission.
|
|
|
|
type JobSubmission struct {
|
|
|
|
// Source contains the original job definition (may be in the format of
|
|
|
|
// hcl1, hcl2, or json).
|
|
|
|
Source string
|
|
|
|
|
|
|
|
// Format indicates what the Source content was (hcl1, hcl2, or json).
|
|
|
|
Format string
|
|
|
|
|
|
|
|
// VariableFlags contains the CLI "-var" flag arguments as submitted with the
|
|
|
|
// job (hcl2 only).
|
|
|
|
VariableFlags map[string]string
|
|
|
|
|
|
|
|
// Variables contains the opaque variables configuration as coming from
|
|
|
|
// a var-file or the WebUI variables input (hcl2 only).
|
|
|
|
Variables string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *JobSubmission) Canonicalize() {
|
|
|
|
if js == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(js.VariableFlags) == 0 {
|
|
|
|
js.VariableFlags = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *JobSubmission) Copy() *JobSubmission {
|
|
|
|
if js == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &JobSubmission{
|
|
|
|
Source: js.Source,
|
|
|
|
Format: js.Format,
|
|
|
|
VariableFlags: maps.Clone(js.VariableFlags),
|
|
|
|
Variables: js.Variables,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:24:26 +00:00
|
|
|
// Job is used to serialize a job.
|
|
|
|
type Job struct {
|
2020-10-22 11:19:12 +00:00
|
|
|
/* Fields parsed from HCL config */
|
|
|
|
|
|
|
|
Region *string `hcl:"region,optional"`
|
|
|
|
Namespace *string `hcl:"namespace,optional"`
|
|
|
|
ID *string `hcl:"id,optional"`
|
|
|
|
Name *string `hcl:"name,optional"`
|
|
|
|
Type *string `hcl:"type,optional"`
|
|
|
|
Priority *int `hcl:"priority,optional"`
|
|
|
|
AllAtOnce *bool `mapstructure:"all_at_once" hcl:"all_at_once,optional"`
|
|
|
|
Datacenters []string `hcl:"datacenters,optional"`
|
2023-06-01 20:08:55 +00:00
|
|
|
NodePool *string `hcl:"node_pool,optional"`
|
2020-10-22 11:19:12 +00:00
|
|
|
Constraints []*Constraint `hcl:"constraint,block"`
|
|
|
|
Affinities []*Affinity `hcl:"affinity,block"`
|
|
|
|
TaskGroups []*TaskGroup `hcl:"group,block"`
|
|
|
|
Update *UpdateStrategy `hcl:"update,block"`
|
|
|
|
Multiregion *Multiregion `hcl:"multiregion,block"`
|
|
|
|
Spreads []*Spread `hcl:"spread,block"`
|
|
|
|
Periodic *PeriodicConfig `hcl:"periodic,block"`
|
|
|
|
ParameterizedJob *ParameterizedJobConfig `hcl:"parameterized,block"`
|
|
|
|
Reschedule *ReschedulePolicy `hcl:"reschedule,block"`
|
|
|
|
Migrate *MigrateStrategy `hcl:"migrate,block"`
|
|
|
|
Meta map[string]string `hcl:"meta,block"`
|
|
|
|
ConsulToken *string `mapstructure:"consul_token" hcl:"consul_token,optional"`
|
|
|
|
VaultToken *string `mapstructure:"vault_token" hcl:"vault_token,optional"`
|
|
|
|
|
|
|
|
/* Fields set by server, not sourced from job config file */
|
|
|
|
|
2021-10-22 16:39:05 +00:00
|
|
|
Stop *bool
|
|
|
|
ParentID *string
|
|
|
|
Dispatched bool
|
|
|
|
DispatchIdempotencyToken *string
|
|
|
|
Payload []byte
|
|
|
|
ConsulNamespace *string `mapstructure:"consul_namespace"`
|
|
|
|
VaultNamespace *string `mapstructure:"vault_namespace"`
|
|
|
|
NomadTokenID *string `mapstructure:"nomad_token_id"`
|
|
|
|
Status *string
|
|
|
|
StatusDescription *string
|
|
|
|
Stable *bool
|
|
|
|
Version *uint64
|
|
|
|
SubmitTime *int64
|
|
|
|
CreateIndex *uint64
|
|
|
|
ModifyIndex *uint64
|
|
|
|
JobModifyIndex *uint64
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsPeriodic returns whether a job is periodic.
|
|
|
|
func (j *Job) IsPeriodic() bool {
|
|
|
|
return j.Periodic != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsParameterized returns whether a job is parameterized job.
|
|
|
|
func (j *Job) IsParameterized() bool {
|
2018-06-11 15:59:03 +00:00
|
|
|
return j.ParameterizedJob != nil && !j.Dispatched
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 20:18:14 +00:00
|
|
|
// IsMultiregion returns whether a job is a multiregion job
|
|
|
|
func (j *Job) IsMultiregion() bool {
|
|
|
|
return j.Multiregion != nil && j.Multiregion.Regions != nil && len(j.Multiregion.Regions) > 0
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
func (j *Job) Canonicalize() {
|
|
|
|
if j.ID == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.ID = pointerOf("")
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.Name == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Name = pointerOf(*j.ID)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2017-02-13 23:18:17 +00:00
|
|
|
if j.ParentID == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.ParentID = pointerOf("")
|
2017-02-13 23:18:17 +00:00
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
if j.Namespace == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Namespace = pointerOf(DefaultNamespace)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.Priority == nil {
|
2023-05-30 07:05:32 +00:00
|
|
|
j.Priority = pointerOf(JobDefaultPriority)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2017-04-15 03:54:30 +00:00
|
|
|
if j.Stop == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Stop = pointerOf(false)
|
2017-04-15 03:54:30 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.Region == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Region = pointerOf(GlobalRegion)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2023-06-01 20:08:55 +00:00
|
|
|
if j.NodePool == nil {
|
|
|
|
j.NodePool = pointerOf(NodePoolDefault)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.Type == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Type = pointerOf("service")
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.AllAtOnce == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.AllAtOnce = pointerOf(false)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2019-11-18 21:05:06 +00:00
|
|
|
if j.ConsulToken == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.ConsulToken = pointerOf("")
|
2019-11-18 21:05:06 +00:00
|
|
|
}
|
2021-03-16 18:22:21 +00:00
|
|
|
if j.ConsulNamespace == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.ConsulNamespace = pointerOf("")
|
2021-03-16 18:22:21 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.VaultToken == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.VaultToken = pointerOf("")
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2020-07-17 14:41:45 +00:00
|
|
|
if j.VaultNamespace == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.VaultNamespace = pointerOf("")
|
2020-07-17 14:41:45 +00:00
|
|
|
}
|
2020-06-19 11:53:29 +00:00
|
|
|
if j.NomadTokenID == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.NomadTokenID = pointerOf("")
|
2020-06-19 11:53:29 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.Status == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Status = pointerOf("")
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.StatusDescription == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.StatusDescription = pointerOf("")
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2017-04-13 20:54:57 +00:00
|
|
|
if j.Stable == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Stable = pointerOf(false)
|
2017-04-13 20:54:57 +00:00
|
|
|
}
|
|
|
|
if j.Version == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.Version = pointerOf(uint64(0))
|
2017-04-13 20:54:57 +00:00
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
if j.CreateIndex == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.CreateIndex = pointerOf(uint64(0))
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.ModifyIndex == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.ModifyIndex = pointerOf(uint64(0))
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.JobModifyIndex == nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
j.JobModifyIndex = pointerOf(uint64(0))
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
if j.Periodic != nil {
|
|
|
|
j.Periodic.Canonicalize()
|
|
|
|
}
|
2017-05-09 00:44:26 +00:00
|
|
|
if j.Update != nil {
|
|
|
|
j.Update.Canonicalize()
|
2019-09-02 17:30:09 +00:00
|
|
|
} else if *j.Type == JobTypeService {
|
|
|
|
j.Update = DefaultUpdateStrategy()
|
2017-05-09 00:44:26 +00:00
|
|
|
}
|
2020-06-09 17:56:58 +00:00
|
|
|
if j.Multiregion != nil {
|
|
|
|
j.Multiregion.Canonicalize()
|
|
|
|
}
|
2017-02-06 19:48:28 +00:00
|
|
|
|
|
|
|
for _, tg := range j.TaskGroups {
|
2017-02-22 20:30:05 +00:00
|
|
|
tg.Canonicalize(j)
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
2019-01-11 15:48:12 +00:00
|
|
|
|
|
|
|
for _, spread := range j.Spreads {
|
|
|
|
spread.Canonicalize()
|
|
|
|
}
|
2019-01-30 20:20:38 +00:00
|
|
|
for _, a := range j.Affinities {
|
|
|
|
a.Canonicalize()
|
|
|
|
}
|
2015-09-14 02:55:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:34:29 +00:00
|
|
|
// LookupTaskGroup finds a task group by name
|
|
|
|
func (j *Job) LookupTaskGroup(name string) *TaskGroup {
|
|
|
|
for _, tg := range j.TaskGroups {
|
|
|
|
if *tg.Name == name {
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:34:19 +00:00
|
|
|
// JobSummary summarizes the state of the allocations of a job
|
|
|
|
type JobSummary struct {
|
2017-09-07 23:56:15 +00:00
|
|
|
JobID string
|
|
|
|
Namespace string
|
|
|
|
Summary map[string]TaskGroupSummary
|
|
|
|
Children *JobChildrenSummary
|
2016-07-21 20:34:19 +00:00
|
|
|
|
|
|
|
// Raft Indexes
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2016-12-06 01:24:37 +00:00
|
|
|
// JobChildrenSummary contains the summary of children job status
|
|
|
|
type JobChildrenSummary struct {
|
2016-12-15 00:58:54 +00:00
|
|
|
Pending int64
|
|
|
|
Running int64
|
|
|
|
Dead int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc *JobChildrenSummary) Sum() int {
|
|
|
|
if jc == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(jc.Pending + jc.Running + jc.Dead)
|
2016-12-06 01:24:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:34:19 +00:00
|
|
|
// TaskGroup summarizes the state of all the allocations of a particular
|
|
|
|
// TaskGroup
|
|
|
|
type TaskGroupSummary struct {
|
|
|
|
Queued int
|
|
|
|
Complete int
|
|
|
|
Failed int
|
|
|
|
Running int
|
|
|
|
Starting int
|
|
|
|
Lost int
|
2022-03-14 16:39:49 +00:00
|
|
|
Unknown int
|
2016-07-21 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 02:55:47 +00:00
|
|
|
// JobListStub is used to return a subset of information about
|
|
|
|
// jobs during list operations.
|
|
|
|
type JobListStub struct {
|
|
|
|
ID string
|
2016-01-07 22:43:55 +00:00
|
|
|
ParentID string
|
2015-09-14 02:55:47 +00:00
|
|
|
Name string
|
2020-05-19 13:58:19 +00:00
|
|
|
Namespace string `json:",omitempty"`
|
2019-01-22 18:59:13 +00:00
|
|
|
Datacenters []string
|
2015-09-14 02:55:47 +00:00
|
|
|
Type string
|
|
|
|
Priority int
|
2017-04-16 00:05:52 +00:00
|
|
|
Periodic bool
|
|
|
|
ParameterizedJob bool
|
2017-04-15 03:54:30 +00:00
|
|
|
Stop bool
|
2015-09-14 02:55:47 +00:00
|
|
|
Status string
|
|
|
|
StatusDescription string
|
2016-07-21 20:34:19 +00:00
|
|
|
JobSummary *JobSummary
|
2015-09-14 02:55:47 +00:00
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2016-06-08 23:48:02 +00:00
|
|
|
JobModifyIndex uint64
|
2017-06-30 02:08:25 +00:00
|
|
|
SubmitTime int64
|
2022-11-02 20:58:24 +00:00
|
|
|
Meta map[string]string `json:",omitempty"`
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:40:51 +00:00
|
|
|
// JobIDSort is used to sort jobs by their job ID's.
|
|
|
|
type JobIDSort []*JobListStub
|
|
|
|
|
|
|
|
func (j JobIDSort) Len() int {
|
|
|
|
return len(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JobIDSort) Less(a, b int) bool {
|
|
|
|
return j[a].ID < j[b].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JobIDSort) Swap(a, b int) {
|
|
|
|
j[a], j[b] = j[b], j[a]
|
|
|
|
}
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
// NewServiceJob creates and returns a new service-style job
|
|
|
|
// for long-lived processes using the provided name, ID, and
|
|
|
|
// relative job priority.
|
2015-09-16 18:42:08 +00:00
|
|
|
func NewServiceJob(id, name, region string, pri int) *Job {
|
|
|
|
return newJob(id, name, region, JobTypeService, pri)
|
2015-09-10 00:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBatchJob creates and returns a new batch-style job for
|
|
|
|
// short-lived processes using the provided name and ID along
|
|
|
|
// with the relative job priority.
|
2015-09-16 18:42:08 +00:00
|
|
|
func NewBatchJob(id, name, region string, pri int) *Job {
|
|
|
|
return newJob(id, name, region, JobTypeBatch, pri)
|
2015-09-10 00:29:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 09:03:20 +00:00
|
|
|
// NewSystemJob creates and returns a new system-style job for processes
|
|
|
|
// designed to run on all clients, using the provided name and ID along with
|
|
|
|
// the relative job priority.
|
|
|
|
func NewSystemJob(id, name, region string, pri int) *Job {
|
|
|
|
return newJob(id, name, region, JobTypeSystem, pri)
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:53:04 +00:00
|
|
|
// NewSysbatchJob creates and returns a new sysbatch-style job for short-lived
|
|
|
|
// processes designed to run on all clients, using the provided name and ID
|
|
|
|
// along with the relative job priority.
|
|
|
|
func NewSysbatchJob(id, name, region string, pri int) *Job {
|
|
|
|
return newJob(id, name, region, JobTypeSysbatch, pri)
|
|
|
|
}
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
// newJob is used to create a new Job struct.
|
2015-09-16 18:42:08 +00:00
|
|
|
func newJob(id, name, region, typ string, pri int) *Job {
|
2015-09-10 00:29:43 +00:00
|
|
|
return &Job{
|
2017-02-06 19:48:28 +00:00
|
|
|
Region: ®ion,
|
|
|
|
ID: &id,
|
|
|
|
Name: &name,
|
|
|
|
Type: &typ,
|
|
|
|
Priority: &pri,
|
2015-09-10 00:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMeta is used to set arbitrary k/v pairs of metadata on a job.
|
|
|
|
func (j *Job) SetMeta(key, val string) *Job {
|
|
|
|
if j.Meta == nil {
|
|
|
|
j.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
j.Meta[key] = val
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddDatacenter is used to add a datacenter to a job.
|
|
|
|
func (j *Job) AddDatacenter(dc string) *Job {
|
|
|
|
j.Datacenters = append(j.Datacenters, dc)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain is used to add a constraint to a job.
|
|
|
|
func (j *Job) Constrain(c *Constraint) *Job {
|
|
|
|
j.Constraints = append(j.Constraints, c)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2018-07-16 13:30:58 +00:00
|
|
|
// AddAffinity is used to add an affinity to a job.
|
|
|
|
func (j *Job) AddAffinity(a *Affinity) *Job {
|
|
|
|
j.Affinities = append(j.Affinities, a)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2015-09-10 00:29:43 +00:00
|
|
|
// AddTaskGroup adds a task group to an existing job.
|
|
|
|
func (j *Job) AddTaskGroup(grp *TaskGroup) *Job {
|
|
|
|
j.TaskGroups = append(j.TaskGroups, grp)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:19:53 +00:00
|
|
|
// AddPeriodicConfig adds a periodic config to an existing job.
|
|
|
|
func (j *Job) AddPeriodicConfig(cfg *PeriodicConfig) *Job {
|
|
|
|
j.Periodic = cfg
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:53:03 +00:00
|
|
|
func (j *Job) AddSpread(s *Spread) *Job {
|
|
|
|
j.Spreads = append(j.Spreads, s)
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
type WriteRequest struct {
|
|
|
|
// The target region for this write
|
|
|
|
Region string
|
2017-08-21 04:39:36 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
// Namespace is the target namespace for this write
|
|
|
|
Namespace string
|
|
|
|
|
2017-08-21 04:39:36 +00:00
|
|
|
// SecretID is the secret ID of an ACL token
|
|
|
|
SecretID string
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JobValidateRequest is used to validate a job
|
|
|
|
type JobValidateRequest struct {
|
|
|
|
Job *Job
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobValidateResponse is the response from validate request
|
|
|
|
type JobValidateResponse struct {
|
|
|
|
// DriverConfigValidated indicates whether the agent validated the driver
|
|
|
|
// config
|
|
|
|
DriverConfigValidated bool
|
|
|
|
|
|
|
|
// ValidationErrors is a list of validation errors
|
|
|
|
ValidationErrors []string
|
2017-03-03 23:00:39 +00:00
|
|
|
|
2017-08-07 21:13:05 +00:00
|
|
|
// Error is a string version of any error that may have occurred
|
2017-03-03 23:00:39 +00:00
|
|
|
Error string
|
2017-05-10 03:52:47 +00:00
|
|
|
|
|
|
|
// Warnings contains any warnings about the given job. These may include
|
|
|
|
// deprecation warnings.
|
|
|
|
Warnings string
|
2017-02-06 19:48:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 20:09:24 +00:00
|
|
|
// JobRevertRequest is used to revert a job to a prior version.
|
|
|
|
type JobRevertRequest struct {
|
|
|
|
// JobID is the ID of the job being reverted
|
|
|
|
JobID string
|
|
|
|
|
|
|
|
// JobVersion the version to revert to.
|
|
|
|
JobVersion uint64
|
|
|
|
|
|
|
|
// EnforcePriorVersion if set will enforce that the job is at the given
|
|
|
|
// version before reverting.
|
|
|
|
EnforcePriorVersion *uint64
|
|
|
|
|
2019-11-18 21:05:06 +00:00
|
|
|
// ConsulToken is the Consul token that proves the submitter of the job revert
|
|
|
|
// has access to the Service Identity policies associated with the job's
|
|
|
|
// Consul Connect enabled services. This field is only used to transfer the
|
|
|
|
// token and is not stored after the Job revert.
|
|
|
|
ConsulToken string `json:",omitempty"`
|
|
|
|
|
2019-03-27 20:24:32 +00:00
|
|
|
// VaultToken is the Vault token that proves the submitter of the job revert
|
|
|
|
// has access to any Vault policies specified in the targeted job version. This
|
2019-03-28 18:55:01 +00:00
|
|
|
// field is only used to authorize the revert and is not stored after the Job
|
2019-03-27 20:24:32 +00:00
|
|
|
// revert.
|
|
|
|
VaultToken string `json:",omitempty"`
|
|
|
|
|
2017-04-18 20:09:24 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:39:44 +00:00
|
|
|
// JobRegisterRequest is used to update a job
|
2017-02-16 01:40:51 +00:00
|
|
|
type JobRegisterRequest struct {
|
2023-04-11 13:45:08 +00:00
|
|
|
Submission *JobSubmission
|
|
|
|
Job *Job
|
|
|
|
|
2017-02-16 00:56:36 +00:00
|
|
|
// If EnforceIndex is set then the job will only be registered if the passed
|
|
|
|
// JobModifyIndex matches the current Jobs index. If the index is zero, the
|
|
|
|
// register only occurs if the job is new.
|
2016-07-28 21:48:25 +00:00
|
|
|
EnforceIndex bool `json:",omitempty"`
|
|
|
|
JobModifyIndex uint64 `json:",omitempty"`
|
2017-09-19 14:47:10 +00:00
|
|
|
PolicyOverride bool `json:",omitempty"`
|
2020-06-16 02:57:50 +00:00
|
|
|
PreserveCounts bool `json:",omitempty"`
|
2020-06-16 03:39:44 +00:00
|
|
|
|
2021-11-23 08:23:31 +00:00
|
|
|
// EvalPriority is an optional priority to use on any evaluation created as
|
|
|
|
// a result on this job registration. This value must be between 1-100
|
|
|
|
// inclusively, where a larger value corresponds to a higher priority. This
|
|
|
|
// is useful when an operator wishes to push through a job registration in
|
|
|
|
// busy clusters with a large evaluation backlog. This avoids needing to
|
|
|
|
// change the job priority which also impacts preemption.
|
|
|
|
EvalPriority int `json:",omitempty"`
|
|
|
|
|
2020-06-16 03:39:44 +00:00
|
|
|
WriteRequest
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 21:57:28 +00:00
|
|
|
// JobRegisterResponse is used to respond to a job registration
|
|
|
|
type JobRegisterResponse struct {
|
|
|
|
EvalID string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
JobModifyIndex uint64
|
2017-05-10 03:52:47 +00:00
|
|
|
|
|
|
|
// Warnings contains any warnings about the given job. These may include
|
|
|
|
// deprecation warnings.
|
|
|
|
Warnings string
|
|
|
|
|
2017-04-19 21:57:28 +00:00
|
|
|
QueryMeta
|
2015-09-08 23:24:26 +00:00
|
|
|
}
|
2015-09-17 00:12:48 +00:00
|
|
|
|
2017-04-19 21:57:28 +00:00
|
|
|
// JobDeregisterResponse is used to respond to a job deregistration
|
|
|
|
type JobDeregisterResponse struct {
|
|
|
|
EvalID string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
JobModifyIndex uint64
|
|
|
|
QueryMeta
|
2015-09-17 00:12:48 +00:00
|
|
|
}
|
2016-05-12 01:51:48 +00:00
|
|
|
|
|
|
|
type JobPlanRequest struct {
|
2017-09-19 14:47:10 +00:00
|
|
|
Job *Job
|
|
|
|
Diff bool
|
|
|
|
PolicyOverride bool
|
2017-02-16 01:40:51 +00:00
|
|
|
WriteRequest
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobPlanResponse struct {
|
2016-06-15 20:34:45 +00:00
|
|
|
JobModifyIndex uint64
|
|
|
|
CreatedEvals []*Evaluation
|
|
|
|
Diff *JobDiff
|
|
|
|
Annotations *PlanAnnotations
|
|
|
|
FailedTGAllocs map[string]*AllocationMetric
|
|
|
|
NextPeriodicLaunch time.Time
|
2017-05-10 03:52:47 +00:00
|
|
|
|
|
|
|
// Warnings contains any warnings about the given job. These may include
|
|
|
|
// deprecation warnings.
|
|
|
|
Warnings string
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobDiff struct {
|
|
|
|
Type string
|
|
|
|
ID string
|
|
|
|
Fields []*FieldDiff
|
|
|
|
Objects []*ObjectDiff
|
|
|
|
TaskGroups []*TaskGroupDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskGroupDiff struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Fields []*FieldDiff
|
|
|
|
Objects []*ObjectDiff
|
|
|
|
Tasks []*TaskDiff
|
|
|
|
Updates map[string]uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskDiff struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Fields []*FieldDiff
|
|
|
|
Objects []*ObjectDiff
|
|
|
|
Annotations []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type FieldDiff struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Old, New string
|
|
|
|
Annotations []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectDiff struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Fields []*FieldDiff
|
|
|
|
Objects []*ObjectDiff
|
|
|
|
}
|
|
|
|
|
|
|
|
type PlanAnnotations struct {
|
|
|
|
DesiredTGUpdates map[string]*DesiredUpdates
|
2018-09-10 17:38:36 +00:00
|
|
|
PreemptedAllocs []*AllocationListStub
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DesiredUpdates struct {
|
|
|
|
Ignore uint64
|
|
|
|
Place uint64
|
|
|
|
Migrate uint64
|
|
|
|
Stop uint64
|
|
|
|
InPlaceUpdate uint64
|
|
|
|
DestructiveUpdate uint64
|
2017-05-23 20:02:47 +00:00
|
|
|
Canary uint64
|
2018-11-01 22:05:17 +00:00
|
|
|
Preemptions uint64
|
2016-05-12 01:51:48 +00:00
|
|
|
}
|
2016-12-05 05:22:13 +00:00
|
|
|
|
|
|
|
type JobDispatchRequest struct {
|
2022-10-06 20:21:40 +00:00
|
|
|
JobID string
|
|
|
|
Payload []byte
|
|
|
|
Meta map[string]string
|
|
|
|
IdPrefixTemplate string
|
2016-12-05 05:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobDispatchResponse struct {
|
|
|
|
DispatchedJobID string
|
|
|
|
EvalID string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
JobCreateIndex uint64
|
2017-02-16 23:47:36 +00:00
|
|
|
WriteMeta
|
2016-12-05 05:22:13 +00:00
|
|
|
}
|
2017-06-30 00:16:20 +00:00
|
|
|
|
|
|
|
// JobVersionsResponse is used for a job get versions request
|
|
|
|
type JobVersionsResponse struct {
|
|
|
|
Versions []*Job
|
|
|
|
Diffs []*JobDiff
|
|
|
|
QueryMeta
|
|
|
|
}
|
2017-07-06 19:49:13 +00:00
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// JobSubmissionResponse is used for a job get submission request
|
|
|
|
type JobSubmissionResponse struct {
|
|
|
|
Submission *JobSubmission
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:49:13 +00:00
|
|
|
// JobStabilityRequest is used to marked a job as stable.
|
|
|
|
type JobStabilityRequest struct {
|
|
|
|
// Job to set the stability on
|
|
|
|
JobID string
|
|
|
|
JobVersion uint64
|
|
|
|
|
|
|
|
// Set the stability
|
|
|
|
Stable bool
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobStabilityResponse is the response when marking a job as stable.
|
|
|
|
type JobStabilityResponse struct {
|
|
|
|
JobModifyIndex uint64
|
|
|
|
WriteMeta
|
|
|
|
}
|
2018-05-09 20:04:27 +00:00
|
|
|
|
|
|
|
// JobEvaluateRequest is used when we just need to re-evaluate a target job
|
|
|
|
type JobEvaluateRequest struct {
|
|
|
|
JobID string
|
|
|
|
EvalOptions EvalOptions
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalOptions is used to encapsulate options when forcing a job evaluation
|
|
|
|
type EvalOptions struct {
|
|
|
|
ForceReschedule bool
|
|
|
|
}
|