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

job region defaults to node region #6064

Merged
merged 5 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ func (c *Client) getNodeClientImpl(nodeID string, timeout time.Duration, q *Quer
// If the client is configured for a particular region use that
region = c.config.Region
default:
// No region information is given so use the default.
region = "global"
// No region information is given so use GlobalRegion as the default.
region = GlobalRegion
}

// Get an API client for the node
Expand Down
9 changes: 8 additions & 1 deletion api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ const (

// DefaultNamespace is the default namespace.
DefaultNamespace = "default"

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

const (
Expand Down Expand Up @@ -704,7 +711,7 @@ func (j *Job) Canonicalize() {
j.Stop = boolToPtr(false)
}
if j.Region == nil {
j.Region = stringToPtr("global")
j.Region = stringToPtr(GlobalRegion)
}
if j.Namespace == nil {
j.Namespace = stringToPtr("default")
Expand Down
20 changes: 16 additions & 4 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,16 @@ func (s *HTTPServer) jobPlan(resp http.ResponseWriter, req *http.Request,
return nil, CodedError(400, "Job ID does not match")
}

// Http region takes precedence over hcl region
// Region in http request query param takes precedence over region in job hcl config
if args.WriteRequest.Region != "" {
args.Job.Region = helper.StringToPtr(args.WriteRequest.Region)
}
// If 'global' region is specified or if no region is given,
// default to region of the node you're submitting to
if args.Job.Region == nil || *args.Job.Region == "" || *args.Job.Region == api.GlobalRegion {
args.Job.Region = &s.agent.config.Region
}
jazzyfresh marked this conversation as resolved.
Show resolved Hide resolved

// If no region given, region is canonicalized to 'global'
sJob := ApiJobToStructJob(args.Job)

planReq := structs.JobPlanRequest{
Expand All @@ -157,6 +161,8 @@ func (s *HTTPServer) jobPlan(resp http.ResponseWriter, req *http.Request,
Region: sJob.Region,
},
}
// parseWriteRequest overrides Namespace, Region and AuthToken
// based on values from the original http request
s.parseWriteRequest(req, &planReq.WriteRequest)
planReq.Namespace = sJob.Namespace

Expand Down Expand Up @@ -384,12 +390,16 @@ func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request,
return nil, CodedError(400, "Job ID does not match name")
}

// Http region takes precedence over hcl region
// Region in http request query param takes precedence over region in job hcl config
if args.WriteRequest.Region != "" {
args.Job.Region = helper.StringToPtr(args.WriteRequest.Region)
}
// If 'global' region is specified or if no region is given,
// default to region of the node you're submitting to
if args.Job.Region == nil || *args.Job.Region == "" || *args.Job.Region == api.GlobalRegion {
args.Job.Region = &s.agent.config.Region
}

jazzyfresh marked this conversation as resolved.
Show resolved Hide resolved
// If no region given, region is canonicalized to 'global'
sJob := ApiJobToStructJob(args.Job)

regReq := structs.JobRegisterRequest{
Expand All @@ -402,6 +412,8 @@ func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request,
AuthToken: args.WriteRequest.SecretID,
},
}
// parseWriteRequest overrides Namespace, Region and AuthToken
// based on values from the original http request
s.parseWriteRequest(req, &regReq.WriteRequest)
regReq.Namespace = sJob.Namespace

Expand Down
8 changes: 7 additions & 1 deletion command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,17 @@ func TestHTTP_JobUpdateRegion(t *testing.T) {
ExpectedRegion: "north-america",
},
{
Name: "falls back to default if no region is provided",
Name: "defaults to node region global if no region is provided",
ConfigRegion: "",
APIRegion: "",
ExpectedRegion: "global",
},
{
Name: "defaults to node region not-global if no region is provided",
ConfigRegion: "",
APIRegion: "",
ExpectedRegion: "not-global",
},
}

for _, tc := range cases {
Expand Down