diff --git a/container/container.go b/container/container.go index efe92397..de478fd0 100644 --- a/container/container.go +++ b/container/container.go @@ -14,11 +14,7 @@ package container -import ( - "errors" - - "github.com/google/cadvisor/info" -) +import "github.com/google/cadvisor/info" // Listing types. const ( @@ -26,8 +22,6 @@ const ( LIST_RECURSIVE ) -var NotActive = errors.New("Container is not active") - type ListType int // Interface for container operation handlers. diff --git a/container/docker/factory.go b/container/docker/factory.go index a8b3c186..0caf8c93 100644 --- a/container/docker/factory.go +++ b/container/docker/factory.go @@ -28,16 +28,15 @@ import ( "github.com/google/cadvisor/info" ) -var ( - ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint") - defaultClient *docker.Client -) +var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint") type dockerFactory struct { machineInfoFactory info.MachineInfoFactory // Whether this system is using systemd. useSystemd bool + + client *docker.Client } func (self *dockerFactory) String() string { @@ -77,10 +76,7 @@ func (self *dockerFactory) CanHandle(name string) bool { if err != nil { return false } - if defaultClient == nil { - log.Fatal("Default docker client is nil") - } - ctnr, err := defaultClient.InspectContainer(id) + ctnr, err := self.client.InspectContainer(id) // We assume that if Inspect fails then the container is not known to docker. // TODO(vishh): Detect lxc containers and avoid handling them. if err != nil || !ctnr.State.Running { @@ -110,12 +106,12 @@ func parseDockerVersion(full_version_string string) ([]int, error) { } // Register root container before running this function! -func Register(factory info.MachineInfoFactory) (err error) { - defaultClient, err = docker.NewClient(*ArgDockerEndpoint) +func Register(factory info.MachineInfoFactory) error { + client, err := docker.NewClient(*ArgDockerEndpoint) if err != nil { return fmt.Errorf("unable to communicate with docker daemon: %v", err) } - if version, err := defaultClient.Version(); err != nil { + if version, err := client.Version(); err != nil { return fmt.Errorf("unable to communicate with docker daemon: %v", err) } else { expected_version := []int{0, 11, 1} @@ -135,6 +131,7 @@ func Register(factory info.MachineInfoFactory) (err error) { f := &dockerFactory{ machineInfoFactory: factory, useSystemd: systemd.UseSystemd(), + client: client, } log.Printf("Registering Docker factory") container.RegisterContainerHandlerFactory(f) diff --git a/container/docker/handler.go b/container/docker/handler.go index c6800c1a..0296090f 100644 --- a/container/docker/handler.go +++ b/container/docker/handler.go @@ -71,8 +71,8 @@ func newDockerContainerHandler( handler.ID = id ctnr, err := client.InspectContainer(id) // We assume that if Inspect fails then the container is not known to docker. - if err != nil || !ctnr.State.Running { - return nil, container.NotActive + if err != nil { + return nil, fmt.Errorf("failed to inspect container %s - %s\n", id, err) } handler.aliases = append(handler.aliases, path.Join("/docker", ctnr.Name)) return handler, nil @@ -126,16 +126,17 @@ func splitName(containerName string) (string, string, error) { 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) { configPath := path.Join(dockerRootDir, self.ID, "container.json") if !utils.FileExists(configPath) { + // TODO(vishh): Return file name as well once we have a better error interface. err = fileNotFound return } f, err := os.Open(configPath) if err != nil { - return + return nil, fmt.Errorf("failed to open %s - %s\n", configPath, err) } defer f.Close() d := json.NewDecoder(f) @@ -152,15 +153,13 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) { statePath := path.Join(dockerRootDir, self.ID, "state.json") if !utils.FileExists(statePath) { + // TODO(vishh): Return file name as well once we have a better error interface. err = fileNotFound return } f, err := os.Open(statePath) if err != nil { - if os.IsNotExist(err) { - err = container.NotActive - } - return + return nil, fmt.Errorf("failed to open %s - %s\n", statePath, err) } defer f.Close() d := json.NewDecoder(f) diff --git a/container/lmctfy/factory.go b/container/lmctfy/factory.go index b8782675..5de660c7 100644 --- a/container/lmctfy/factory.go +++ b/container/lmctfy/factory.go @@ -49,7 +49,6 @@ func (self *lmctfyFactory) NewContainerHandler(name string) (container.Container } func (self *lmctfyFactory) CanHandle(name string) bool { - // TODO(vmarmol): Try to attach to the container before blindly saying true. cmd := exec.Command(lmctfyBinary, "stats", "summary", name) _, err := cmd.Output() if err != nil { diff --git a/manager/container.go b/manager/container.go index 60f641fc..b366c9d6 100644 --- a/manager/container.go +++ b/manager/container.go @@ -127,7 +127,7 @@ func (c *containerData) housekeeping() { func (c *containerData) housekeepingTick() { err := c.updateStats() - if err != nil && err != container.NotActive { + if err != nil { log.Printf("Failed to update stats for container \"%s\": %s", c.info.Name, err) } } diff --git a/manager/manager.go b/manager/manager.go index 4ec6fc49..1f684f50 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -284,9 +284,7 @@ func (m *manager) detectContainers() error { for _, cont := range added { _, err = m.createContainer(cont.Name) if err != nil { - if err != container.NotActive { - log.Printf("failed to create existing container: %s: %s", cont.Name, err) - } + log.Printf("failed to create existing container: %s: %s", cont.Name, err) } } @@ -294,9 +292,7 @@ func (m *manager) detectContainers() error { for _, cont := range removed { err = m.destroyContainer(cont.Name) if err != nil { - if err != container.NotActive { - log.Printf("failed to destroy existing container: %s: %s", cont.Name, err) - } + log.Printf("failed to destroy existing container: %s: %s", cont.Name, err) } }