From 724d8299ef1d14520a6f2aa268fcd3711243623c Mon Sep 17 00:00:00 2001 From: esara Date: Wed, 31 Jan 2024 00:24:49 -0500 Subject: [PATCH] parse containerid for a pid from cgroup as a hex chain --- pkg/internal/helpers/container/container.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/internal/helpers/container/container.go b/pkg/internal/helpers/container/container.go index 176dfcab6..ece783d0b 100644 --- a/pkg/internal/helpers/container/container.go +++ b/pkg/internal/helpers/container/container.go @@ -25,12 +25,12 @@ type Info struct { } // A docker cgroup entry is a string like: -// 0::/docker//kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/ -// kubelet-kubepods-besteffort-pod.slice/cri-containerd-.scope +// GKE: /kubepods/burstable/pod4a163a05-439d-484b-8e53-2968bc15824f/cde6dfaf5007ed65aad2d6aed72af91b0f3d95813492f773286e29ae145d20f4 +// GKE-containerd: /kubepods/burstable/podb53dfa9e2dc9b890f7fadb2770857b03/bb959773d06ad1a07d469bced637bbc49b6f8573c493fd0548d7f5810eb3e5a8 +// EKS: /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddde1244b_5bb5_4297_b544_85f3bc0d80bf.slice/cri-containerd-acb62391578595ec849d87d8556369cee7f935f0425097fd5f870df0e8aabd3c.scope +// Containerd: /kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7260904bbd08e72e4dff95d9fccd2ee8.slice/cri-containerd-d36686f9785534531160dc936aec9d711a26eb37f4fc7752a2ae27d0a24345c1.scope // where the last chain is the container ID inside its Pod -// The /docker/ part might not be visible inside the Pod (e.g. deploying Beyla -// as a sidecar). That's why we search for the "kubelet.slice" string. -var dockerCgroup = regexp.MustCompile(`^\d+:.*:.*/.*-([\da-fA-F]+)\.scope`) +var dockerCgroup = regexp.MustCompile(`([0-9a-f]){64}`) // InfoForPID returns the container ID and PID namespace for the given PID. func InfoForPID(pid uint32) (Info, error) { @@ -45,10 +45,10 @@ func InfoForPID(pid uint32) (Info, error) { } for _, cgroupEntry := range bytes.Split(cgroupBytes, []byte{'\n'}) { submatches := dockerCgroup.FindSubmatch(cgroupEntry) - if len(submatches) < 2 { + if len(submatches) < 1 { continue } - return Info{PIDNamespace: ns, ContainerID: string(submatches[1])}, nil + return Info{PIDNamespace: ns, ContainerID: string(submatches[0])}, nil } return Info{}, fmt.Errorf("%s: couldn't find any docker entry for process with PID %d", cgroupFile, pid) }