From 080f17dbb4bd9f052e43e7037a51f3d1732ede20 Mon Sep 17 00:00:00 2001 From: DrAuYueng Date: Sat, 25 May 2024 14:23:25 +0800 Subject: [PATCH] Parse container id --- pkg/procfs/procfs.go | 9 ++------- pkg/tracejob/selected_target.go | 9 +++++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/procfs/procfs.go b/pkg/procfs/procfs.go index d297a11..f065179 100644 --- a/pkg/procfs/procfs.go +++ b/pkg/procfs/procfs.go @@ -51,13 +51,8 @@ func FindPidByPodContainer(podUID, containerID string) (string, error) { // See https://github.com/kubernetes/kubernetes/blob/2f3a4ec9cb96e8e2414834991d63c59988c3c866/pkg/kubelet/cm/cgroup_manager_linux.go#L81-L85 // Note that these identifiers are currently specific to systemd, however, this mounting approach is what allows us to find the containerized // process. - // - // EG: /kubepods/burstable/pod31dd0274-bb43-4975-bdbc-7e10047a23f8/851c75dad6ad8ce6a5d9b9129a4eb1645f7c6e5ba8406b12d50377b665737072 - // /kubepods/burstable/pod{POD_ID}/{CONTAINER_ID} - // - // This "needle" that we look for in the mountinfo haystack should match one and only one container. - needle := path.Join(podUID, containerID) - if strings.Contains(root, needle) { + // EG: /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod40934a46_d409_4f4a_bb8b_1ec0b0436320.slice/cri-containerd-afc114c69e71f18166abd63715398d7daa763c69a6454f71157272ab0bdca783.scope + if strings.Contains(root, podUID) && strings.Contains(root, containerID) { return dname, nil } } diff --git a/pkg/tracejob/selected_target.go b/pkg/tracejob/selected_target.go index 141f61d..a0d1869 100644 --- a/pkg/tracejob/selected_target.go +++ b/pkg/tracejob/selected_target.go @@ -277,8 +277,13 @@ func resolvePodToTarget(podClient corev1.PodInterface, resourceID, container, ta for _, s := range pod.Status.ContainerStatuses { if s.Name == targetContainer { - containerID := strings.TrimPrefix(s.ContainerID, "docker://") - containerID = strings.TrimPrefix(containerID, "containerd://") + // Trim the quotes and split the type and ID. + // See https://github.com/kubernetes/kubernetes/blob/ec93d3b71a6634659b59bf435959f4befafe3796/pkg/kubelet/container/runtime.go#L234 + parts := strings.Split(strings.Trim(s.ContainerID, "\""), "://") + if len(parts) != 2 { + return fmt.Errorf("invalid container ID: %q", s.ContainerID) + } + containerID := parts[1] target.ContainerID = containerID break }