Merge pull request #94 from vmarmol/systemd2
Implement ListContainers and no-op GetSpec().
This commit is contained in:
commit
2700b4bcb0
@ -15,12 +15,20 @@
|
|||||||
package raw
|
package raw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/docker/libcontainer/cgroups"
|
||||||
"github.com/google/cadvisor/container"
|
"github.com/google/cadvisor/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type cgroupSubsystems struct {
|
||||||
|
// Cgroup subsystem mounts.
|
||||||
|
mounts []cgroups.Mount
|
||||||
|
}
|
||||||
|
|
||||||
type rawFactory struct {
|
type rawFactory struct {
|
||||||
|
cgroupSubsystems *cgroupSubsystems
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *rawFactory) String() string {
|
func (self *rawFactory) String() string {
|
||||||
@ -28,7 +36,7 @@ func (self *rawFactory) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHandler, error) {
|
func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHandler, error) {
|
||||||
return newRawContainerHandler(name)
|
return newRawContainerHandler(name, self.cgroupSubsystems)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The raw factory can handle any container.
|
// The raw factory can handle any container.
|
||||||
@ -37,7 +45,42 @@ func (self *rawFactory) CanHandle(name string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Register() error {
|
func Register() error {
|
||||||
|
// Get all cgroup mounts.
|
||||||
|
allCgroups, err := cgroups.GetCgroupMounts()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(allCgroups) == 0 {
|
||||||
|
return fmt.Errorf("failed to find cgroup mounts for the raw factory")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim the mounts to only the subsystems we care about.
|
||||||
|
supportedCgroups := make([]cgroups.Mount, 0, len(allCgroups))
|
||||||
|
for _, mount := range allCgroups {
|
||||||
|
for _, subsystem := range mount.Subsystems {
|
||||||
|
if _, ok := supportedSubsystems[subsystem]; ok {
|
||||||
|
supportedCgroups = append(supportedCgroups, mount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(supportedCgroups) == 0 {
|
||||||
|
return fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Registering Raw factory")
|
log.Printf("Registering Raw factory")
|
||||||
container.RegisterContainerHandlerFactory(new(rawFactory))
|
factory := &rawFactory{
|
||||||
|
cgroupSubsystems: &cgroupSubsystems{
|
||||||
|
mounts: supportedCgroups,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
container.RegisterContainerHandlerFactory(factory)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cgroup susbsystems we support listing (should be the minimal set we need stats from).
|
||||||
|
var supportedSubsystems map[string]struct{} = map[string]struct{}{
|
||||||
|
"cpu": struct{}{},
|
||||||
|
"cpuset": struct{}{},
|
||||||
|
"cpuacct": struct{}{},
|
||||||
|
"memory": struct{}{},
|
||||||
|
}
|
||||||
|
@ -15,6 +15,9 @@
|
|||||||
package raw
|
package raw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/docker/libcontainer/cgroups"
|
"github.com/docker/libcontainer/cgroups"
|
||||||
"github.com/google/cadvisor/container"
|
"github.com/google/cadvisor/container"
|
||||||
"github.com/google/cadvisor/container/libcontainer"
|
"github.com/google/cadvisor/container/libcontainer"
|
||||||
@ -22,12 +25,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type rawContainerHandler struct {
|
type rawContainerHandler struct {
|
||||||
name string
|
name string
|
||||||
|
cgroupSubsystems *cgroupSubsystems
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRawContainerHandler(name string) (container.ContainerHandler, error) {
|
func newRawContainerHandler(name string, cgroupSubsystems *cgroupSubsystems) (container.ContainerHandler, error) {
|
||||||
return &rawContainerHandler{
|
return &rawContainerHandler{
|
||||||
name: name,
|
name: name,
|
||||||
|
cgroupSubsystems: cgroupSubsystems,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,8 +44,12 @@ func (self *rawContainerHandler) ContainerReference() (info.ContainerReference,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
|
func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
|
||||||
|
ret := new(info.ContainerSpec)
|
||||||
|
ret.Cpu = new(info.CpuSpec)
|
||||||
|
ret.Memory = new(info.MemorySpec)
|
||||||
|
|
||||||
// TODO(vmarmol): Implement
|
// TODO(vmarmol): Implement
|
||||||
return new(info.ContainerSpec), nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *rawContainerHandler) GetStats() (stats *info.ContainerStats, err error) {
|
func (self *rawContainerHandler) GetStats() (stats *info.ContainerStats, err error) {
|
||||||
@ -52,9 +61,48 @@ func (self *rawContainerHandler) GetStats() (stats *info.ContainerStats, err err
|
|||||||
return libcontainer.GetStats(cgroup, false)
|
return libcontainer.GetStats(cgroup, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lists all directories under "path" and outputs the results as children of "parent".
|
||||||
|
func listDirectories(path string, parent string, recursive bool, output map[string]struct{}) error {
|
||||||
|
entries, err := ioutil.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, entry := range entries {
|
||||||
|
// We only grab directories.
|
||||||
|
if entry.IsDir() {
|
||||||
|
name := filepath.Join(parent, entry.Name())
|
||||||
|
output[name] = struct{}{}
|
||||||
|
|
||||||
|
// List subcontainers if asked to.
|
||||||
|
if recursive {
|
||||||
|
err := listDirectories(filepath.Join(path, entry.Name()), name, true, output)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (self *rawContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
|
func (self *rawContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
|
||||||
// TODO(vmarmol): Implement
|
containers := make(map[string]struct{}, 16)
|
||||||
return make([]info.ContainerReference, 0, 0), nil
|
for _, subsystem := range self.cgroupSubsystems.mounts {
|
||||||
|
err := listDirectories(filepath.Join(subsystem.Mountpoint, self.name), self.name, listType == container.LIST_RECURSIVE, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *rawContainerHandler) ListThreads(listType container.ListType) ([]int, error) {
|
func (self *rawContainerHandler) ListThreads(listType container.ListType) ([]int, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user