diff --git a/.changelog/13058.txt b/.changelog/13058.txt new file mode 100644 index 000000000..8ed6bbe80 --- /dev/null +++ b/.changelog/13058.txt @@ -0,0 +1,3 @@ +```release-note:bug +docker: Fixed a bug where cgroups-v1 parent was being set +``` diff --git a/drivers/docker/driver.go b/drivers/docker/driver.go index f418434ac..ec2455515 100644 --- a/drivers/docker/driver.go +++ b/drivers/docker/driver.go @@ -778,6 +778,15 @@ func memoryLimits(driverHardLimitMB int64, taskMemory drivers.MemoryResources) ( return hard * 1024 * 1024, softBytes } +// Extract the cgroup parent from the nomad cgroup (only for linux/v2) +func cgroupParent(resources *drivers.Resources) string { + var parent string + if cgutil.UseV2 && resources != nil && resources.LinuxResources != nil { + parent, _ = cgutil.SplitPath(resources.LinuxResources.CpusetCgroupPath) + } + return parent +} + func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *TaskConfig, imageID string) (docker.CreateContainerOptions, error) { @@ -843,11 +852,8 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T pidsLimit = driverConfig.PidsLimit } - // Extract the cgroup parent from the nomad cgroup (bypass the need for plugin config) - parent, _ := cgutil.SplitPath(task.Resources.LinuxResources.CpusetCgroupPath) - hostConfig := &docker.HostConfig{ - CgroupParent: parent, + CgroupParent: cgroupParent(task.Resources), // if applicable Memory: memory, // hard limit MemoryReservation: memoryReservation, // soft limit diff --git a/drivers/docker/driver_test.go b/drivers/docker/driver_test.go index a670dd765..4cc613e1d 100644 --- a/drivers/docker/driver_test.go +++ b/drivers/docker/driver_test.go @@ -2844,6 +2844,32 @@ func TestDockerDriver_memoryLimits(t *testing.T) { } } +func TestDockerDriver_cgroupParent(t *testing.T) { + ci.Parallel(t) + + t.Run("v1", func(t *testing.T) { + testutil.CgroupsCompatibleV1(t) + + parent := cgroupParent(&drivers.Resources{ + LinuxResources: &drivers.LinuxResources{ + CpusetCgroupPath: "/sys/fs/cgroup/cpuset/nomad", + }, + }) + require.Equal(t, "", parent) + }) + + t.Run("v2", func(t *testing.T) { + testutil.CgroupsCompatibleV2(t) + + parent := cgroupParent(&drivers.Resources{ + LinuxResources: &drivers.LinuxResources{ + CpusetCgroupPath: "/sys/fs/cgroup/nomad.slice", + }, + }) + require.Equal(t, "nomad.slice", parent) + }) +} + func TestDockerDriver_parseSignal(t *testing.T) { ci.Parallel(t)