Removed NotActive error message from container package.

Imporved error messages.
This commit is contained in:
Vishnu Kannan 2014-07-22 17:30:38 +00:00
parent ef13440034
commit 5dfa7b64ba
6 changed files with 19 additions and 34 deletions

View File

@ -14,11 +14,7 @@
package container package container
import ( import "github.com/google/cadvisor/info"
"errors"
"github.com/google/cadvisor/info"
)
// Listing types. // Listing types.
const ( const (
@ -26,8 +22,6 @@ const (
LIST_RECURSIVE LIST_RECURSIVE
) )
var NotActive = errors.New("Container is not active")
type ListType int type ListType int
// Interface for container operation handlers. // Interface for container operation handlers.

View File

@ -28,16 +28,15 @@ import (
"github.com/google/cadvisor/info" "github.com/google/cadvisor/info"
) )
var ( var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint")
ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint")
defaultClient *docker.Client
)
type dockerFactory struct { type dockerFactory struct {
machineInfoFactory info.MachineInfoFactory machineInfoFactory info.MachineInfoFactory
// Whether this system is using systemd. // Whether this system is using systemd.
useSystemd bool useSystemd bool
client *docker.Client
} }
func (self *dockerFactory) String() string { func (self *dockerFactory) String() string {
@ -77,10 +76,7 @@ func (self *dockerFactory) CanHandle(name string) bool {
if err != nil { if err != nil {
return false return false
} }
if defaultClient == nil { ctnr, err := self.client.InspectContainer(id)
log.Fatal("Default docker client is nil")
}
ctnr, err := defaultClient.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.
// TODO(vishh): Detect lxc containers and avoid handling them. // TODO(vishh): Detect lxc containers and avoid handling them.
if err != nil || !ctnr.State.Running { 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! // Register root container before running this function!
func Register(factory info.MachineInfoFactory) (err error) { func Register(factory info.MachineInfoFactory) error {
defaultClient, err = docker.NewClient(*ArgDockerEndpoint) client, err := docker.NewClient(*ArgDockerEndpoint)
if err != nil { if err != nil {
return fmt.Errorf("unable to communicate with docker daemon: %v", err) 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) return fmt.Errorf("unable to communicate with docker daemon: %v", err)
} else { } else {
expected_version := []int{0, 11, 1} expected_version := []int{0, 11, 1}
@ -135,6 +131,7 @@ func Register(factory info.MachineInfoFactory) (err error) {
f := &dockerFactory{ f := &dockerFactory{
machineInfoFactory: factory, machineInfoFactory: factory,
useSystemd: systemd.UseSystemd(), useSystemd: systemd.UseSystemd(),
client: client,
} }
log.Printf("Registering Docker factory") log.Printf("Registering Docker factory")
container.RegisterContainerHandlerFactory(f) container.RegisterContainerHandlerFactory(f)

View File

@ -71,8 +71,8 @@ func newDockerContainerHandler(
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 || !ctnr.State.Running { if err != nil {
return nil, container.NotActive return nil, fmt.Errorf("failed to inspect container %s - %s\n", id, err)
} }
handler.aliases = append(handler.aliases, path.Join("/docker", ctnr.Name)) handler.aliases = append(handler.aliases, path.Join("/docker", ctnr.Name))
return handler, nil return handler, nil
@ -126,16 +126,17 @@ func splitName(containerName string) (string, string, error) {
return parent, id, nil 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.
err = fileNotFound err = fileNotFound
return return
} }
f, err := os.Open(configPath) f, err := os.Open(configPath)
if err != nil { if err != nil {
return return nil, fmt.Errorf("failed to open %s - %s\n", configPath, err)
} }
defer f.Close() defer f.Close()
d := json.NewDecoder(f) d := json.NewDecoder(f)
@ -152,15 +153,13 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain
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.
err = fileNotFound err = fileNotFound
return return
} }
f, err := os.Open(statePath) f, err := os.Open(statePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { return nil, fmt.Errorf("failed to open %s - %s\n", statePath, err)
err = container.NotActive
}
return
} }
defer f.Close() defer f.Close()
d := json.NewDecoder(f) d := json.NewDecoder(f)

View File

@ -49,7 +49,6 @@ func (self *lmctfyFactory) NewContainerHandler(name string) (container.Container
} }
func (self *lmctfyFactory) CanHandle(name string) bool { 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) cmd := exec.Command(lmctfyBinary, "stats", "summary", name)
_, err := cmd.Output() _, err := cmd.Output()
if err != nil { if err != nil {

View File

@ -127,7 +127,7 @@ func (c *containerData) housekeeping() {
func (c *containerData) housekeepingTick() { func (c *containerData) housekeepingTick() {
err := c.updateStats() 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) log.Printf("Failed to update stats for container \"%s\": %s", c.info.Name, err)
} }
} }

View File

@ -284,21 +284,17 @@ func (m *manager) detectContainers() error {
for _, cont := range added { for _, cont := range added {
_, err = m.createContainer(cont.Name) _, err = m.createContainer(cont.Name)
if err != nil { 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)
} }
} }
}
// Remove the old containers. // Remove the old containers.
for _, cont := range removed { for _, cont := range removed {
err = m.destroyContainer(cont.Name) err = m.destroyContainer(cont.Name)
if err != nil { 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)
} }
} }
}
return nil return nil
} }