Skip to content

Commit c095de9

Browse files
committed
common/helpers: path lookups fail in the cgroups v2 cgroupPaths that only has key ""
1 parent 34bbefa commit c095de9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

container/common/helpers.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
105105
}
106106

107107
// CPU.
108-
cpuRoot, ok := cgroupPaths["cpu"]
108+
cpuRoot, ok := getControllerPath(cgroupPaths, "cpu", cgroup2UnifiedMode)
109109
if ok {
110110
if utils.FileExists(cpuRoot) {
111111
if cgroup2UnifiedMode {
@@ -152,7 +152,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
152152

153153
// Cpu Mask.
154154
// This will fail for non-unified hierarchies. We'll return the whole machine mask in that case.
155-
cpusetRoot, ok := cgroupPaths["cpuset"]
155+
cpusetRoot, ok := getControllerPath(cgroupPaths, "cpuset", cgroup2UnifiedMode)
156156
if ok {
157157
if utils.FileExists(cpusetRoot) {
158158
spec.HasCpu = true
@@ -167,7 +167,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
167167
}
168168

169169
// Memory
170-
memoryRoot, ok := cgroupPaths["memory"]
170+
memoryRoot, ok := getControllerPath(cgroupPaths, "memory", cgroup2UnifiedMode)
171171
if ok {
172172
if cgroup2UnifiedMode {
173173
if utils.FileExists(path.Join(memoryRoot, "memory.max")) {
@@ -195,7 +195,8 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
195195
}
196196

197197
// Processes, read it's value from pids path directly
198-
pidsRoot, ok := cgroupPaths["pids"]
198+
//pidsRoot, ok := cgroupPaths["pids"]
199+
pidsRoot, ok := getControllerPath(cgroupPaths, "pids", cgroup2UnifiedMode)
199200
if ok {
200201
if utils.FileExists(pidsRoot) {
201202
spec.HasProcesses = true
@@ -217,6 +218,19 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
217218
return spec, nil
218219
}
219220

221+
func getControllerPath(cgroupPaths map[string]string, controllerName string, cgroup2UnifiedMode bool) (string, bool) {
222+
223+
ok := false
224+
path := ""
225+
226+
if cgroup2UnifiedMode {
227+
path, ok = cgroupPaths[""]
228+
} else {
229+
path, ok = cgroupPaths[controllerName]
230+
}
231+
return path, ok
232+
}
233+
220234
func readString(dirpath string, file string) string {
221235
cgroupFile := path.Join(dirpath, file)
222236

container/common/helpers_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ func TestGetSpecCgroupV2(t *testing.T) {
143143
}
144144

145145
cgroupPaths := map[string]string{
146-
"memory": filepath.Join(root, "test_resources/cgroup_v2/test1"),
147-
"cpu": filepath.Join(root, "test_resources/cgroup_v2/test1"),
148-
"cpuset": filepath.Join(root, "test_resources/cgroup_v2/test1"),
149-
"pids": filepath.Join(root, "test_resources/cgroup_v2/test1"),
146+
"": filepath.Join(root, "test_resources/cgroup_v2/test1"),
150147
}
151148

152149
spec, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, true)
@@ -176,9 +173,7 @@ func TestGetSpecCgroupV2Max(t *testing.T) {
176173
assert.Nil(t, err)
177174

178175
cgroupPaths := map[string]string{
179-
"memory": filepath.Join(root, "test_resources/cgroup_v2/test2"),
180-
"cpu": filepath.Join(root, "test_resources/cgroup_v2/test2"),
181-
"pids": filepath.Join(root, "test_resources/cgroup_v2/test2"),
176+
"": filepath.Join(root, "test_resources/cgroup_v2/test2"),
182177
}
183178

184179
spec, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, true)

0 commit comments

Comments
 (0)