Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(cli/clistat): improve detection of container environment
  • Loading branch information
johnstcn committed Jul 21, 2023
commit f2c1c515d40ef82db9e62ef7fbeb91893f861c2e
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"
)

// 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