Skip to content

Commit

Permalink
Merge pull request #1 from tombokombo/feature/fix_containerd
Browse files Browse the repository at this point in the history
containerd support
  • Loading branch information
def authored Mar 12, 2022
2 parents 5eac9ec + b316860 commit 20918ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/([^/]+))`)
)
Expand All @@ -29,6 +30,7 @@ const (
ContainerTypeStandaloneProcess
ContainerTypeDocker
ContainerTypeCrio
ContainerTypeContainerd
ContainerTypeLxc
ContainerTypeSystemdService
)
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions containers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] != "" {
Expand All @@ -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
Expand Down

0 comments on commit 20918ff

Please sign in to comment.