diff --git a/README.md b/README.md index 1dd2659a..bb8500f0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ this plugin to Nomad! * Monitor the memory consumption * Monitor CPU usage * Task config cpu value is used to populate podman CpuShares +* Task config cores value is used to populate podman Cpuset * Container log is forwarded to [Nomad logger](https://www.nomadproject.io/docs/commands/alloc/logs.html) * Utilize podmans --init feature * Set username or UID used for the specified command within the container (podman --user option). diff --git a/driver.go b/driver.go index 75960ba6..2cdc90d0 100644 --- a/driver.go +++ b/driver.go @@ -525,7 +525,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive } // Set the nomad slice as cgroup parent - d.setupCgroup(createOpts) + d.setupCgroup(&createOpts) // Resources config options createOpts.ContainerResourceConfig.ResourceLimits = &spec.LinuxResources{ @@ -769,7 +769,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive } // setupCgroup customizes where the cgroup lives (v2 only) -func (d *Driver) setupCgroup(opts api.SpecGenerator) { +func (d *Driver) setupCgroup(opts *api.SpecGenerator) { if d.cgroupV2 { opts.ContainerCgroupConfig.CgroupParent = "nomad.slice" } @@ -804,6 +804,12 @@ func memoryLimits(r drivers.MemoryResources, reservation string) (hard, soft *in } func setCPUResources(cfg TaskConfig, systemResources *drivers.LinuxResources, taskCPU *spec.LinuxCPU) error { + // always assign cpuset + taskCPU.Cpus = systemResources.CpusetCpus + + // only set bandwidth if hard limit is enabled + // currently only docker and podman even provide this option - the exec + // drivers do not even support bandwidth yet if !cfg.CPUHardLimit { return nil } diff --git a/driver_test.go b/driver_test.go index 6490cb4f..d69fe74b 100644 --- a/driver_test.go +++ b/driver_test.go @@ -2123,6 +2123,28 @@ func createInspectImage(t *testing.T, image, reference string) { must.Eq(t, idRef, idTest) } +func Test_setTaskCpuset(t *testing.T) { + ci.Parallel(t) + + t.Run("empty", func(t *testing.T) { + sysResources := &drivers.LinuxResources{CpusetCpus: ""} + taskCPU := new(spec.LinuxCPU) + cfg := TaskConfig{} + err := setCPUResources(cfg, sysResources, taskCPU) + must.NoError(t, err) + must.Eq(t, "", taskCPU.Cpus) + }) + + t.Run("non-empty", func(t *testing.T) { + sysResources := &drivers.LinuxResources{CpusetCpus: "2,5-8"} + taskCPU := new(spec.LinuxCPU) + cfg := TaskConfig{} + err := setCPUResources(cfg, sysResources, taskCPU) + must.NoError(t, err) + must.Eq(t, "2,5-8", taskCPU.Cpus) + }) +} + func Test_cpuLimits(t *testing.T) { ci.Parallel(t)