Merge pull request #39471 from crosbymichael/parse-cgroups

Enhance container detection on some corner cases (carry #36038)
This commit is contained in:
Yong Tang 2019-07-09 10:30:21 -07:00 committed by GitHub
commit d118a08292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -84,7 +84,7 @@ func IsContainerized() (bool, error) {
return false, err
}
for _, line := range bytes.Split(b, []byte{'\n'}) {
if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) && !bytes.HasSuffix(line, []byte("init.scope")) {
if len(line) > 0 && !bytes.HasSuffix(line, []byte(":/")) && !bytes.HasSuffix(line, []byte(":/init.scope")) {
return true, nil
}
}

View file

@ -215,6 +215,9 @@ func TestIsContainerized(t *testing.T) {
3:cpuacct:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
2:cpu:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
1:cpuset:/`)
nonContainerizedProc1CgroupNotSystemd = []byte(`9:memory:/not/init.scope
1:name=not_systemd:/not.init.scope
`)
)
dir := os.TempDir()
@ -247,6 +250,17 @@ func TestIsContainerized(t *testing.T) {
t.Fatal("Wrongly assuming containerized for systemd /init.scope cgroup layout")
}
if err := ioutil.WriteFile(proc1Cgroup, nonContainerizedProc1CgroupNotSystemd, 0600); err != nil {
t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
}
inContainer, err = IsContainerized()
if err != nil {
t.Fatal(err)
}
if !inContainer {
t.Fatal("Wrongly assuming non-containerized")
}
if err := ioutil.WriteFile(proc1Cgroup, containerizedProc1Cgroup, 0600); err != nil {
t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
}