Skip to content

Commit fa1f681

Browse files
authored
[CONTINT-3937] Fix origin detection when the cgroup namespace is shared but is not host cgroup ns
2 parents 9d92025 + 6684f58 commit fa1f681

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

statsd/container_linux.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,22 @@ func readCIDOrInode(userProvidedID, cgroupPath, selfMountInfoPath, defaultCgroup
198198
}
199199

200200
if cgroupFallback {
201-
if isHostCgroupNs {
202-
containerID = readContainerID(cgroupPath)
201+
containerID = readContainerID(cgroupPath)
202+
if containerID != "" {
203203
return
204204
}
205+
205206
containerID = readMountinfo(selfMountInfoPath)
206-
if containerID == "" {
207-
containerID = getCgroupInode(defaultCgroupMountPath, cgroupPath)
207+
if containerID != "" {
208+
return
209+
}
210+
211+
// If we're in the host cgroup namespace, the cid should be retrievable in /proc/self/cgroup
212+
// In private cgroup namespace, we can retrieve the cgroup controller inode.
213+
if containerID == "" && isHostCgroupNs {
214+
return
208215
}
216+
217+
containerID = getCgroupInode(defaultCgroupMountPath, cgroupPath)
209218
}
210219
}

statsd/container_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,18 +416,34 @@ func TestReadCIDOrInode(t *testing.T) {
416416
isHostCgroupNs: true,
417417
expectedResult: "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", // Will be formatted with inode number
418418
},
419-
419+
{
420+
description: "extract container-id from /proc/self/cgroup in private cgroup ns",
421+
procSelfCgroupContent: "4:blkio:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa\n",
422+
expectedResult: "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", // Will be formatted with inode number
423+
},
424+
{
425+
description: "extract container-id from mountinfo in private cgroup ns",
426+
mountInfoContent: "2282 2269 8:1 /var/lib/containerd/io.containerd.grpc.v1.cri/sandboxes/c0a82a3506b0366c9666f6dbe71c783abeb26ba65e312e918a49e10a277196d0/hostname /host/var/run/containerd/io.containerd.runtime.v2.task/k8s.io/fc7038bc73a8d3850c66ddbfb0b2901afa378bfcbb942cc384b051767e4ac6b0/rootfs/etc/hostname rw,nosuid,nodev,relatime - ext4 /dev/sda1 rw,commit=30\n",
427+
expectedResult: "fc7038bc73a8d3850c66ddbfb0b2901afa378bfcbb942cc384b051767e4ac6b0",
428+
},
420429
{
421430
description: "extract container-id from mountinfo",
422431
mountInfoContent: "2282 2269 8:1 /var/lib/containerd/io.containerd.grpc.v1.cri/sandboxes/c0a82a3506b0366c9666f6dbe71c783abeb26ba65e312e918a49e10a277196d0/hostname /host/var/run/containerd/io.containerd.runtime.v2.task/k8s.io/fc7038bc73a8d3850c66ddbfb0b2901afa378bfcbb942cc384b051767e4ac6b0/rootfs/etc/hostname rw,nosuid,nodev,relatime - ext4 /dev/sda1 rw,commit=30\n",
423432
expectedResult: "fc7038bc73a8d3850c66ddbfb0b2901afa378bfcbb942cc384b051767e4ac6b0",
433+
isHostCgroupNs: true,
424434
},
425435
{
426-
description: "extract inode",
436+
description: "extract inode only in private cgroup ns",
427437
cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope",
428438
procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n",
429439
expectedResult: "in-%d",
430440
},
441+
{
442+
description: "do not extract inode in host cgroup ns",
443+
cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope",
444+
procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n",
445+
isHostCgroupNs: true,
446+
},
431447
}
432448

433449
for _, tc := range tests {

0 commit comments

Comments
 (0)