From 2da8bd8f58b481c432c62e3f0acff641e19fd74b Mon Sep 17 00:00:00 2001 From: James Rasell Date: Wed, 22 Jul 2020 11:10:59 +0200 Subject: [PATCH] agent: task groups in system jobs do not support scaling stanzas. --- command/agent/job_endpoint.go | 12 ++++++++++++ command/agent/job_endpoint_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/command/agent/job_endpoint.go b/command/agent/job_endpoint.go index 6287876a6..f112372f6 100644 --- a/command/agent/job_endpoint.go +++ b/command/agent/job_endpoint.go @@ -391,6 +391,18 @@ func (s *HTTPServer) jobUpdate(resp http.ResponseWriter, req *http.Request, } } + // GH-8481. Jobs of type system can only have a count of 1 and therefore do + // not support scaling. Even though this returns an error on the first + // occurrence, the error is generic but detailed enough that an operator + // can fix the problem across multiple task groups. + if args.Job.Type != nil && *args.Job.Type == api.JobTypeSystem { + for _, tg := range args.Job.TaskGroups { + if tg.Scaling != nil { + return nil, CodedError(400, "Task groups with job type system do not support scaling stanzas") + } + } + } + sJob, writeReq := s.apiJobAndRequestToStructs(args.Job, req, args.WriteRequest) regReq := structs.JobRegisterRequest{ Job: sJob, diff --git a/command/agent/job_endpoint_test.go b/command/agent/job_endpoint_test.go index 3f4227b68..cb3fbfb54 100644 --- a/command/agent/job_endpoint_test.go +++ b/command/agent/job_endpoint_test.go @@ -448,6 +448,36 @@ func TestHTTP_JobQuery_Payload(t *testing.T) { }) } +func TestHTTP_jobUpdate_systemScaling(t *testing.T) { + t.Parallel() + httpTest(t, nil, func(s *TestAgent) { + // Create the job + job := MockJob() + job.Type = helper.StringToPtr("system") + job.TaskGroups[0].Scaling = &api.ScalingPolicy{Enabled: helper.BoolToPtr(true)} + args := api.JobRegisterRequest{ + Job: job, + WriteRequest: api.WriteRequest{ + Region: "global", + Namespace: api.DefaultNamespace, + }, + } + buf := encodeReq(args) + + // Make the HTTP request + req, err := http.NewRequest("PUT", "/v1/job/"+*job.ID, buf) + if err != nil { + t.Fatalf("err: %v", err) + } + respW := httptest.NewRecorder() + + // Make the request + obj, err := s.Server.JobSpecificRequest(respW, req) + assert.Nil(t, obj) + assert.Equal(t, CodedError(400, "Task groups with job type system do not support scaling stanzas"), err) + }) +} + func TestHTTP_JobUpdate(t *testing.T) { t.Parallel() httpTest(t, nil, func(s *TestAgent) {