Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

(DI-755) Update GetLocalContainerID to work in DIND

This commit updates the GetLocalContainerID function and its tests to
handle the extraction of the local container ID when Lumogon is running
within a nested Docker engine, Docker-in-Docker for example.

It does so by extracting the right-most id match in `/proc/self/cgroup`.
  • Loading branch information...
johnmccabe committed Sep 27, 2017
1 parent 1d654aa commit d9068f24bfa7944b7b9f5e463e9d285b393ad526
Showing with 21 additions and 5 deletions.
  1. +11 −0 utils/fixtures/cgroup.dind
  2. +1 −1 utils/getlocalcontainerid.go
  3. +9 −4 utils/getlocalcontainerid_test.go
@@ -0,0 +1,11 @@
11:devices:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
10:memory:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
9:hugetlb:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
8:cpuset:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
7:net_prio,net_cls:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
6:perf_event:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
5:blkio:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
4:pids:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
3:freezer:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
2:cpuacct,cpu:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
1:name=systemd:/docker/5462b001b4b4253ac2ee52b27b04a821e48f369ed2c923b958995e6e51c496b4/docker/2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5
@@ -15,7 +15,7 @@ func GetLocalContainerID(cgroupfile string) (string, error) {
}
defer file.Close()

re := regexp.MustCompile(`^\d+:cpu.*:/docker/(\S+)$`)
re := regexp.MustCompile(`^\d+:cpu.*/docker/(\S+)$`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
matches := re.FindStringSubmatch(scanner.Text())
@@ -22,6 +22,12 @@ var getLocalContainerIDTests = []struct {
expectedContainerID: "c7acb1eb27bb163d07c2ce0a3e7deb36a698824472f21074662e605bf57c7521",
expectError: false,
},
{
title: "nested container running in Docker-in-Docker",
cgroupfile: "./fixtures/cgroup.dind",
expectedContainerID: "2ea749928409f83bd7af82f2d520f56345c99a4fcf5d0642cc99bd5cbc8168d5",
expectError: false,
},
{
title: "missingCgroupFile",
cgroupfile: "./fixtures/cgroup.doesnotexist",
@@ -56,10 +62,9 @@ func Test_GetLocalContainerID(t *testing.T) {
if !test.expectError {
t.Errorf("Test [%s] threw unexpected error [%s]", test.title, err)
}
if err == nil {
if actualContainerID != test.expectedContainerID {
t.Errorf("Extracted containerID [%s] does not match expected value [%s]", actualContainerID, test.expectedContainerID)
}
} else {
if actualContainerID != test.expectedContainerID {
t.Errorf("Extracted containerID [%s] does not match expected value [%s]", actualContainerID, test.expectedContainerID)
}
}
})

0 comments on commit d9068f2

Please sign in to comment.
You can’t perform that action at this time.