diff --git a/container/common/helpers.go b/container/common/helpers.go index ac00b2ca45..3606d6860c 100644 --- a/container/common/helpers.go +++ b/container/common/helpers.go @@ -23,6 +23,7 @@ import ( "strings" "time" + "github.com/google/cadvisor/container" info "github.com/google/cadvisor/info/v1" "github.com/google/cadvisor/utils" @@ -201,3 +202,23 @@ func CgroupExists(cgroupPaths map[string]string) bool { } return false } + +func ListContainers(name string, cgroupPaths map[string]string, listType container.ListType) ([]info.ContainerReference, error) { + containers := make(map[string]struct{}) + for _, cgroupPath := range cgroupPaths { + err := ListDirectories(cgroupPath, name, listType == container.ListRecursive, containers) + if err != nil { + return nil, err + } + } + + // Make into container references. + ret := make([]info.ContainerReference, 0, len(containers)) + for cont := range containers { + ret = append(ret, info.ContainerReference{ + Name: cont, + }) + } + + return ret, nil +} diff --git a/container/raw/handler.go b/container/raw/handler.go index 0e55046edd..8ee3379c2a 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -269,23 +269,7 @@ func (self *rawContainerHandler) GetContainerLabels() map[string]string { } func (self *rawContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) { - containers := make(map[string]struct{}) - for _, cgroupPath := range self.cgroupPaths { - err := common.ListDirectories(cgroupPath, self.name, listType == container.ListRecursive, containers) - if err != nil { - return nil, err - } - } - - // Make into container references. - ret := make([]info.ContainerReference, 0, len(containers)) - for cont := range containers { - ret = append(ret, info.ContainerReference{ - Name: cont, - }) - } - - return ret, nil + return common.ListContainers(self.name, self.cgroupPaths, listType) } func (self *rawContainerHandler) ListThreads(listType container.ListType) ([]int, error) { diff --git a/container/rkt/factory.go b/container/rkt/factory.go index 9f4ca76199..b4337b6864 100644 --- a/container/rkt/factory.go +++ b/container/rkt/factory.go @@ -63,7 +63,7 @@ func (self *rktFactory) CanHandleAndAccept(name string) (bool, bool, error) { if strings.HasPrefix(name, "/machine.slice/machine-rkt\\x2d") { accept, err := verifyName(name) - return true, accept, err + return accept, accept, err } return false, false, fmt.Errorf("%s not handled by rkt handler", name) } diff --git a/container/rkt/handler.go b/container/rkt/handler.go index 31ca9707f5..a6b95a198d 100644 --- a/container/rkt/handler.go +++ b/container/rkt/handler.go @@ -18,7 +18,6 @@ package rkt import ( "fmt" "os" - "path" "time" rktapi "github.com/coreos/rkt/api/v1alpha" @@ -260,48 +259,7 @@ func (handler *rktContainerHandler) GetContainerLabels() map[string]string { } func (handler *rktContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) { - containers := make(map[string]struct{}) - - // Rkt containers do not have subcontainers, only the "Pod" does. - if handler.isPod == false { - var ret []info.ContainerReference - return ret, nil - } - - // Turn the system.slice cgroups into the Pod's subcontainers - for _, cgroupPath := range handler.cgroupPaths { - err := common.ListDirectories(path.Join(cgroupPath, "system.slice"), path.Join(handler.name, "system.slice"), listType == container.ListRecursive, containers) - if err != nil { - return nil, err - } - } - - // Create the container references. for the Pod's subcontainers - ret := make([]info.ContainerReference, 0, len(handler.apiPod.Apps)) - for cont := range containers { - aliases := make([]string, 1) - parsed, err := parseName(cont) - if err != nil { - return nil, fmt.Errorf("this should be impossible!, unable to parse rkt subcontainer name = %s", cont) - } - aliases = append(aliases, parsed.Pod+":"+parsed.Container) - - labels := make(map[string]string) - if annotations, ok := findAnnotations(handler.apiPod.Apps, parsed.Container); !ok { - glog.Warningf("couldn't find application in Pod matching %v", parsed.Container) - } else { - labels = createLabels(annotations) - } - - ret = append(ret, info.ContainerReference{ - Name: cont, - Aliases: aliases, - Namespace: RktNamespace, - Labels: labels, - }) - } - - return ret, nil + return common.ListContainers(handler.name, handler.cgroupPaths, listType) } func (handler *rktContainerHandler) ListThreads(listType container.ListType) ([]int, error) {