Skip to content

Commit d5e429d

Browse files
committed
job region defaults to client node region if 'global' or none provided (#6064)
1 parent e4920d2 commit d5e429d

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

api/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ func (c *Client) getNodeClientImpl(nodeID string, timeout time.Duration, q *Quer
453453
// If the client is configured for a particular region use that
454454
region = c.config.Region
455455
default:
456-
// No region information is given so use the default.
457-
region = "global"
456+
// No region information is given so use GlobalRegion as the default.
457+
region = GlobalRegion
458458
}
459459

460460
// Get an API client for the node

api/jobs.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const (
2525

2626
// DefaultNamespace is the default namespace.
2727
DefaultNamespace = "default"
28+
29+
// For Job configuration, GlobalRegion is a sentinel region value
30+
// that users may specify to indicate the job should be run on
31+
// the region of the node that the job was submitted to.
32+
// For Client configuration, if no region information is given,
33+
// the client node will default to be part of the GlobalRegion.
34+
GlobalRegion = "global"
2835
)
2936

3037
const (
@@ -704,7 +711,7 @@ func (j *Job) Canonicalize() {
704711
j.Stop = boolToPtr(false)
705712
}
706713
if j.Region == nil {
707-
j.Region = stringToPtr("global")
714+
j.Region = stringToPtr(GlobalRegion)
708715
}
709716
if j.Namespace == nil {
710717
j.Namespace = stringToPtr("default")

command/agent/job_endpoint.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ func (s *HTTPServer) jobPlan(resp http.ResponseWriter, req *http.Request,
141141
return nil, CodedError(400, "Job ID does not match")
142142
}
143143

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

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

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

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

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

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

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

command/agent/job_endpoint_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,17 @@ func TestHTTP_JobUpdateRegion(t *testing.T) {
493493
ExpectedRegion: "north-america",
494494
},
495495
{
496-
Name: "falls back to default if no region is provided",
496+
Name: "defaults to node region global if no region is provided",
497497
ConfigRegion: "",
498498
APIRegion: "",
499499
ExpectedRegion: "global",
500500
},
501+
{
502+
Name: "defaults to node region not-global if no region is provided",
503+
ConfigRegion: "",
504+
APIRegion: "",
505+
ExpectedRegion: "not-global",
506+
},
501507
}
502508

503509
for _, tc := range cases {

0 commit comments

Comments
 (0)