From 825efc3925f65f76c52ed74e6a52ed8575bd3c03 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Fri, 5 Apr 2024 08:05:51 -0500 Subject: [PATCH] docker: use correct effective cpuset filename on legacy cgroups v1 systems (#20294) --- .changelog/20294.txt | 3 +++ drivers/docker/cpuset.go | 14 ++++++++++++-- drivers/docker/cpuset_test.go | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .changelog/20294.txt diff --git a/.changelog/20294.txt b/.changelog/20294.txt new file mode 100644 index 000000000..83f756e45 --- /dev/null +++ b/.changelog/20294.txt @@ -0,0 +1,3 @@ +```release-note:bug +docker: Fixed a bug where cpuset cgroup would not be updated on cgroup v1 systems +``` diff --git a/drivers/docker/cpuset.go b/drivers/docker/cpuset.go index 7534c3364..a36b5ea58 100644 --- a/drivers/docker/cpuset.go +++ b/drivers/docker/cpuset.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + "github.com/hashicorp/nomad/client/lib/cgroupslib" "github.com/hashicorp/nomad/helper" ) @@ -49,8 +50,17 @@ func (c *cpuset) watch() { } } +func effectiveCpusetFile() string { + switch cgroupslib.GetMode() { + case cgroupslib.CG1: + return "cpuset.effective_cpus" + default: + return "cpuset.cpus.effective" + } +} + func (c *cpuset) copyCpuset(source, destination string) { - source = filepath.Join(source, "cpuset.cpus.effective") + source = filepath.Join(source, effectiveCpusetFile()) destination = filepath.Join(destination, "cpuset.cpus") // read the current value of usable cores @@ -67,7 +77,7 @@ func (c *cpuset) copyCpuset(source, destination string) { } // otherwise write the new value - err = os.WriteFile(destination, b, 0644) + err = os.WriteFile(destination, b, 0o644) if err != nil { return } diff --git a/drivers/docker/cpuset_test.go b/drivers/docker/cpuset_test.go index c4a7ceffb..73434f777 100644 --- a/drivers/docker/cpuset_test.go +++ b/drivers/docker/cpuset_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/hashicorp/nomad/ci" + "github.com/hashicorp/nomad/client/testutil" "github.com/shoenig/test/must" ) @@ -42,3 +43,21 @@ func Test_cpuset_watch(t *testing.T) { must.Eq(t, 1, hits) } + +func Test_effectiveCpusetFile_cgroupsv1(t *testing.T) { + testutil.CgroupsCompatibleV1(t) + + ci.Parallel(t) + + result := effectiveCpusetFile() + must.Eq(t, "cpuset.effective_cpus", result) +} + +func Test_effectiveCpusetFile_cgroupsv2(t *testing.T) { + testutil.CgroupsCompatibleV2(t) + + ci.Parallel(t) + + result := effectiveCpusetFile() + must.Eq(t, "cpuset.cpus.effective", result) +}