diff --git a/container/common/helpers.go b/container/common/helpers.go index f6ffe366..ac00b2ca 100644 --- a/container/common/helpers.go +++ b/container/common/helpers.go @@ -191,3 +191,13 @@ func MakeCgroupPaths(mountPoints map[string]string, name string) map[string]stri return cgroupPaths } + +func CgroupExists(cgroupPaths map[string]string) bool { + // If any cgroup exists, the container is still alive. + for _, cgroupPath := range cgroupPaths { + if utils.FileExists(cgroupPath) { + return true + } + } + return false +} diff --git a/container/docker/handler.go b/container/docker/handler.go index 75656eb4..821bd7e7 100644 --- a/container/docker/handler.go +++ b/container/docker/handler.go @@ -333,7 +333,7 @@ func (self *dockerContainerHandler) StopWatchingSubcontainers() error { } func (self *dockerContainerHandler) Exists() bool { - return containerlibcontainer.Exists(*dockerRootDir, *dockerRunDir, self.id) + return common.CgroupExists(self.cgroupPaths) } func DockerInfo() (docker.DockerInfo, error) { diff --git a/container/libcontainer/compatibility.go b/container/libcontainer/compatibility.go deleted file mode 100644 index 481d3ef4..00000000 --- a/container/libcontainer/compatibility.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2015 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package libcontainer - -import ( - "path" - - "github.com/google/cadvisor/utils" -) - -const ( - // Relative path to the libcontainer execdriver directory. - libcontainerExecDriverPath = "execdriver/native" - // Absolute path of the containerd directory. - containerdPath = "/run/containerd" -) - -// Gets the path to the libcontainer configuration. -func configPath(dockerRun, containerID string) string { - return path.Join(dockerRun, libcontainerExecDriverPath, containerID, "state.json") -} - -func containerdConfigPath(dockerRun, containerID string) string { - return path.Join(containerdPath, containerID, "state.json") -} - -// Gets the path to the old libcontainer configuration. -func oldConfigPath(dockerRoot, containerID string) string { - return path.Join(dockerRoot, libcontainerExecDriverPath, containerID, "container.json") -} - -// Gets whether the specified container exists. -func Exists(dockerRoot, dockerRun, containerID string) bool { - // New or old config must exist for the container to be considered alive. - return utils.FileExists(containerdConfigPath(dockerRun, containerID)) || - utils.FileExists(configPath(dockerRun, containerID)) || - utils.FileExists(oldConfigPath(dockerRoot, containerID)) -} diff --git a/container/raw/handler.go b/container/raw/handler.go index 314320a4..0e55046e 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -26,7 +26,6 @@ import ( "github.com/google/cadvisor/container/libcontainer" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/utils" "github.com/google/cadvisor/utils/machine" "github.com/golang/glog" @@ -444,11 +443,5 @@ func (self *rawContainerHandler) StopWatchingSubcontainers() error { } func (self *rawContainerHandler) Exists() bool { - // If any cgroup exists, the container is still alive. - for _, cgroupPath := range self.cgroupPaths { - if utils.FileExists(cgroupPath) { - return true - } - } - return false + return common.CgroupExists(self.cgroupPaths) } diff --git a/container/rkt/handler.go b/container/rkt/handler.go index 5effc1f9..31ca9707 100644 --- a/container/rkt/handler.go +++ b/container/rkt/handler.go @@ -27,7 +27,6 @@ import ( "github.com/google/cadvisor/container/libcontainer" "github.com/google/cadvisor/fs" info "github.com/google/cadvisor/info/v1" - "github.com/google/cadvisor/utils" "golang.org/x/net/context" "github.com/golang/glog" @@ -324,11 +323,5 @@ func (handler *rktContainerHandler) StopWatchingSubcontainers() error { } func (handler *rktContainerHandler) Exists() bool { - // If any cgroup exists, the container is still alive. - for _, cgroupPath := range handler.cgroupPaths { - if utils.FileExists(cgroupPath) { - return true - } - } - return false + return common.CgroupExists(handler.cgroupPaths) }