diff --git a/comp/core/workloadmeta/collectors/internal/containerd/container_builder.go b/comp/core/workloadmeta/collectors/internal/containerd/container_builder.go index 50bd32b8c29267..5f0152be7f939e 100644 --- a/comp/core/workloadmeta/collectors/internal/containerd/container_builder.go +++ b/comp/core/workloadmeta/collectors/internal/containerd/container_builder.go @@ -136,13 +136,7 @@ func buildWorkloadMetaContainer(namespace string, container containerd.Container workloadContainer.EnvVars = envs workloadContainer.Hostname = spec.Hostname if spec.Linux != nil { - // Containerd applies some transformations to the cgroup path, we need to revert them - // https://github.com/containerd/containerd/blob/b168147ca8fccf05003117324f493d40f97b6077/internal/cri/server/podsandbox/helpers_linux.go#L64-L65 - // See https://github.com/opencontainers/runc/blob/main/docs/systemd.md - workloadContainer.CgroupPath = spec.Linux.CgroupsPath - if l := strings.Split(workloadContainer.CgroupPath, ":"); len(l) == 3 { - workloadContainer.CgroupPath = l[0] + "/" + l[1] + "-" + l[2] + ".scope" - } + workloadContainer.CgroupPath = extractCgroupPath(spec.Linux.CgroupsPath) } } else if errors.Is(err, cutil.ErrSpecTooLarge) { log.Warnf("Skipping parsing of container spec for container id: %s, spec is bigger than: %d", info.ID, cutil.DefaultAllowedSpecMaxSize) @@ -153,6 +147,17 @@ func buildWorkloadMetaContainer(namespace string, container containerd.Container return workloadContainer, nil } +// Containerd applies some transformations to the cgroup path, we need to revert them +// https://github.com/containerd/containerd/blob/b168147ca8fccf05003117324f493d40f97b6077/internal/cri/server/podsandbox/helpers_linux.go#L64-L65 +// See https://github.com/opencontainers/runc/blob/main/docs/systemd.md +func extractCgroupPath(path string) string { + res := path + if l := strings.Split(path, ":"); len(l) == 3 { + res = l[0] + "/" + l[1] + "-" + l[2] + ".scope" + } + return res +} + func extractStatus(status containerd.ProcessStatus) workloadmeta.ContainerStatus { switch status { case containerd.Paused, containerd.Pausing: diff --git a/comp/core/workloadmeta/collectors/internal/containerd/container_builder_test.go b/comp/core/workloadmeta/collectors/internal/containerd/container_builder_test.go index 67e4e541396b31..8a106993c0ae8d 100644 --- a/comp/core/workloadmeta/collectors/internal/containerd/container_builder_test.go +++ b/comp/core/workloadmeta/collectors/internal/containerd/container_builder_test.go @@ -182,6 +182,44 @@ func TestBuildWorkloadMetaContainer(t *testing.T) { assert.Equal(t, expected, result) } +func TestExtractCgroupPath(t *testing.T) { + tests := []struct { + name string + path string + expected string + }{ + // cgroupfs retrieved using minikube + qemu2 driver + { + name: "cgroupfs + kubernetes pod", + path: "/kubepods/burstable/pod84a7cac1-5690-4935-bffc-4b808e0240e4/0af39253daf5d1054519efdd054023e929785c2813c29f6a0ce887f652e1a997", + expected: "/kubepods/burstable/pod84a7cac1-5690-4935-bffc-4b808e0240e4/0af39253daf5d1054519efdd054023e929785c2813c29f6a0ce887f652e1a997", + }, + // systemd retrieved using kind + { + name: "systemd + kubernetes pod", + path: "kubelet-kubepods-burstable-pod99dcb84d2a34f7e338778606703258c4.slice:cri-containerd:ec9ea0ad54dd0d96142d5dbe11eb3f1509e12ba9af739620c7b5ad377ce94602", + expected: "kubelet-kubepods-burstable-pod99dcb84d2a34f7e338778606703258c4.slice/cri-containerd-ec9ea0ad54dd0d96142d5dbe11eb3f1509e12ba9af739620c7b5ad377ce94602.scope", + }, + // custom retrieved using ctr run + { + name: "systemd/cgroupfs + container", + path: "/default/redis", + expected: "/default/redis", + }, + // garden + { + name: "garden", + path: "/garden/1a65c217-84b2-8d13-3d78-46b14b6c7ea1", + expected: "/garden/1a65c217-84b2-8d13-3d78-46b14b6c7ea1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, extractCgroupPath(tt.path)) + }) + } +} + func TestExtractRuntimeFlavor(t *testing.T) { tests := []struct { name string