Skip to content

Commit

Permalink
Fix container.id issue in some crio scenarios (#11382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui Liu authored and opentelemetrybot committed May 20, 2024
1 parent 8d19825 commit 46e7898
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class CgroupV2ContainerIdExtractor {

static final Path V2_CGROUP_PATH = Paths.get("/proc/self/mountinfo");
private static final Pattern CONTAINER_ID_RE = Pattern.compile("^[0-9a-f]{64}$");
private static final Pattern CRI_CONTAINER_ID_RE = Pattern.compile("cri-containerd:[0-9a-f]{64}");
private static final Pattern CONTAINERD_CONTAINER_ID_RE =
Pattern.compile("cri-containerd:[0-9a-f]{64}");
private static final Pattern CRIO_CONTAINER_ID_RE = Pattern.compile("\\/crio-[0-9a-f]{64}");

private final ContainerResource.Filesystem filesystem;

Expand Down Expand Up @@ -54,20 +56,32 @@ Optional<String> extractContainerId() {

Optional<String> optCid =
fileAsList.stream()
.filter(line -> line.contains("/containers/"))
.flatMap(line -> Stream.of(line.split("/")))
.map(CONTAINER_ID_RE::matcher)
.filter(Matcher::matches)
.reduce((first, second) -> second)
.map(matcher -> matcher.group(0));
.filter(line -> line.contains("/crio-"))
.map(CRIO_CONTAINER_ID_RE::matcher)
.filter(Matcher::find)
.findFirst()
.map(matcher -> matcher.group(0).substring(6));
if (optCid.isPresent()) {
return optCid;
}

optCid =
fileAsList.stream()
.filter(line -> line.contains("cri-containerd:"))
.map(CONTAINERD_CONTAINER_ID_RE::matcher)
.filter(Matcher::find)
.findFirst()
.map(matcher -> matcher.group(0).substring(15));
if (optCid.isPresent()) {
return optCid;
}

return fileAsList.stream()
.filter(line -> line.contains("cri-containerd:"))
.map(CRI_CONTAINER_ID_RE::matcher)
.filter(Matcher::find)
.findFirst()
.map(matcher -> matcher.group(0).substring(15));
.filter(line -> line.contains("/containers/"))
.flatMap(line -> Stream.of(line.split("/")))
.map(CONTAINER_ID_RE::matcher)
.filter(Matcher::matches)
.reduce((first, second) -> second)
.map(matcher -> matcher.group(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ void extractSuccess_crio1() throws Exception {
"f23ec1d4b715c6531a17e9c549222fbbe1f7ffff697a29a2212b3b4cdc37f52e");
}

@Test
void extractSuccess_crio2() throws Exception {
verifyContainerId(
"crio_proc_self_mountinfo2",
"b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6");
}

private static List<String> fileToListOfLines(String filename) {
InputStream in =
CgroupV2ContainerIdExtractorTest.class.getClassLoader().getResourceAsStream(filename);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
5989 5586 0:433 / / rw,relatime - overlay overlay rw,context="system_u:object_r:container_file_t:s0:c898,c999",lowerdir=/var/lib/containers/storage/overlay/l/6L7GBRDSDQ5M2S7KRI6X24TWEK:/var/lib/containers/storage/overlay/l/BDMEMSU6EKIVTDHY6DPE6ERXTU:/var/lib/containers/storage/overlay/l/I64YWGP3Z4E6ZFBAKJS22PN5R7,upperdir=/var/lib/containers/storage/overlay/92e5b09335dc1206f36bc26dfc2c6672082b02fc6eb21f8c38334523fdef835e/diff,workdir=/var/lib/containers/storage/overlay/92e5b09335dc1206f36bc26dfc2c6672082b02fc6eb21f8c38334523fdef835e/work,volatile
5990 5989 0:437 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
5991 5989 0:438 / /dev rw,nosuid - tmpfs tmpfs rw,context="system_u:object_r:container_file_t:s0:c898,c999",size=65536k,mode=755,inode64
5992 5991 0:439 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,context="system_u:object_r:container_file_t:s0:c898,c999",gid=5,mode=620,ptmxmode=666
5993 5991 0:316 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw,seclabel
5994 5989 0:440 / /sys ro,nosuid,nodev,noexec,relatime - sysfs sysfs ro,seclabel
5995 5994 0:441 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,context="system_u:object_r:container_file_t:s0:c898,c999",mode=755,inode64
5996 5995 0:27 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/systemd ro,nosuid,nodev,noexec,relatime master:9 - cgroup cgroup rw,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd
5997 5995 0:30 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/blkio ro,nosuid,nodev,noexec,relatime master:10 - cgroup cgroup rw,seclabel,blkio
5998 5995 0:31 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/net_cls,net_prio ro,nosuid,nodev,noexec,relatime master:11 - cgroup cgroup rw,seclabel,net_cls,net_prio
5999 5995 0:32 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/rdma ro,nosuid,nodev,noexec,relatime master:12 - cgroup cgroup rw,seclabel,rdma
6000 5995 0:33 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/memory ro,nosuid,nodev,noexec,relatime master:13 - cgroup cgroup rw,seclabel,memory
6001 5995 0:34 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/cpuset ro,nosuid,nodev,noexec,relatime master:14 - cgroup cgroup rw,seclabel,cpuset
6002 5995 0:35 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/pids ro,nosuid,nodev,noexec,relatime master:15 - cgroup cgroup rw,seclabel,pids
6003 5995 0:36 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/hugetlb ro,nosuid,nodev,noexec,relatime master:16 - cgroup cgroup rw,seclabel,hugetlb
6004 5995 0:37 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/freezer ro,nosuid,nodev,noexec,relatime master:17 - cgroup cgroup rw,seclabel,freezer
6005 5995 0:38 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/perf_event ro,nosuid,nodev,noexec,relatime master:18 - cgroup cgroup rw,seclabel,perf_event
6006 5995 0:39 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/cpu,cpuacct ro,nosuid,nodev,noexec,relatime master:19 - cgroup cgroup rw,seclabel,cpu,cpuacct
6007 5995 0:40 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/devices ro,nosuid,nodev,noexec,relatime master:20 - cgroup cgroup rw,seclabel,devices
6008 5995 0:41 /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod19ccacf5_fcff_4bd3_a33a_19ff41f6ccd2.slice/crio-b4873629b312dc1d77472aba6fb177c6ce9a8f7c205ad7a03302726805007fe6.scope /sys/fs/cgroup/misc ro,nosuid,nodev,noexec,relatime master:21 - cgroup cgroup rw,seclabel,misc
6009 5991 0:315 / /dev/shm rw,nosuid,nodev,noexec,relatime master:766 - tmpfs shm rw,context="system_u:object_r:container_file_t:s0:c898,c999",size=65536k,inode64
6010 5989 0:25 /containers/storage/overlay-containers/eff9d85aabab6d1fd3a244b1f22c6545132ef459ee6e4ead13f7abc2c963e6dc/userdata/resolv.conf /etc/resolv.conf rw,nosuid,nodev,noexec master:28 - tmpfs tmpfs rw,seclabel,size=6418660k,nr_inodes=819200,mode=755,inode64
6011 5989 0:25 /containers/storage/overlay-containers/eff9d85aabab6d1fd3a244b1f22c6545132ef459ee6e4ead13f7abc2c963e6dc/userdata/hostname /etc/hostname rw,nosuid,nodev master:28 - tmpfs tmpfs rw,seclabel,size=6418660k,nr_inodes=819200,mode=755,inode64
6012 5989 0:25 /containers/storage/overlay-containers/eff9d85aabab6d1fd3a244b1f22c6545132ef459ee6e4ead13f7abc2c963e6dc/userdata/.containerenv /run/.containerenv rw,nosuid,nodev master:28 - tmpfs tmpfs rw,seclabel,size=6418660k,nr_inodes=819200,mode=755,inode64
6013 5989 252:4 /ostree/deploy/rhcos/var/lib/kubelet/pods/19ccacf5-fcff-4bd3-a33a-19ff41f6ccd2/etc-hosts /etc/hosts rw,relatime - xfs /dev/vda4 rw,seclabel,attr2,inode64,logbufs=8,logbsize=32k,prjquota
6014 5991 252:4 /ostree/deploy/rhcos/var/lib/kubelet/pods/19ccacf5-fcff-4bd3-a33a-19ff41f6ccd2/containers/accountingservice/640fe7ac /dev/termination-log rw,relatime - xfs /dev/vda4 rw,seclabel,attr2,inode64,logbufs=8,logbsize=32k,prjquota
6015 5989 0:307 / /run/secrets/kubernetes.io/serviceaccount ro,relatime - tmpfs tmpfs rw,seclabel,size=30942320k,inode64
5587 5990 0:437 /bus /proc/bus ro,nosuid,nodev,noexec,relatime - proc proc rw
5588 5990 0:437 /fs /proc/fs ro,nosuid,nodev,noexec,relatime - proc proc rw
5590 5990 0:437 /irq /proc/irq ro,nosuid,nodev,noexec,relatime - proc proc rw
5591 5990 0:437 /sys /proc/sys ro,nosuid,nodev,noexec,relatime - proc proc rw
5592 5990 0:437 /sysrq-trigger /proc/sysrq-trigger ro,nosuid,nodev,noexec,relatime - proc proc rw
5593 5990 0:442 / /proc/acpi ro,relatime - tmpfs tmpfs ro,context="system_u:object_r:container_file_t:s0:c898,c999",inode64
5594 5990 0:438 /null /proc/kcore rw,nosuid - tmpfs tmpfs rw,context="system_u:object_r:container_file_t:s0:c898,c999",size=65536k,mode=755,inode64
5595 5990 0:438 /null /proc/keys rw,nosuid - tmpfs tmpfs rw,context="system_u:object_r:container_file_t:s0:c898,c999",size=65536k,mode=755,inode64
5596 5990 0:438 /null /proc/timer_list rw,nosuid - tmpfs tmpfs rw,context="system_u:object_r:container_file_t:s0:c898,c999",size=65536k,mode=755,inode64
5597 5990 0:443 / /proc/scsi ro,relatime - tmpfs tmpfs ro,context="system_u:object_r:container_file_t:s0:c898,c999",inode64
5598 5994 0:444 / /sys/firmware ro,relatime - tmpfs tmpfs ro,context="system_u:object_r:container_file_t:s0:c898,c999",inode64

0 comments on commit 46e7898

Please sign in to comment.