From cdd6f29036a5b488291b95524db4cb42b88e187c Mon Sep 17 00:00:00 2001 From: tombokombo Date: Fri, 11 Mar 2022 17:57:26 +0100 Subject: [PATCH 1/2] add containerd support Signed-off-by: tombokombo --- cgroup/cgroup.go | 10 +++++++++- containers/registry.go | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cgroup/cgroup.go b/cgroup/cgroup.go index 8dfcc9a..46a3171 100644 --- a/cgroup/cgroup.go +++ b/cgroup/cgroup.go @@ -18,6 +18,7 @@ var ( dockerIdRegexp = regexp.MustCompile(`([a-z0-9]{64})`) crioIdRegexp = regexp.MustCompile(`crio-([a-z0-9]{64})`) + containerdIdRegexp = regexp.MustCompile(`cri-containerd-([a-z0-9]{64})`) lxcIdRegexp = regexp.MustCompile(`/lxc/([^/]+)`) systemSliceIdRegexp = regexp.MustCompile(`(/system\.slice/([^/]+))`) ) @@ -29,6 +30,7 @@ const ( ContainerTypeStandaloneProcess ContainerTypeDocker ContainerTypeCrio + ContainerTypeContainerd ContainerTypeLxc ContainerTypeSystemdService ) @@ -41,6 +43,8 @@ func (t ContainerType) String() string { return "docker" case ContainerTypeCrio: return "crio" + case ContainerTypeContainerd: + return "cri-containerd" case ContainerTypeLxc: return "lxc" case ContainerTypeSystemdService: @@ -184,11 +188,15 @@ func containerByCgroup(path string) (ContainerType, string, error) { return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path) } return ContainerTypeDocker, matches[1], nil - case "kubepods": + case "kubepods", "kubepods.slice": crioMatches := crioIdRegexp.FindStringSubmatch(path) if crioMatches != nil { return ContainerTypeCrio, crioMatches[1], nil } + containerdMatches := containerdIdRegexp.FindStringSubmatch(path) + if containerdMatches != nil { + return ContainerTypeContainerd, containerdMatches[1], nil + } matches := dockerIdRegexp.FindStringSubmatch(path) if matches == nil { return ContainerTypeUnknown, "", fmt.Errorf("invalid docker cgroup %s", path) diff --git a/containers/registry.go b/containers/registry.go index 18fbe6b..0f0e916 100644 --- a/containers/registry.go +++ b/containers/registry.go @@ -251,7 +251,7 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID { if cg.ContainerType == cgroup.ContainerTypeSystemdService { return ContainerID(cg.ContainerId) } - if cg.ContainerType != cgroup.ContainerTypeDocker { + if cg.ContainerType != cgroup.ContainerTypeDocker && cg.ContainerType != cgroup.ContainerTypeContainerd { return "" } if md.labels["io.kubernetes.pod.name"] != "" { @@ -271,7 +271,7 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID { } func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) { - if cg.ContainerType != cgroup.ContainerTypeDocker { + if cg.ContainerType != cgroup.ContainerTypeDocker && cg.ContainerType != cgroup.ContainerTypeContainerd { return &ContainerMetadata{}, nil } var dockerdErr error From b316860653380890064b5fc145999b95eb33f6ea Mon Sep 17 00:00:00 2001 From: tombokombo Date: Sat, 12 Mar 2022 03:59:26 +0100 Subject: [PATCH 2/2] add test Signed-off-by: tombokombo --- cgroup/cgroup_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cgroup/cgroup_test.go b/cgroup/cgroup_test.go index 45629fa..743ed75 100644 --- a/cgroup/cgroup_test.go +++ b/cgroup/cgroup_test.go @@ -79,6 +79,11 @@ func TestContainerByCgroup(t *testing.T) { as.Equal("63425c4a8b4291744a79dd9011fddc7a1f8ffda61f65d72196aa01d00cae2e2e", id) as.Nil(err) + typ, id, err = containerByCgroup("/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod3e61c214bc3ed9ff81e21474dd6cba17.slice/cri-containerd-c74b0f5062f0bc726cae1e9369ad4a95deed6b298d247f0407475adb23fa3190") + as.Equal(typ, ContainerTypeContainerd) + as.Equal("c74b0f5062f0bc726cae1e9369ad4a95deed6b298d247f0407475adb23fa3190", id) + as.Nil(err) + typ, id, err = containerByCgroup("/system.slice/system-serial\\x2dgetty.slice") as.Equal(typ, ContainerTypeSystemdService) as.Equal("/system.slice/system-serial\\x2dgetty.slice", id)