From d33373d8fb7c40247ac580b65f413c3dca8c8f4a Mon Sep 17 00:00:00 2001 From: Tolyan Korniltsev Date: Wed, 16 Nov 2022 17:43:26 +0800 Subject: [PATCH 1/2] fix(ebpf): update regexps for sd cgroupv1 matching --- pkg/agent/ebpfspy/sd/k8s.go | 8 ++++---- pkg/agent/ebpfspy/sd/k8s_test.go | 17 +++++++++++++++++ 2 files changed, 21 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..2511edaa00 100644 --- a/pkg/agent/ebpfspy/sd/k8s_test.go +++ b/pkg/agent/ebpfspy/sd/k8s_test.go @@ -32,3 +32,20 @@ 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) + } +} From 3559e20e7aa51bd04ecc984fffdc21c4a830d253 Mon Sep 17 00:00:00 2001 From: Tolyan Korniltsev Date: Wed, 16 Nov 2022 17:47:22 +0800 Subject: [PATCH 2/2] linter --- pkg/agent/ebpfspy/sd/k8s_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/agent/ebpfspy/sd/k8s_test.go b/pkg/agent/ebpfspy/sd/k8s_test.go index 2511edaa00..4fada4d5bd 100644 --- a/pkg/agent/ebpfspy/sd/k8s_test.go +++ b/pkg/agent/ebpfspy/sd/k8s_test.go @@ -35,7 +35,8 @@ func TestDockerCgroupsV2(t *testing.T) { 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" + 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 {