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

Allow specification of a custom job name/prefix for parameterized jobs #14631

Merged
merged 3 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/14631.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Added `-id-prefix-template` option to `nomad job dispatch`
```
16 changes: 9 additions & 7 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,13 @@ func (j *Jobs) Summary(jobID string, q *QueryOptions) (*JobSummary, *QueryMeta,
}

func (j *Jobs) Dispatch(jobID string, meta map[string]string,
payload []byte, q *WriteOptions) (*JobDispatchResponse, *WriteMeta, error) {
payload []byte, idPrefixTemplate string, q *WriteOptions) (*JobDispatchResponse, *WriteMeta, error) {
var resp JobDispatchResponse
req := &JobDispatchRequest{
JobID: jobID,
Meta: meta,
Payload: payload,
JobID: jobID,
Meta: meta,
Payload: payload,
IdPrefixTemplate: idPrefixTemplate,
}
wm, err := j.client.write("/v1/job/"+url.PathEscape(jobID)+"/dispatch", req, &resp, q)
if err != nil {
Expand Down Expand Up @@ -1342,9 +1343,10 @@ type DesiredUpdates struct {
}

type JobDispatchRequest struct {
JobID string
Payload []byte
Meta map[string]string
JobID string
Payload []byte
Meta map[string]string
IdPrefixTemplate string
}

type JobDispatchResponse struct {
Expand Down
4 changes: 3 additions & 1 deletion command/job_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ func (c *JobDispatchCommand) Run(args []string) int {
var detach, verbose bool
var idempotencyToken string
var meta []string
var idPrefixTemplate string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&detach, "detach", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.StringVar(&idempotencyToken, "idempotency-token", "", "")
flags.Var((*flaghelper.StringFlag)(&meta), "meta", "")
flags.StringVar(&idPrefixTemplate, "id-prefix-template", "", "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -174,7 +176,7 @@ func (c *JobDispatchCommand) Run(args []string) int {
w := &api.WriteOptions{
IdempotencyToken: idempotencyToken,
}
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, w)
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, idPrefixTemplate, w)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
return 1
Expand Down
2 changes: 1 addition & 1 deletion e2e/scheduler_sysbatch/sysbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (tc *SysBatchSchedulerTest) TestJobRunDispatch(f *framework.F) {
jobs := nomadClient.Jobs()
result, _, err := jobs.Dispatch(jobID, map[string]string{
"KEY": "value",
}, nil, nil)
}, nil, "", nil)
require.NoError(t, err)

// grab the new dispatched jobID
Expand Down
2 changes: 1 addition & 1 deletion nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ func (j *Job) Dispatch(args *structs.JobDispatchRequest, reply *structs.JobDispa

// Derive the child job and commit it via Raft - with initial status
dispatchJob := parameterizedJob.Copy()
dispatchJob.ID = structs.DispatchedID(parameterizedJob.ID, time.Now())
dispatchJob.ID = structs.DispatchedID(parameterizedJob.ID, args.IdPrefixTemplate, time.Now())
dispatchJob.ParentID = parameterizedJob.ID
dispatchJob.Name = dispatchJob.ID
dispatchJob.SetSubmitTime()
Expand Down
8 changes: 7 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ type JobDispatchRequest struct {
Payload []byte
Meta map[string]string
WriteRequest
IdPrefixTemplate string
}

// JobValidateRequest is used to validate a job
Expand Down Expand Up @@ -5366,8 +5367,13 @@ func (d *ParameterizedJobConfig) Copy() *ParameterizedJobConfig {

// DispatchedID returns an ID appropriate for a job dispatched against a
// particular parameterized job
func DispatchedID(templateID string, t time.Time) string {
func DispatchedID(templateID, idPrefixTemplate string, t time.Time) string {
u := uuid.Generate()[:8]

if idPrefixTemplate != "" {
return fmt.Sprintf("%s%s%s-%d-%s", templateID, DispatchLaunchSuffix, idPrefixTemplate, t.Unix(), u)
}

return fmt.Sprintf("%s%s%d-%s", templateID, DispatchLaunchSuffix, t.Unix(), u)
}

Expand Down