Skip to content

Commit

Permalink
add test cases for cgroupfs / systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
AliDatadog committed May 14, 2024
1 parent e1fe9ac commit a83b759
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a83b759

Please sign in to comment.