Skip to content

Commit

Permalink
Merge pull request #212 from lucaslorentz/feature/id-extract-rootless…
Browse files Browse the repository at this point in the history
…-docker

Improve ID extraction to work on rootless docker
  • Loading branch information
lucaslorentz authored Jan 23, 2021
2 parents 395b0a2 + 931eee1 commit 1284096
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugin/docker/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (wrapper *dockerUtils) GetCurrentContainerID() (string, error) {
}

func (wrapper *dockerUtils) ExtractContainerID(cgroups string) (string, error) {
idRegex := regexp.MustCompile(`(?i):[^:]*\bcpu\b[^:]*:[^/]*/.*([[:alnum:]]{64}).*`)
idRegex := regexp.MustCompile(`(?im)^[^:]*:[^:]*:.*\b([[:alnum:]]{64})\b`)
matches := idRegex.FindStringSubmatch(cgroups)

if len(matches) == 0 {
Expand Down
38 changes: 32 additions & 6 deletions plugin/docker/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import (

func TestFailExtractBasicDockerId(t *testing.T) {
read :=
`5:cpu,cpuacct:/system.slice/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442.scope
4:cpu,cpuacct:/system.slice/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442
3:zpu,cpuacct:/system.slice/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b
2:cpu,cpuacct:system.slice:d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b
1:cpu,cpuacct:/system.slice/d39fa516d8377ecddf9bf8ef33f81cbf5 8b4d604d85293ced7cdb0c7fc52442b.scope
`
`1:cpu:/not_an_id`

utils := dockerUtils{}

Expand Down Expand Up @@ -179,3 +174,34 @@ func TestExtractECSDockerId(t *testing.T) {
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
}
}

func TestExtractRootlessDockerId(t *testing.T) {
read :=
`11:rdma:/
10:freezer:/
9:cpuset:/
8:net_cls,net_prio:/
7:cpu,cpuacct:/
6:devices:/user.slice
5:memory:/user.slice/user-1000.slice/[email protected]
4:perf_event:/
3:pids:/user.slice/user-1000.slice/[email protected]
2:blkio:/
1:name=systemd:/user.slice/user-1000.slice/[email protected]/docker.service/f7df0c0b3a8d4350647486b24a5bd5785d494c1a0910cfaee66d3db0db784093
0::/user.slice/user-1000.slice/[email protected]/docker.service
`

expected := "f7df0c0b3a8d4350647486b24a5bd5785d494c1a0910cfaee66d3db0db784093"

utils := dockerUtils{}

actual, err := utils.ExtractContainerID(read)

if err != nil {
t.Fatalf("Could not extract container id : %v", err)
}

if actual != expected {
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
}
}

0 comments on commit 1284096

Please sign in to comment.