Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: enable support for setting original job source #16763

Merged
merged 10 commits into from
Apr 11, 2023
3 changes: 3 additions & 0 deletions .changelog/16763.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: enable support for storing original job source
```
88 changes: 79 additions & 9 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/hashicorp/cronexpr"
"golang.org/x/exp/maps"
)

const (
Expand Down Expand Up @@ -68,6 +69,11 @@ type JobsParseRequest struct {
// HCLv1 indicates whether the JobHCL should be parsed with the hcl v1 parser
HCLv1 bool `json:"hclv1,omitempty"`

// 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

// Canonicalize is a flag as to if the server should return default values
// for unset fields
Canonicalize bool
Expand All @@ -78,7 +84,7 @@ func (c *Client) Jobs() *Jobs {
return &Jobs{client: c}
}

// ParseHCL is used to convert the HCL repesentation of a Job to JSON server side.
// ParseHCL is used to convert the HCL representation of a Job to JSON server side.
// To parse the HCL client side see package github.com/hashicorp/nomad/jobspec
// Use ParseHCLOpts if you need to customize JobsParseRequest.
func (j *Jobs) ParseHCL(jobHCL string, canonicalize bool) (*Job, error) {
Expand All @@ -89,10 +95,8 @@ func (j *Jobs) ParseHCL(jobHCL string, canonicalize bool) (*Job, error) {
return j.ParseHCLOpts(req)
}

// ParseHCLOpts is used to convert the HCL representation of a Job to JSON
// server side. To parse the HCL client side see package
// github.com/hashicorp/nomad/jobspec.
// ParseHCL is an alternative convenience API for HCLv2 users.
// 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.
func (j *Jobs) ParseHCLOpts(req *JobsParseRequest) (*Job, error) {
var job Job
_, err := j.client.put("/v1/jobs/parse", req, &job, nil)
Expand All @@ -116,6 +120,7 @@ type RegisterOptions struct {
PolicyOverride bool
PreserveCounts bool
EvalPriority int
Submission *JobSubmission
}

// Register is used to register a new job. It returns the ID
Expand All @@ -134,9 +139,7 @@ func (j *Jobs) EnforceRegister(job *Job, modifyIndex uint64, q *WriteOptions) (*
// returns the ID of the evaluation, along with any errors encountered.
func (j *Jobs) RegisterOpts(job *Job, opts *RegisterOptions, q *WriteOptions) (*JobRegisterResponse, *WriteMeta, error) {
// Format the request
req := &JobRegisterRequest{
Job: job,
}
req := &JobRegisterRequest{Job: job}
if opts != nil {
if opts.EnforceIndex {
req.EnforceIndex = true
Expand All @@ -145,6 +148,7 @@ func (j *Jobs) RegisterOpts(job *Job, opts *RegisterOptions, q *WriteOptions) (*
req.PolicyOverride = opts.PolicyOverride
req.PreserveCounts = opts.PreserveCounts
req.EvalPriority = opts.EvalPriority
req.Submission = opts.Submission
}

var resp JobRegisterResponse
Expand Down Expand Up @@ -252,6 +256,19 @@ func (j *Jobs) Versions(jobID string, diffs bool, q *QueryOptions) ([]*Job, []*J
return resp.Versions, resp.Diffs, qm, nil
}

// 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
}

// Allocations is used to return the allocs for a given job ID.
func (j *Jobs) Allocations(jobID string, allAllocs bool, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
var resp []*AllocationListStub
Expand Down Expand Up @@ -863,6 +880,51 @@ type ParameterizedJobConfig struct {
MetaOptional []string `mapstructure:"meta_optional" hcl:"meta_optional,optional"`
}

// 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
Comment on lines +900 to +902
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a map[string]string to preserve each filename and and its contents?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the parse endpoint is really only used by the webUI and the webUI's variable content is coming from a html form, I don't think it quite make sense to associate the content with a file name

}

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,
}
}

// Job is used to serialize a job.
type Job struct {
/* Fields parsed from HCL config */
Expand Down Expand Up @@ -1248,7 +1310,9 @@ type JobRevertRequest struct {

// JobRegisterRequest is used to update a job
type JobRegisterRequest struct {
Job *Job
Submission *JobSubmission
Job *Job
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Job *Job
Job *Job


// 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.
Expand Down Expand Up @@ -1386,6 +1450,12 @@ type JobVersionsResponse struct {
QueryMeta
}

// JobSubmissionResponse is used for a job get submission request
type JobSubmissionResponse struct {
Submission *JobSubmission
QueryMeta
}

// JobStabilityRequest is used to marked a job as stable.
type JobStabilityRequest struct {
// Job to set the stability on
Expand Down
Loading