Skip to content

Commit 6020ede

Browse files
authored
util/cgroup: fix InContainer for cgroup v2 (#48344)
close #48342
1 parent 662528d commit 6020ede

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

pkg/util/cgroup/cgroup_cpu_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func CPUQuotaToGOMAXPROCS(minValue int) (int, CPUQuotaStatus, error) {
5757

5858
// InContainer returns true if the process is running in a container.
5959
func InContainer() bool {
60-
v, err := os.ReadFile(procPathCGroup)
60+
// for cgroup V1, check /proc/self/cgroup, for V2, check /proc/self/mountinfo
61+
return inContainer(procPathCGroup) || inContainer(procPathMountInfo)
62+
}
63+
64+
func inContainer(path string) bool {
65+
v, err := os.ReadFile(path)
6166
if err != nil {
6267
return false
6368
}

0 commit comments

Comments
 (0)