Skip to content

Commit

Permalink
Merge pull request #1261 from sjpotter/listToCommon
Browse files Browse the repository at this point in the history
move ListContainers code to common and make rkt play nice
  • Loading branch information
vishh committed May 3, 2016
2 parents 1faa767 + 5ca11bb commit bebe18b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 61 deletions.
21 changes: 21 additions & 0 deletions container/common/helpers.go
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"time"

"github.com/google/cadvisor/container"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/utils"

Expand Down Expand Up @@ -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
}
18 changes: 1 addition & 17 deletions container/raw/handler.go
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion container/rkt/factory.go
Expand Up @@ -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)
}
Expand Down
44 changes: 1 addition & 43 deletions container/rkt/handler.go
Expand Up @@ -18,7 +18,6 @@ package rkt
import (
"fmt"
"os"
"path"
"time"

rktapi "github.com/coreos/rkt/api/v1alpha"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bebe18b

Please sign in to comment.