Skip to content

Commit fce8746

Browse files
authored
Merge pull request #429 from lucaslorentz/improve-self-id-discovery
Improve logic to find self container id
2 parents 4a77bae + f923224 commit fce8746

File tree

2 files changed

+88
-75
lines changed

2 files changed

+88
-75
lines changed

docker/utils.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package docker
22

33
import (
4-
"errors"
5-
"fmt"
64
"io/ioutil"
75
"os"
86
"regexp"
@@ -23,29 +21,54 @@ func CreateUtils() Utils {
2321

2422
// GetCurrentContainerID returns the id of the container running this application
2523
func (wrapper *dockerUtils) GetCurrentContainerID() (string, error) {
26-
if runtime.GOOS == "windows" {
27-
return os.Hostname()
24+
var containerID string
25+
var err error
26+
if runtime.GOOS == "linux" {
27+
if containerID == "" && err == nil {
28+
containerID, err = wrapper.getCurrentContainerIDFromCGroup()
29+
}
30+
if containerID == "" && err == nil {
31+
containerID, err = wrapper.getCurrentContainerIDFromMountInfo()
32+
}
2833
}
34+
if containerID == "" && err == nil {
35+
containerID, err = os.Hostname()
36+
}
37+
return containerID, err
38+
}
2939

30-
bytes, err := ioutil.ReadFile("/proc/self/cgroup")
40+
func (wrapper *dockerUtils) getCurrentContainerIDFromMountInfo() (string, error) {
41+
bytes, err := ioutil.ReadFile("/proc/self/mountinfo")
3142
if err != nil {
3243
return "", err
3344
}
34-
if len(bytes) == 0 {
35-
return "", errors.New("Cannot read /proc/self/cgroup")
36-
}
45+
containerID := wrapper.extractContainerIDFromMountInfo(string(bytes))
46+
return containerID, nil
47+
}
3748

38-
return wrapper.ExtractContainerID(string(bytes))
49+
func (wrapper *dockerUtils) getCurrentContainerIDFromCGroup() (string, error) {
50+
bytes, err := ioutil.ReadFile("/proc/self/cgroup")
51+
if err != nil {
52+
return "", err
53+
}
54+
containerID := wrapper.extractContainerIDFromCGroups(string(bytes))
55+
return containerID, nil
56+
}
3957

58+
func (wrapper *dockerUtils) extractContainerIDFromMountInfo(cgroups string) string {
59+
idRegex := regexp.MustCompile(`containers/([[:alnum:]]{64})/`)
60+
matches := idRegex.FindStringSubmatch(cgroups)
61+
if len(matches) == 0 {
62+
return ""
63+
}
64+
return matches[len(matches)-1]
4065
}
4166

42-
func (wrapper *dockerUtils) ExtractContainerID(cgroups string) (string, error) {
67+
func (wrapper *dockerUtils) extractContainerIDFromCGroups(cgroups string) string {
4368
idRegex := regexp.MustCompile(`(?im)^[^:]*:[^:]*:.*\b([[:alnum:]]{64})\b`)
4469
matches := idRegex.FindStringSubmatch(cgroups)
45-
4670
if len(matches) == 0 {
47-
return "", fmt.Errorf("Cannot find container id in cgroups: %v", cgroups)
71+
return ""
4872
}
49-
50-
return matches[len(matches)-1], nil
73+
return matches[len(matches)-1]
5174
}

docker/utils_test.go

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,52 @@ package docker
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

9+
func TestExtratFromMountInfo(t *testing.T) {
10+
read :=
11+
`982 811 0:188 / / rw,relatime master:211 - overlay overlay rw,lowerdir=/var/lib/docker/overlay2/l/JITAD3AQIAPDR63API26SHX5CQ:/var/lib/docker/overlay2/l/OD4G2XK3EBQC7UCYC2MKN2VU4N:/var/lib/docker/overlay2/l/XCYRLTZ7FPAFABA6UPAECHCUFM:/var/lib/docker/overlay2/l/2UTXO3KIF3I7EQFKXPOYQO6WGN,upperdir=/var/lib/docker/overlay2/11a7a30cc374c98491c15185334a99f07e2761a9759c2c5b3ba1b4122ec9fbf7/diff,workdir=/var/lib/docker/overlay2/11a7a30cc374c98491c15185334a99f07e2761a9759c2c5b3ba1b4122ec9fbf7/work
12+
984 982 0:205 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
13+
986 982 0:207 / /dev rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755
14+
988 986 0:209 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=666
15+
990 982 0:211 / /sys ro,nosuid,nodev,noexec,relatime - sysfs sysfs ro
16+
993 990 0:33 / /sys/fs/cgroup ro,nosuid,nodev,noexec,relatime - cgroup2 cgroup rw
17+
994 986 0:202 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw
18+
996 986 0:213 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k
19+
999 982 254:1 /docker/containers/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b/resolv.conf /etc/resolv.conf rw,relatime - ext4 /dev/vda1 rw
20+
1001 982 254:1 /docker/containers/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b/hostname /etc/hostname rw,relatime - ext4 /dev/vda1 rw
21+
1002 982 254:1 /docker/containers/d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b/hosts /etc/hosts rw,relatime - ext4 /dev/vda1 rw
22+
1003 982 0:23 /host-services/docker.proxy.sock /run/docker.sock rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,size=608152k,mode=755
23+
576 984 0:205 /bus /proc/bus ro,nosuid,nodev,noexec,relatime - proc proc rw
24+
587 984 0:205 /fs /proc/fs ro,nosuid,nodev,noexec,relatime - proc proc rw
25+
588 984 0:205 /irq /proc/irq ro,nosuid,nodev,noexec,relatime - proc proc rw
26+
589 984 0:205 /sys /proc/sys ro,nosuid,nodev,noexec,relatime - proc proc rw
27+
590 984 0:205 /sysrq-trigger /proc/sysrq-trigger ro,nosuid,nodev,noexec,relatime - proc proc rw
28+
591 984 0:207 /null /proc/kcore rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755
29+
592 984 0:207 /null /proc/keys rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755
30+
593 984 0:207 /null /proc/timer_list rw,nosuid - tmpfs tmpfs rw,size=65536k,mode=755
31+
594 990 0:216 / /sys/firmware ro,relatime - tmpfs tmpfs ro`
32+
33+
expected := "d39fa516d8377ecddf9bf8ef33f81cbf58b4d604d85293ced7cdb0c7fc52442b"
34+
35+
utils := dockerUtils{}
36+
37+
actual := utils.extractContainerIDFromMountInfo(read)
38+
39+
assert.Equal(t, expected, actual)
40+
}
41+
742
func TestFailExtractBasicDockerId(t *testing.T) {
843
read :=
944
`1:cpu:/not_an_id`
1045

1146
utils := dockerUtils{}
1247

13-
actual, err := utils.ExtractContainerID(read)
14-
15-
if err == nil {
16-
t.Fatalf("Got unexpected container id %v", actual)
17-
}
48+
actual := utils.extractContainerIDFromCGroups(read)
1849

50+
assert.Empty(t, actual)
1951
}
2052

2153
func TestExtractBasicDockerId(t *testing.T) {
@@ -30,15 +62,9 @@ func TestExtractBasicDockerId(t *testing.T) {
3062

3163
utils := dockerUtils{}
3264

33-
actual, err := utils.ExtractContainerID(read)
34-
35-
if err != nil {
36-
t.Fatalf("Could not extract container id : %v", err)
37-
}
65+
actual := utils.extractContainerIDFromCGroups(read)
3866

39-
if actual != expected {
40-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
41-
}
67+
assert.Equal(t, expected, actual)
4268
}
4369

4470
func TestExtractScopedDockerId(t *testing.T) {
@@ -53,15 +79,9 @@ func TestExtractScopedDockerId(t *testing.T) {
5379

5480
utils := dockerUtils{}
5581

56-
actual, err := utils.ExtractContainerID(read)
82+
actual := utils.extractContainerIDFromCGroups(read)
5783

58-
if err != nil {
59-
t.Fatalf("Could not extract container id : %v", err)
60-
}
61-
62-
if actual != expected {
63-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
64-
}
84+
assert.Equal(t, expected, actual)
6585
}
6686

6787
func TestExtractSlashPrefixedDockerId(t *testing.T) {
@@ -76,15 +96,9 @@ func TestExtractSlashPrefixedDockerId(t *testing.T) {
7696

7797
utils := dockerUtils{}
7898

79-
actual, err := utils.ExtractContainerID(read)
80-
81-
if err != nil {
82-
t.Fatalf("Could not extract container id : %v", err)
83-
}
99+
actual := utils.extractContainerIDFromCGroups(read)
84100

85-
if actual != expected {
86-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
87-
}
101+
assert.Equal(t, expected, actual)
88102
}
89103

90104
func TestExtractNestedDockerId(t *testing.T) {
@@ -105,15 +119,9 @@ func TestExtractNestedDockerId(t *testing.T) {
105119

106120
utils := dockerUtils{}
107121

108-
actual, err := utils.ExtractContainerID(read)
122+
actual := utils.extractContainerIDFromCGroups(read)
109123

110-
if err != nil {
111-
t.Fatalf("Could not extract container id : %v", err)
112-
}
113-
114-
if actual != expected {
115-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
116-
}
124+
assert.Equal(t, expected, actual)
117125
}
118126

119127
func TestExtractAKSDockerId(t *testing.T) {
@@ -136,15 +144,9 @@ func TestExtractAKSDockerId(t *testing.T) {
136144

137145
utils := dockerUtils{}
138146

139-
actual, err := utils.ExtractContainerID(read)
140-
141-
if err != nil {
142-
t.Fatalf("Could not extract container id : %v", err)
143-
}
147+
actual := utils.extractContainerIDFromCGroups(read)
144148

145-
if actual != expected {
146-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
147-
}
149+
assert.Equal(t, expected, actual)
148150
}
149151

150152
func TestExtractECSDockerId(t *testing.T) {
@@ -164,15 +166,9 @@ func TestExtractECSDockerId(t *testing.T) {
164166

165167
utils := dockerUtils{}
166168

167-
actual, err := utils.ExtractContainerID(read)
169+
actual := utils.extractContainerIDFromCGroups(read)
168170

169-
if err != nil {
170-
t.Fatalf("Could not extract container id : %v", err)
171-
}
172-
173-
if actual != expected {
174-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
175-
}
171+
assert.Equal(t, expected, actual)
176172
}
177173

178174
func TestExtractRootlessDockerId(t *testing.T) {
@@ -195,13 +191,7 @@ func TestExtractRootlessDockerId(t *testing.T) {
195191

196192
utils := dockerUtils{}
197193

198-
actual, err := utils.ExtractContainerID(read)
199-
200-
if err != nil {
201-
t.Fatalf("Could not extract container id : %v", err)
202-
}
194+
actual := utils.extractContainerIDFromCGroups(read)
203195

204-
if actual != expected {
205-
t.Fatalf("id mismatch: actual %v, expected %v", actual, expected)
206-
}
196+
assert.Equal(t, expected, actual)
207197
}

0 commit comments

Comments
 (0)