Better handle nested containers and libcontainer.

This commit is contained in:
Victor Marmol 2014-07-22 18:04:08 -07:00
parent 4658d5c32a
commit eef8c01e4e
4 changed files with 72 additions and 51 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/docker/libcontainer/cgroups/systemd" "github.com/docker/libcontainer/cgroups/systemd"
"github.com/fsouza/go-dockerclient" "github.com/fsouza/go-dockerclient"
"github.com/google/cadvisor/container" "github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/info" "github.com/google/cadvisor/info"
) )
@ -74,7 +75,7 @@ func (self *dockerFactory) CanHandle(name string) bool {
return false return false
} }
// Check if the container is known to docker and it is active. // Check if the container is known to docker and it is active.
_, id, err := splitName(name) _, id, err := libcontainer.SplitName(name)
if err != nil { if err != nil {
return false return false
} }

View File

@ -15,14 +15,12 @@
package docker package docker
import ( import (
"bufio"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"math" "math"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"github.com/docker/libcontainer" "github.com/docker/libcontainer"
@ -42,7 +40,7 @@ type dockerContainerHandler struct {
client *docker.Client client *docker.Client
name string name string
parent string parent string
ID string id string
aliases []string aliases []string
machineInfoFactory info.MachineInfoFactory machineInfoFactory info.MachineInfoFactory
useSystemd bool useSystemd bool
@ -63,12 +61,12 @@ func newDockerContainerHandler(
if handler.isDockerRoot() { if handler.isDockerRoot() {
return handler, nil return handler, nil
} }
parent, id, err := splitName(name) parent, id, err := containerLibcontainer.SplitName(name)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid docker container %v: %v", name, err) return nil, fmt.Errorf("invalid docker container %v: %v", name, err)
} }
handler.parent = parent handler.parent = parent
handler.ID = id handler.id = id
ctnr, err := client.InspectContainer(id) ctnr, err := client.InspectContainer(id)
// We assume that if Inspect fails then the container is not known to docker. // We assume that if Inspect fails then the container is not known to docker.
if err != nil { if err != nil {
@ -89,45 +87,9 @@ func (self *dockerContainerHandler) isDockerRoot() bool {
return self.name == "/docker" return self.name == "/docker"
} }
func splitName(containerName string) (string, string, error) {
parent, id := path.Split(containerName)
cgroupSelf, err := os.Open("/proc/self/cgroup")
if err != nil {
return "", "", err
}
scanner := bufio.NewScanner(cgroupSelf)
subsys := []string{"memory", "cpu"}
nestedLevels := 0
for scanner.Scan() {
line := scanner.Text()
elems := strings.Split(line, ":")
if len(elems) < 3 {
continue
}
for _, s := range subsys {
if elems[1] == s {
// count how many nested docker containers are there.
nestedLevels = strings.Count(elems[2], "/docker")
break
}
}
}
if nestedLevels > 0 {
// we are running inside a docker container
upperLevel := strings.Repeat("../../", nestedLevels)
parent = filepath.Join(upperLevel, parent)
}
// Strip the last "/"
if parent[len(parent)-1] == '/' {
parent = parent[:len(parent)-1]
}
return parent, id, nil
}
// TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API. // TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API.
func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) { func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) {
configPath := path.Join(dockerRootDir, self.ID, "container.json") configPath := path.Join(dockerRootDir, self.id, "container.json")
if !utils.FileExists(configPath) { if !utils.FileExists(configPath) {
// TODO(vishh): Return file name as well once we have a better error interface. // TODO(vishh): Return file name as well once we have a better error interface.
err = fileNotFound err = fileNotFound
@ -146,11 +108,15 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain
} }
config = retConfig config = retConfig
// Replace cgroup parent and name with our own since we may be running in a different context.
config.Cgroups.Parent = self.parent
config.Cgroups.Name = self.id
return return
} }
func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) { func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) {
statePath := path.Join(dockerRootDir, self.ID, "state.json") statePath := path.Join(dockerRootDir, self.id, "state.json")
if !utils.FileExists(statePath) { if !utils.FileExists(statePath) {
// TODO(vishh): Return file name as well once we have a better error interface. // TODO(vishh): Return file name as well once we have a better error interface.
err = fileNotFound err = fileNotFound

View File

@ -1,6 +1,11 @@
package libcontainer package libcontainer
import ( import (
"bufio"
"os"
"path"
"path/filepath"
"strings"
"time" "time"
"github.com/docker/libcontainer" "github.com/docker/libcontainer"
@ -64,3 +69,48 @@ func toContainerStats(libcontainerStats *libcontainer.ContainerStats) *info.Cont
return ret return ret
} }
// Given a container name, returns the parent and name of the container to be fed to libcontainer.
func SplitName(containerName string) (string, string, error) {
parent, id := path.Split(containerName)
cgroupSelf, err := os.Open("/proc/self/cgroup")
if err != nil {
return "", "", err
}
scanner := bufio.NewScanner(cgroupSelf)
// Find how nested we are. Libcontainer takes container names relative to the current process.
subsys := []string{"memory", "cpu"}
nestedLevels := 0
for scanner.Scan() {
line := scanner.Text()
elems := strings.Split(line, ":")
if len(elems) < 3 {
continue
}
for _, s := range subsys {
if elems[1] == s {
if elems[2] == "/" {
// We're running at root, no nesting.
nestedLevels = 0
} else {
// Count how deeply nested we are.
nestedLevels = strings.Count(elems[2], "/")
}
break
}
}
}
if nestedLevels > 0 {
// we are running inside a docker container
upperLevel := strings.Repeat("../", nestedLevels)
parent = filepath.Join(upperLevel, parent)
}
// Strip the last "/"
if parent[len(parent)-1] == '/' {
parent = parent[:len(parent)-1]
}
return parent, id, nil
}

View File

@ -31,13 +31,22 @@ import (
type rawContainerHandler struct { type rawContainerHandler struct {
name string name string
cgroup *cgroups.Cgroup
cgroupSubsystems *cgroupSubsystems cgroupSubsystems *cgroupSubsystems
machineInfoFactory info.MachineInfoFactory machineInfoFactory info.MachineInfoFactory
} }
func newRawContainerHandler(name string, cgroupSubsystems *cgroupSubsystems, machineInfoFactory info.MachineInfoFactory) (container.ContainerHandler, error) { func newRawContainerHandler(name string, cgroupSubsystems *cgroupSubsystems, machineInfoFactory info.MachineInfoFactory) (container.ContainerHandler, error) {
parent, id, err := libcontainer.SplitName(name)
if err != nil {
return nil, err
}
return &rawContainerHandler{ return &rawContainerHandler{
name: name, name: name,
cgroup: &cgroups.Cgroup{
Parent: parent,
Name: id,
},
cgroupSubsystems: cgroupSubsystems, cgroupSubsystems: cgroupSubsystems,
machineInfoFactory: machineInfoFactory, machineInfoFactory: machineInfoFactory,
}, nil }, nil
@ -116,12 +125,7 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
} }
func (self *rawContainerHandler) GetStats() (stats *info.ContainerStats, err error) { func (self *rawContainerHandler) GetStats() (stats *info.ContainerStats, err error) {
cgroup := &cgroups.Cgroup{ return libcontainer.GetStatsCgroupOnly(self.cgroup)
Parent: "/",
Name: self.name,
}
return libcontainer.GetStatsCgroupOnly(cgroup)
} }
// Lists all directories under "path" and outputs the results as children of "parent". // Lists all directories under "path" and outputs the results as children of "parent".