diff --git a/container/docker/factory.go b/container/docker/factory.go index 1f4915bd949..2c096737e1e 100644 --- a/container/docker/factory.go +++ b/container/docker/factory.go @@ -141,17 +141,21 @@ func ContainerNameToDockerId(name string) string { return id } +// isContainerName returns true if the cgroup with associated name +// corresponds to a docker container. func isContainerName(name string) bool { + // always ignore .mount cgroup even if associated with docker and delegate to systemd + if strings.HasSuffix(name, ".mount") { + return false + } return dockerCgroupRegexp.MatchString(path.Base(name)) } // Docker handles all containers under /docker func (self *dockerFactory) CanHandleAndAccept(name string) (bool, bool, error) { - // docker factory accepts all containers it can handle. - canAccept := true - + // if the container is not associated with docker, we can't handle it or accept it. if !isContainerName(name) { - return false, canAccept, fmt.Errorf("invalid container name") + return false, false, nil } // Check if the container is known to docker and it is active. @@ -160,10 +164,10 @@ func (self *dockerFactory) CanHandleAndAccept(name string) (bool, bool, error) { // We assume that if Inspect fails then the container is not known to docker. ctnr, err := self.client.ContainerInspect(context.Background(), id) if err != nil || !ctnr.State.Running { - return false, canAccept, fmt.Errorf("error inspecting container: %v", err) + return false, true, fmt.Errorf("error inspecting container: %v", err) } - return true, canAccept, nil + return true, true, nil } func (self *dockerFactory) DebugInfo() map[string][]string { diff --git a/container/docker/factory_test.go b/container/docker/factory_test.go index 3131aac8586..ce1a8d969b1 100644 --- a/container/docker/factory_test.go +++ b/container/docker/factory_test.go @@ -49,3 +49,24 @@ func TestEnsureThinLsKernelVersion(t *testing.T) { } } } + +func TestIsContainerName(t *testing.T) { + tests := []struct { + name string + expected bool + }{ + { + name: "/system.slice/var-lib-docker-overlay-9f086b233ab7c786bf8b40b164680b658a8f00e94323868e288d6ce20bc92193-merged.mount", + expected: false, + }, + { + name: "/system.slice/docker-72e5a5ff5eef3c4222a6551b992b9360a99122f77d2229783f0ee0946dfd800e.scope", + expected: true, + }, + } + for _, test := range tests { + if actual := isContainerName(test.name); actual != test.expected { + t.Fatalf("%s: expected: %v, actual: %v", test.name, test.expected, actual) + } + } +}