Skip to content

Commit

Permalink
Merge pull request #12496 from hashicorp/f-cores-env
Browse files Browse the repository at this point in the history
client: set environment variable indicating set of reserved cpu cores
  • Loading branch information
shoenig authored Apr 7, 2022
2 parents a30c3f3 + 5c5607a commit 2c6e84c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/12496.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
client: set NOMAD_CPU_CORES environment variable when reserving cpu cores
```
11 changes: 10 additions & 1 deletion client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/nomad/helper"
hargs "github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/lib/cpuset"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -40,6 +41,9 @@ const (
// CpuLimit is the environment variable with the tasks CPU limit in MHz.
CpuLimit = "NOMAD_CPU_LIMIT"

// CpuCores is the environment variable for passing the task's reserved cpu cores
CpuCores = "NOMAD_CPU_CORES"

// AllocID is the environment variable for passing the allocation ID.
AllocID = "NOMAD_ALLOC_ID"

Expand Down Expand Up @@ -397,6 +401,7 @@ type Builder struct {
// clientTaskSecretsDir is the secrets dir from the client's perspective; eg <client_task_root>/secrets
clientTaskSecretsDir string

cpuCores string
cpuLimit int64
memLimit int64
memMaxLimit int64
Expand Down Expand Up @@ -493,6 +498,9 @@ func (b *Builder) buildEnv(allocDir, localDir, secretsDir string,
if b.cpuLimit != 0 {
envMap[CpuLimit] = strconv.FormatInt(b.cpuLimit, 10)
}
if b.cpuCores != "" {
envMap[CpuCores] = b.cpuCores
}

// Add the task metadata
if b.allocId != "" {
Expand Down Expand Up @@ -742,6 +750,7 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
// Populate task resources
if tr, ok := alloc.AllocatedResources.Tasks[b.taskName]; ok {
b.cpuLimit = tr.Cpu.CpuShares
b.cpuCores = cpuset.New(tr.Cpu.ReservedCores...).String()
b.memLimit = tr.Memory.MemoryMB
b.memMaxLimit = tr.Memory.MemoryMaxMB

Expand Down Expand Up @@ -788,7 +797,7 @@ func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
}
}

upstreams := []structs.ConsulUpstream{}
var upstreams []structs.ConsulUpstream
for _, svc := range tg.Services {
if svc.Connect.HasSidecar() && svc.Connect.SidecarService.HasUpstreams() {
upstreams = append(upstreams, svc.Connect.SidecarService.Proxy.Upstreams...)
Expand Down
8 changes: 7 additions & 1 deletion client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ func TestEnvironment_AsList(t *testing.T) {
a := mock.Alloc()
a.Job.ParentID = fmt.Sprintf("mock-parent-service-%s", uuid.Generate())
a.AllocatedResources.Tasks["web"] = &structs.AllocatedTaskResources{
Cpu: structs.AllocatedCpuResources{CpuShares: 500},
Cpu: structs.AllocatedCpuResources{
CpuShares: 500,
ReservedCores: []uint16{0, 5, 6, 7},
},
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
MemoryMaxMB: 512,
Expand Down Expand Up @@ -215,6 +218,7 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_PORT_ssh_other=1234",
"NOMAD_PORT_ssh_ssh=22",
"NOMAD_CPU_LIMIT=500",
"NOMAD_CPU_CORES=0,5-7",
"NOMAD_DC=dc1",
"NOMAD_NAMESPACE=not-default",
"NOMAD_REGION=global",
Expand Down Expand Up @@ -260,6 +264,7 @@ func TestEnvironment_AllValues(t *testing.T) {
MBits: 50,
DynamicPorts: []structs.Port{{Label: "http", Value: 80}},
}
a.AllocatedResources.Tasks["web"].Cpu.ReservedCores = []uint16{0, 5, 6, 7}
a.AllocatedResources.Tasks["ssh"] = &structs.AllocatedTaskResources{
Networks: []*structs.NetworkResource{
{
Expand Down Expand Up @@ -378,6 +383,7 @@ func TestEnvironment_AllValues(t *testing.T) {
"NOMAD_PORT_ssh_other": "1234",
"NOMAD_PORT_ssh_ssh": "22",
"NOMAD_CPU_LIMIT": "500",
"NOMAD_CPU_CORES": "0,5-7",
"NOMAD_DC": "dc1",
"NOMAD_PARENT_CGROUP": "abc.slice",
"NOMAD_NAMESPACE": "default",
Expand Down
9 changes: 9 additions & 0 deletions website/content/partials/envvars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
</td>
<td>CPU limit in MHz for the task</td>
</tr>
<tr>
<td>
<code>NOMAD_CPU_CORES</code>
</td>
<td>
The specific CPU cores reserved for the task in cpuset list notation.
Omitted if the the task does not request cpu cores. E.g. <code>0-2,7,12-14</code>
</td>
</tr>
<tr>
<td>
<code>NOMAD_ALLOC_ID</code>
Expand Down

0 comments on commit 2c6e84c

Please sign in to comment.