Skip to content

fix(cli/clistat): improve detection of container environment #8643

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

Merged
merged 2 commits into from
Jul 21, 2023
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
2 changes: 1 addition & 1 deletion cli/clistat/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func readInt64Prefix(fs afero.Fs, path, prefix string) (int64, error) {

scn := bufio.NewScanner(bytes.NewReader(data))
for scn.Scan() {
line := scn.Text()
line := strings.TrimSpace(scn.Text())
if !strings.HasPrefix(line, prefix) {
continue
}
Expand Down
13 changes: 11 additions & 2 deletions cli/clistat/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

const (
procMounts = "/proc/mounts"
procOneCgroup = "/proc/1/cgroup"
procMounts = "/proc/mounts"
procOneCgroup = "/proc/1/cgroup"
kubernetesDefaultServiceAccountToken = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint:gosec
)

// IsContainerized returns whether the host is containerized.
Expand All @@ -38,6 +39,14 @@ func IsContainerized(fs afero.Fs) (ok bool, err error) {
}
}

// Sometimes the above method of sniffing /proc/1/cgroup isn't reliable.
// If a Kubernetes service account token is present, that's
// also a good indication that we are in a container.
_, err = afero.ReadFile(fs, kubernetesDefaultServiceAccountToken)
if err == nil {
return true, nil
}

// Last-ditch effort to detect Sysbox containers.
// Check if we have anything mounted as type sysboxfs in /proc/mounts
mountsData, err := afero.ReadFile(fs, procMounts)
Expand Down