Skip to content

Commit

Permalink
scaling: use request namespace as target if unset in jobspec
Browse files Browse the repository at this point in the history
When jobs are submitted with a scaling policy, the scaling policy's target only
includes the job's namespace if the `namespace` field is set in the jobspec and
not from the request. Normally jobs are canonicalized in the RPC handler before
being written to Raft. But the scaling policy targets are instead written during
the conversion from `api.Job` to `structs.Job`. We populate the `structs.Job`
namespace from the request here as well, but only after the conversion has
occurred. Swap the order of these operations so that the conversion is always
happening with a correct namespace.

Fixes: #24039
  • Loading branch information
tgross committed Sep 26, 2024
1 parent ee9eb45 commit cfd10bf
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/nomad/acl"
api "github.com/hashicorp/nomad/api"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/jobspec2"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand Down Expand Up @@ -1008,15 +1009,17 @@ func (s *HTTPServer) apiJobAndRequestToStructs(job *api.Job, req *http.Request,
job, queryRegion, writeReq.Region, s.agent.GetConfig().Region,
)

sJob := ApiJobToStructJob(job)
sJob.Region = jobRegion
writeReq.Region = requestRegion

// mutate the namespace before we convert just in case anything is expecting
// the namespace to be correct
queryNamespace := req.URL.Query().Get("namespace")
namespace := namespaceForJob(job.Namespace, queryNamespace, writeReq.Namespace)
sJob.Namespace = namespace
job.Namespace = pointer.Of(namespace)
writeReq.Namespace = namespace

sJob := ApiJobToStructJob(job)
sJob.Region = jobRegion
writeReq.Region = requestRegion

return sJob, writeReq
}

Expand Down

0 comments on commit cfd10bf

Please sign in to comment.