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

util: support systems using the new cgroup v2 structure #3091

Merged
merged 1 commit into from
May 7, 2022
Merged
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
26 changes: 18 additions & 8 deletions internal/util/pidlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,44 @@ import (
)

const (
procCgroup = "/proc/self/cgroup"
sysPidsMaxFmt = "/sys/fs/cgroup/pids%s/pids.max"
procCgroup = "/proc/self/cgroup"
sysPidsMaxFmtCgroupV1 = "/sys/fs/cgroup/pids%s/pids.max"
sysPidsMaxFmtCgroupV2 = "/sys/fs/cgroup%s/pids.max"
)

// return the cgouprs "pids.max" file of the current process
//
// find the line containing the pids group from the /proc/self/cgroup file
// $ grep 'pids' /proc/self/cgroup
// getCgroupPidsFile return the cgroups "pids.max" file of the
// current process
// For cgroup v1, find the line containing the pids group from the /proc/self/cgroup file
// $ grep ':pids:' /proc/self/cgroup
// 7:pids:/kubepods.slice/kubepods-besteffort.slice/....scope
// $ cat /sys/fs/cgroup/pids + *.scope + /pids.max.
// The entry for cgroup v2 is always in the format "0::...scope", no subsystem given.
// (see https://www.kernel.org/doc/Documentation/cgroup-v2.txt)
func getCgroupPidsFile() (string, error) {
cgroup, err := os.Open(procCgroup)
if err != nil {
return "", err
}
defer cgroup.Close() // #nosec: error on close is not critical here

pidsMax := ""
scanner := bufio.NewScanner(cgroup)
var slice string
for scanner.Scan() {
parts := strings.SplitN(scanner.Text(), ":", 3)
if parts == nil || len(parts) < 3 {
continue
}
// No cgroup subsystem given, then it is cgroupv2
if parts[0] == "0" && parts[1] == "" {
slice = parts[2]
pidsMax = fmt.Sprintf(sysPidsMaxFmtCgroupV2, slice)

break
}
if parts[1] == "pids" {
slice = parts[2]
pidsMax = fmt.Sprintf(sysPidsMaxFmtCgroupV1, slice)

break
}
Expand All @@ -61,8 +73,6 @@ func getCgroupPidsFile() (string, error) {
return "", fmt.Errorf("could not find a cgroup for 'pids'")
}

pidsMax := fmt.Sprintf(sysPidsMaxFmt, slice)

return pidsMax, nil
}

Expand Down