Skip to content
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

Recognize systemd based cgroups #71

Merged
merged 3 commits into from
Nov 17, 2019
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added detection of systemd cgroups to the `IsContainerized` check. [#71](https://github.com/elastic/go-sysinfo/pull/71)

### Changed

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion providers/linux/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func isContainerizedCgroup(data []byte) (bool, error) {

// Following a suggestion on Stack Overflow on how to detect
// being inside a container: https://stackoverflow.com/a/20012536/235203
if bytes.Contains(line, []byte("docker")) || bytes.Contains(line, []byte("lxc")) {
if bytes.Contains(line, []byte("docker")) || bytes.Contains(line, []byte(".slice")) || bytes.Contains(line, []byte("lxc")) {
return true, nil
}
}
Expand Down
73 changes: 47 additions & 26 deletions providers/linux/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,57 @@ const lxcCgroup = `9:hugetlb:/lxc/81438f4655cd771c425607dcf7654f4dc03c073c0123ed
2:cpu:/lxc/81438f4655cd771c425607dcf7654f4dc03c073c0123edc45fcfad28132e8c60
1:cpuset:/lxc/81438f4655cd771c425607dcf7654f4dc03c073c0123edc45fcfad28132e8c60`

const systemdCgroup = `12:hugetlb:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
11:perf_event:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
10:pids:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
9:cpu,cpuacct:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
8:cpuset:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
7:memory:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
6:freezer:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
5:rdma:/
4:net_cls,net_prio:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
3:devices:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
2:blkio:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1
1:name=systemd:/kubepods.slice/kubepods-podc1281d63_01ab_11ea_ba0a_3cfdfe55a1c0.slice/e2b68f8a6e227921b236c686a243e8ff50f561f493d401da7ac3f8cae28f08b1`

const emptyCgroup = ``

func TestIsContainerized(t *testing.T) {
containerized, err := isContainerizedCgroup([]byte(nonContainerizedCgroup))
if err != nil {
t.Fatal(err)
}
assert.False(t, containerized)

containerized, err = isContainerizedCgroup([]byte(containerCgroup))
if err != nil {
t.Fatal(err)
}
assert.True(t, containerized)

containerized, err = isContainerizedCgroup([]byte(containerHostPIDNamespaceCgroup))
if err != nil {
t.Fatal(err)
}
assert.False(t, containerized)

containerized, err = isContainerizedCgroup([]byte(lxcCgroup))
if err != nil {
t.Fatal(err)
tests := []struct {
cgroupStr string
containerized bool
}{
{
cgroupStr: nonContainerizedCgroup,
containerized: false,
},
{
cgroupStr: containerCgroup,
containerized: true,
},
{
cgroupStr: containerHostPIDNamespaceCgroup,
containerized: false,
},
{
cgroupStr: lxcCgroup,
containerized: true,
},
{
cgroupStr: systemdCgroup,
containerized: true,
},
{
cgroupStr: emptyCgroup,
containerized: false,
},
}
assert.True(t, containerized)

containerized, err = isContainerizedCgroup([]byte(emptyCgroup))
if err != nil {
t.Fatal(err)
for _, test := range tests {
containerized, err := isContainerizedCgroup([]byte(test.cgroupStr))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.containerized, containerized)
}
assert.False(t, containerized)
}
8 changes: 3 additions & 5 deletions providers/linux/machineid.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import (
"github.com/elastic/go-sysinfo/types"
)

var (
// Possible (current and historic) locations of the machine-id file.
// These will be searched in order.
machineIDFiles = []string{"/etc/machine-id", "/var/lib/dbus/machine-id", "/var/db/dbus/machine-id"}
)
// Possible (current and historic) locations of the machine-id file.
// These will be searched in order.
var machineIDFiles = []string{"/etc/machine-id", "/var/lib/dbus/machine-id", "/var/db/dbus/machine-id"}

func MachineID() (string, error) {
var contents []byte
Expand Down