Skip to content

Commit

Permalink
fix(ebpf): update regexps for sd cgroupv1 matching (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev authored Nov 16, 2022
1 parent b3fbd3c commit ebc951d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/agent/ebpfspy/sd/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ 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]
}
return ""
}

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$`)
)
18 changes: 18 additions & 0 deletions pkg/agent/ebpfspy/sd/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ebc951d

Please sign in to comment.