-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Making the API default missing values #2300
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f551dcb
Making the job spec return api.Job
diptanu 5502a66
Added tests
diptanu 79d5181
Added docs
diptanu b56764a
Fixed the nomad status command
diptanu 67e6098
Removing un-neccessary conversions
diptanu 7eac718
Use RC to see if cause of failure
dadgar 592bc73
Making the job register endpoint use api.Job
diptanu ea79a14
Fix detection of missing port
dadgar b6e0d12
Flatmap
dadgar 6debb3e
Refactor test and fix them
dadgar ef00766
Fix CLI handling of parameterized/periodic jobs
dadgar 8557a89
Fixed cli tests
diptanu aa820e9
Merge branch 'refactor-parser' of github.com:hashicorp/nomad into ref…
diptanu 00f8237
Downgrading to Go 1.7
diptanu 76ab4b6
Merge branch 'master' into refactor-parser
dadgar 0ba19ff
Bump
dadgar 503746c
Add defaulting test and fix types on the agent job endpoint tests
dadgar 7cef0d7
fix pointer
dadgar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ services: | |
language: go | ||
|
||
go: | ||
- 1.7 | ||
- 1.8 | ||
|
||
branches: | ||
only: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ import ( | |
"sort" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gorhill/cronexpr" | ||
"github.com/hashicorp/nomad/helper" | ||
) | ||
|
||
const ( | ||
|
@@ -14,6 +17,9 @@ const ( | |
|
||
// JobTypeBatch indicates a short-lived process | ||
JobTypeBatch = "batch" | ||
|
||
// PeriodicSpecCron is used for a cron spec. | ||
PeriodicSpecCron = "cron" | ||
) | ||
|
||
const ( | ||
|
@@ -32,6 +38,16 @@ func (c *Client) Jobs() *Jobs { | |
return &Jobs{client: c} | ||
} | ||
|
||
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} | ||
} | ||
wm, err := j.client.write("/v1/validate/job", req, &resp, q) | ||
return &resp, wm, err | ||
} | ||
|
||
// Register is used to register a new job. It returns the ID | ||
// of the evaluation, along with any errors encountered. | ||
func (j *Jobs) Register(job *Job, q *WriteOptions) (string, *WriteMeta, error) { | ||
|
@@ -162,7 +178,7 @@ func (j *Jobs) Plan(job *Job, diff bool, q *WriteOptions) (*JobPlanResponse, *Wr | |
Job: job, | ||
Diff: diff, | ||
} | ||
wm, err := j.client.write("/v1/job/"+job.ID+"/plan", req, &resp, q) | ||
wm, err := j.client.write("/v1/job/"+*job.ID+"/plan", req, &resp, q) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
@@ -207,13 +223,42 @@ type UpdateStrategy struct { | |
|
||
// PeriodicConfig is for serializing periodic config for a job. | ||
type PeriodicConfig struct { | ||
Enabled bool | ||
Spec string | ||
SpecType string | ||
ProhibitOverlap bool | ||
Enabled *bool | ||
Spec *string | ||
SpecType *string | ||
ProhibitOverlap *bool | ||
TimeZone *string | ||
} | ||
|
||
func (p *PeriodicConfig) Canonicalize() { | ||
if p.Enabled == nil { | ||
p.Enabled = helper.BoolToPtr(true) | ||
} | ||
if p.SpecType == nil { | ||
p.SpecType = helper.StringToPtr(PeriodicSpecCron) | ||
} | ||
if p.ProhibitOverlap == nil { | ||
p.ProhibitOverlap = helper.BoolToPtr(false) | ||
} | ||
if p.TimeZone == nil || *p.TimeZone == "" { | ||
p.TimeZone = helper.StringToPtr("UTC") | ||
} | ||
} | ||
|
||
// 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. | ||
func (p *PeriodicConfig) Next(fromTime time.Time) time.Time { | ||
if *p.SpecType == PeriodicSpecCron { | ||
if e, err := cronexpr.Parse(*p.Spec); err == nil { | ||
return e.Next(fromTime) | ||
} | ||
} | ||
|
||
return time.Time{} | ||
} | ||
|
||
func (p *PeriodicConfig) GetLocation() (*time.Location, error) { | ||
if p.TimeZone == nil || *p.TimeZone == "" { | ||
return time.UTC, nil | ||
|
@@ -231,13 +276,13 @@ type ParameterizedJobConfig struct { | |
|
||
// Job is used to serialize a job. | ||
type Job struct { | ||
Region string | ||
ID string | ||
ParentID string | ||
Name string | ||
Type string | ||
Priority int | ||
AllAtOnce bool | ||
Region *string | ||
ID *string | ||
ParentID *string | ||
Name *string | ||
Type *string | ||
Priority *int | ||
AllAtOnce *bool | ||
Datacenters []string | ||
Constraints []*Constraint | ||
TaskGroups []*TaskGroup | ||
|
@@ -246,12 +291,71 @@ type Job struct { | |
ParameterizedJob *ParameterizedJobConfig | ||
Payload []byte | ||
Meta map[string]string | ||
VaultToken string | ||
Status string | ||
StatusDescription string | ||
CreateIndex uint64 | ||
ModifyIndex uint64 | ||
JobModifyIndex uint64 | ||
VaultToken *string | ||
Status *string | ||
StatusDescription *string | ||
CreateIndex *uint64 | ||
ModifyIndex *uint64 | ||
JobModifyIndex *uint64 | ||
} | ||
|
||
// 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 { | ||
return j.ParameterizedJob != nil | ||
} | ||
|
||
func (j *Job) Canonicalize() { | ||
if j.ID == nil { | ||
j.ID = helper.StringToPtr("") | ||
} | ||
if j.Name == nil { | ||
j.Name = j.ID | ||
} | ||
if j.ParentID == nil { | ||
j.ParentID = helper.StringToPtr("") | ||
} | ||
if j.Priority == nil { | ||
j.Priority = helper.IntToPtr(50) | ||
} | ||
if j.Region == nil { | ||
j.Region = helper.StringToPtr("global") | ||
} | ||
if j.Type == nil { | ||
j.Type = helper.StringToPtr("service") | ||
} | ||
if j.AllAtOnce == nil { | ||
j.AllAtOnce = helper.BoolToPtr(false) | ||
} | ||
if j.VaultToken == nil { | ||
j.VaultToken = helper.StringToPtr("") | ||
} | ||
if j.Status == nil { | ||
j.Status = helper.StringToPtr("") | ||
} | ||
if j.StatusDescription == nil { | ||
j.StatusDescription = helper.StringToPtr("") | ||
} | ||
if j.CreateIndex == nil { | ||
j.CreateIndex = helper.Uint64ToPtr(0) | ||
} | ||
if j.ModifyIndex == nil { | ||
j.ModifyIndex = helper.Uint64ToPtr(0) | ||
} | ||
if j.JobModifyIndex == nil { | ||
j.JobModifyIndex = helper.Uint64ToPtr(0) | ||
} | ||
if j.Periodic != nil { | ||
j.Periodic.Canonicalize() | ||
} | ||
|
||
for _, tg := range j.TaskGroups { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Break these up into different functions |
||
tg.Canonicalize(*j.Type) | ||
} | ||
} | ||
|
||
// JobSummary summarizes the state of the allocations of a job | ||
|
@@ -339,11 +443,11 @@ func NewBatchJob(id, name, region string, pri int) *Job { | |
// newJob is used to create a new Job struct. | ||
func newJob(id, name, region, typ string, pri int) *Job { | ||
return &Job{ | ||
Region: region, | ||
ID: id, | ||
Name: name, | ||
Type: typ, | ||
Priority: pri, | ||
Region: ®ion, | ||
ID: &id, | ||
Name: &name, | ||
Type: &typ, | ||
Priority: &pri, | ||
} | ||
} | ||
|
||
|
@@ -380,6 +484,39 @@ func (j *Job) AddPeriodicConfig(cfg *PeriodicConfig) *Job { | |
return j | ||
} | ||
|
||
type WriteRequest struct { | ||
// The target region for this write | ||
Region string | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// JobUpdateRequest is used to update a job | ||
type JobRegisterRequest struct { | ||
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. | ||
EnforceIndex bool | ||
JobModifyIndex uint64 | ||
|
||
WriteRequest | ||
} | ||
|
||
// RegisterJobRequest is used to serialize a job registration | ||
type RegisterJobRequest struct { | ||
Job *Job | ||
|
@@ -400,6 +537,7 @@ type deregisterJobResponse struct { | |
type JobPlanRequest struct { | ||
Job *Job | ||
Diff bool | ||
WriteRequest | ||
} | ||
|
||
type JobPlanResponse struct { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar block for update strategy and parameterized?