Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing metrics on cgroups v2 #3007

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
}

// CPU.
cpuRoot, ok := cgroupPaths["cpu"]
cpuRoot, ok := getControllerPath(cgroupPaths, "cpu", cgroup2UnifiedMode)
if ok {
if utils.FileExists(cpuRoot) {
if cgroup2UnifiedMode {
Expand Down Expand Up @@ -152,7 +152,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach

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

// Memory
memoryRoot, ok := cgroupPaths["memory"]
memoryRoot, ok := getControllerPath(cgroupPaths, "memory", cgroup2UnifiedMode)
if ok {
if cgroup2UnifiedMode {
if utils.FileExists(path.Join(memoryRoot, "memory.max")) {
Expand Down Expand Up @@ -195,7 +195,7 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
}

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

func getControllerPath(cgroupPaths map[string]string, controllerName string, cgroup2UnifiedMode bool) (string, bool) {

ok := false
path := ""

if cgroup2UnifiedMode {
path, ok = cgroupPaths[""]
} else {
path, ok = cgroupPaths[controllerName]
}
return path, ok
}

func readString(dirpath string, file string) string {
cgroupFile := path.Join(dirpath, file)

Expand Down
9 changes: 2 additions & 7 deletions container/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ func TestGetSpecCgroupV2(t *testing.T) {
}

cgroupPaths := map[string]string{
"memory": filepath.Join(root, "test_resources/cgroup_v2/test1"),
"cpu": filepath.Join(root, "test_resources/cgroup_v2/test1"),
"cpuset": filepath.Join(root, "test_resources/cgroup_v2/test1"),
"pids": filepath.Join(root, "test_resources/cgroup_v2/test1"),
"": filepath.Join(root, "test_resources/cgroup_v2/test1"),
}

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

cgroupPaths := map[string]string{
"memory": filepath.Join(root, "test_resources/cgroup_v2/test2"),
"cpu": filepath.Join(root, "test_resources/cgroup_v2/test2"),
"pids": filepath.Join(root, "test_resources/cgroup_v2/test2"),
"": filepath.Join(root, "test_resources/cgroup_v2/test2"),
}

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