From ebc951da393c555f2a285074d46dd470ba8c0014 Mon Sep 17 00:00:00 2001 From: Tolya Korniltsev Date: Wed, 16 Nov 2022 20:31:43 +0800 Subject: [PATCH] fix(ebpf): update regexps for sd cgroupv1 matching (#1719) --- pkg/agent/ebpfspy/sd/k8s.go | 8 ++++---- pkg/agent/ebpfspy/sd/k8s_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/agent/ebpfspy/sd/k8s.go b/pkg/agent/ebpfspy/sd/k8s.go index 1dae1c4d4c..aee38b7b9d 100644 --- a/pkg/agent/ebpfspy/sd/k8s.go +++ b/pkg/agent/ebpfspy/sd/k8s.go @@ -160,7 +160,7 @@ func getContainerIDFromCGroup(line string) string { if parts != nil { return parts[1] } - parts = cgroupV2ScopePattern.FindStringSubmatch(line) + parts = cgroupScopePattern.FindStringSubmatch(line) if parts != nil { return parts[1] } @@ -168,7 +168,7 @@ func getContainerIDFromCGroup(line string) string { } var ( - kubePattern = regexp.MustCompile(`\d+:.+:/kubepods/[^/]+/pod[^/]+/([0-9a-f]{64})`) - dockerPattern = regexp.MustCompile(`\d+:.+:/docker/pod[^/]+/([0-9a-f]{64})`) - cgroupV2ScopePattern = regexp.MustCompile(`^0::.*/(?:docker-|cri-containerd-)([0-9a-f]{64})\.scope$`) + kubePattern = regexp.MustCompile(`\d+:.+:/kubepods/[^/]+/pod[^/]+/([0-9a-f]{64})`) + dockerPattern = regexp.MustCompile(`\d+:.+:/docker/pod[^/]+/([0-9a-f]{64})`) + cgroupScopePattern = regexp.MustCompile(`^\d+:.*/(?:docker-|cri-containerd-)([0-9a-f]{64})\.scope$`) ) diff --git a/pkg/agent/ebpfspy/sd/k8s_test.go b/pkg/agent/ebpfspy/sd/k8s_test.go index 4e71489ad9..4fada4d5bd 100644 --- a/pkg/agent/ebpfspy/sd/k8s_test.go +++ b/pkg/agent/ebpfspy/sd/k8s_test.go @@ -32,3 +32,21 @@ func TestDockerCgroupsV2(t *testing.T) { t.Fatalf("wrong cid %s != %s", cid, expected) } } + +func TestCRI(t *testing.T) { + statusContainerID := "containerd://a534eb629135e43beb13213976e37bb2ab95cba4c0d1d0b4e27c6bc4d8091b83" + cgroup := "12:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod471203d1_984f_477e_9c35_db96487ffe5e.slice/" + + "cri-containerd-a534eb629135e43beb13213976e37bb2ab95cba4c0d1d0b4e27c6bc4d8091b83.scope" + cid := getContainerIDFromCGroup(cgroup) + expected := "a534eb629135e43beb13213976e37bb2ab95cba4c0d1d0b4e27c6bc4d8091b83" + if cid != expected { + t.Fatalf("wrong cid %s != %s", cid, expected) + } + cid, err := getContainerIDFromK8S(statusContainerID) + if err != nil { + t.Fatal(err) + } + if cid != expected { + t.Fatalf("wrong cid %s != %s", cid, expected) + } +}