Skip to content

Commit

Permalink
api: enable support for setting original job source (#16763)
Browse files Browse the repository at this point in the history
* api: enable support for setting original source alongside job

This PR adds support for setting job source material along with
the registration of a job.

This includes a new HTTP endpoint and a new RPC endpoint for
making queries for the original source of a job. The
HTTP endpoint is /v1/job/<id>/submission?version=<version> and
the RPC method is Job.GetJobSubmission.

The job source (if submitted, and doing so is always optional), is
stored in the job_submission memdb table, separately from the
actual job. This way we do not incur overhead of reading the large
string field throughout normal job operations.

The server config now includes job_max_source_size for configuring
the maximum size the job source may be, before the server simply
drops the source material. This should help prevent Bad Things from
happening when huge jobs are submitted. If the value is set to 0,
all job source material will be dropped.

* api: avoid writing var content to disk for parsing

* api: move submission validation into RPC layer

* api: return an error if updating a job submission without namespace or job id

* api: be exact about the job index we associate a submission with (modify)

* api: reword api docs scheduling

* api: prune all but the last 6 job submissions

* api: protect against nil job submission in job validation

* api: set max job source size in test server

* api: fixups from pr
  • Loading branch information
shoenig committed Apr 11, 2023
1 parent 27b5596 commit 2c44cbb
Show file tree
Hide file tree
Showing 79 changed files with 1,986 additions and 653 deletions.
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 @@ -12,6 +12,7 @@ import (
"time"

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

const (
Expand Down Expand Up @@ -71,6 +72,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 @@ -81,7 +87,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 @@ -92,10 +98,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 @@ -119,6 +123,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 @@ -137,9 +142,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 @@ -148,6 +151,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 @@ -255,6 +259,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 @@ -866,6 +883,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
}

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 @@ -1251,7 +1313,9 @@ type JobRevertRequest struct {

// JobRegisterRequest is used to update a job
type JobRegisterRequest struct {
Job *Job
Submission *JobSubmission
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 @@ -1389,6 +1453,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

0 comments on commit 2c44cbb

Please sign in to comment.