Skip to content

Commit b0be1ac

Browse files
authored
Merge pull request #3007 from mhermida/missing_metrics_cgroupsv2
Fix missing metrics on cgroups v2
2 parents 3c6e309 + c3b109f commit b0be1ac

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

container/common/helpers.go

Lines changed: 17 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,7 @@ 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 := getControllerPath(cgroupPaths, "pids", cgroup2UnifiedMode)
199199
if ok {
200200
if utils.FileExists(pidsRoot) {
201201
spec.HasProcesses = true
@@ -217,6 +217,19 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
217217
return spec, nil
218218
}
219219

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

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)