Skip to content

Commit 0e1c96a

Browse files
committed
add cross-platform declaration for IsContainerized()
1 parent 2ba7392 commit 0e1c96a

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

cli/clistat/container.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !linux
2+
3+
package clistat
4+
5+
// IsContainerized returns whether the host is containerized.
6+
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
7+
// with modifications to support Sysbox containers.
8+
// On non-Linux platforms, it always returns false.
9+
func IsContainerized() (bool, error) {
10+
return false, nil
11+
}

cli/clistat/container_linux.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package clistat
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"os"
7+
8+
"golang.org/x/xerrors"
9+
)
10+
11+
// IsContainerized returns whether the host is containerized.
12+
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
13+
// with modifications to support Sysbox containers.
14+
// On non-Linux platforms, it always returns false.
15+
func IsContainerized() (bool, error) {
16+
data, err := os.ReadFile(procOneCgroup)
17+
if err != nil {
18+
if os.IsNotExist(err) { // how?
19+
return false, nil
20+
}
21+
return false, xerrors.Errorf("read process cgroups: %w", err)
22+
}
23+
24+
s := bufio.NewScanner(bytes.NewReader(data))
25+
for s.Scan() {
26+
line := s.Bytes()
27+
if bytes.Contains(line, []byte("docker")) ||
28+
bytes.Contains(line, []byte(".slice")) ||
29+
bytes.Contains(line, []byte("lxc")) ||
30+
bytes.Contains(line, []byte("kubepods")) {
31+
return true, nil
32+
}
33+
}
34+
35+
// Last-ditch effort to detect Sysbox containers.
36+
// Check if we have anything mounted as type sysboxfs in /proc/mounts
37+
data, err = os.ReadFile("/proc/mounts")
38+
if err != nil {
39+
return false, xerrors.Errorf("read /proc/mounts: %w", err)
40+
}
41+
42+
s = bufio.NewScanner(bytes.NewReader(data))
43+
for s.Scan() {
44+
line := s.Bytes()
45+
if bytes.HasPrefix(line, []byte("sysboxfs")) {
46+
return true, nil
47+
}
48+
}
49+
50+
// If we get here, we are _probably_ not running in a container.
51+
return false, nil
52+
}

cli/clistat/stat.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package clistat
22

33
import (
4-
"bufio"
5-
"bytes"
6-
"os"
74
"runtime"
85
"strconv"
96
"strings"
@@ -156,45 +153,3 @@ func (s *Statter) ContainerCPU() (*Result, error) {
156153
func (s *Statter) ContainerMemory() (*Result, error) {
157154
return nil, xerrors.Errorf("not implemented")
158155
}
159-
160-
// IsContainerized returns whether the host is containerized.
161-
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
162-
// with modifications to support Sysbox containers.
163-
func IsContainerized() (bool, error) {
164-
data, err := os.ReadFile(procOneCgroup)
165-
if err != nil {
166-
if os.IsNotExist(err) { // how?
167-
return false, nil
168-
}
169-
return false, xerrors.Errorf("read process cgroups: %w", err)
170-
}
171-
172-
s := bufio.NewScanner(bytes.NewReader(data))
173-
for s.Scan() {
174-
line := s.Bytes()
175-
if bytes.Contains(line, []byte("docker")) ||
176-
bytes.Contains(line, []byte(".slice")) ||
177-
bytes.Contains(line, []byte("lxc")) ||
178-
bytes.Contains(line, []byte("kubepods")) {
179-
return true, nil
180-
}
181-
}
182-
183-
// Last-ditch effort to detect Sysbox containers.
184-
// Check if we have anything mounted as type sysboxfs in /proc/mounts
185-
data, err = os.ReadFile("/proc/mounts")
186-
if err != nil {
187-
return false, xerrors.Errorf("read /proc/mounts: %w", err)
188-
}
189-
190-
s = bufio.NewScanner(bytes.NewReader(data))
191-
for s.Scan() {
192-
line := s.Bytes()
193-
if bytes.HasPrefix(line, []byte("sysboxfs")) {
194-
return true, nil
195-
}
196-
}
197-
198-
// If we get here, we are _probably_ not running in a container.
199-
return false, nil
200-
}

0 commit comments

Comments
 (0)