Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse container id #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions pkg/procfs/procfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

@cakemanny cakemanny Jun 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which setup have you tested this with?
I also found that I needed to fix this when working on my PR: https://github.com/iovisor/kubectl-trace/pull/205/files#diff-3fc753f2ae105df1b499d656901818c3aae006a40a0d807d4871520a19d612a5

I found podUID contained -s in place of _s in the cgroup name.
i.e. in your example, I'd expect podUID to be "40934a46-d409-4f4a-bb8b-1ec0b0436320".
It looks like that was also mentioned in the description of #174 . (I was using kind v0.19.x on an ubuntu 22.04 VM, not sure the host matters though)

You could also feel inspired to add a unit test like I did..., but I'm not from the iovisor group, just a fellow attempting contributor.

return dname, nil
}
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/tracejob/selected_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down