diff --git a/container/docker/factory.go b/container/docker/factory.go index 6b7271e6..8ad8bd29 100644 --- a/container/docker/factory.go +++ b/container/docker/factory.go @@ -63,20 +63,19 @@ func (self *dockerFactory) NewContainerHandler(name string) (handler container.C } // Docker handles all containers under /docker -// TODO(vishh): Change the CanHandle interface to be able to return errors. -func (self *dockerFactory) CanHandle(name string) bool { +func (self *dockerFactory) CanHandle(name string) (bool, error) { // In systemd systems the containers are: /system.slice/docker-{ID} if self.useSystemd { if !strings.HasPrefix(name, "/system.slice/docker-") { - return false + return false, fmt.Errorf("Expected path prefix /system.slice/docker- but got %s", name) } } else if name == "/" { - return false + return false, nil } else if name == "/docker" { // We need the docker driver to handle /docker. Otherwise the aggregation at the API level will break. - return true + return true, nil } else if !strings.HasPrefix(name, "/docker/") { - return false + return false, nil } // Check if the container is known to docker and it is active. id := path.Base(name) @@ -84,10 +83,10 @@ func (self *dockerFactory) CanHandle(name string) bool { // 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 { - return false + return false, fmt.Errorf("Error inspecting container: %v", err) } - return true + return true, nil } func parseDockerVersion(full_version_string string) ([]int, error) { diff --git a/container/factory.go b/container/factory.go index 5386f577..0885fad1 100644 --- a/container/factory.go +++ b/container/factory.go @@ -26,7 +26,7 @@ type ContainerHandlerFactory interface { NewContainerHandler(name string) (ContainerHandler, error) // Returns whether this factory can handle the specified container. - CanHandle(name string) bool + CanHandle(name string) (bool, error) // Name of the factory. String() string @@ -55,7 +55,11 @@ func NewContainerHandler(name string) (ContainerHandler, error) { // Create the ContainerHandler with the first factory that supports it. for _, factory := range factories { - if factory.CanHandle(name) { + canHandle, err := factory.CanHandle(name) + if err != nil { + return nil, fmt.Errorf("Error trying to work out if we can hande %s: %v", name, err) + } + if canHandle { glog.V(1).Infof("Using factory %q for container %q", factory.String(), name) return factory.NewContainerHandler(name) } diff --git a/container/factory_test.go b/container/factory_test.go index b96caab5..45b7945d 100644 --- a/container/factory_test.go +++ b/container/factory_test.go @@ -30,8 +30,8 @@ func (self *mockContainerHandlerFactory) String() string { return self.Name } -func (self *mockContainerHandlerFactory) CanHandle(name string) bool { - return self.CanHandleValue +func (self *mockContainerHandlerFactory) CanHandle(name string) (bool, error) { + return self.CanHandleValue, nil } func (self *mockContainerHandlerFactory) NewContainerHandler(name string) (ContainerHandler, error) { diff --git a/container/raw/factory.go b/container/raw/factory.go index e20f5638..569c62c7 100644 --- a/container/raw/factory.go +++ b/container/raw/factory.go @@ -46,8 +46,8 @@ func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHan } // The raw factory can handle any container. -func (self *rawFactory) CanHandle(name string) bool { - return true +func (self *rawFactory) CanHandle(name string) (bool, error) { + return true, nil } func Register(machineInfoFactory info.MachineInfoFactory) error {