Change the interface of CanHandle to return error information.

This commit is contained in:
Satnam Singh 2014-09-26 10:13:32 -07:00
parent 797a4986ff
commit adce0c5433
4 changed files with 17 additions and 14 deletions

View File

@ -63,20 +63,19 @@ func (self *dockerFactory) NewContainerHandler(name string) (handler container.C
} }
// Docker handles all containers under /docker // 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, error) {
func (self *dockerFactory) CanHandle(name string) bool {
// In systemd systems the containers are: /system.slice/docker-{ID} // In systemd systems the containers are: /system.slice/docker-{ID}
if self.useSystemd { if self.useSystemd {
if !strings.HasPrefix(name, "/system.slice/docker-") { 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 == "/" { } else if name == "/" {
return false return false, nil
} else if name == "/docker" { } else if name == "/docker" {
// We need the docker driver to handle /docker. Otherwise the aggregation at the API level will break. // 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/") { } else if !strings.HasPrefix(name, "/docker/") {
return false return false, nil
} }
// 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 := path.Base(name) 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. // 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 {
return false return false, fmt.Errorf("Error inspecting container: %v", err)
} }
return true return true, nil
} }
func parseDockerVersion(full_version_string string) ([]int, error) { func parseDockerVersion(full_version_string string) ([]int, error) {

View File

@ -26,7 +26,7 @@ type ContainerHandlerFactory interface {
NewContainerHandler(name string) (ContainerHandler, error) NewContainerHandler(name string) (ContainerHandler, error)
// Returns whether this factory can handle the specified container. // Returns whether this factory can handle the specified container.
CanHandle(name string) bool CanHandle(name string) (bool, error)
// Name of the factory. // Name of the factory.
String() string String() string
@ -55,7 +55,11 @@ func NewContainerHandler(name string) (ContainerHandler, error) {
// Create the ContainerHandler with the first factory that supports it. // Create the ContainerHandler with the first factory that supports it.
for _, factory := range factories { 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) glog.V(1).Infof("Using factory %q for container %q", factory.String(), name)
return factory.NewContainerHandler(name) return factory.NewContainerHandler(name)
} }

View File

@ -30,8 +30,8 @@ func (self *mockContainerHandlerFactory) String() string {
return self.Name return self.Name
} }
func (self *mockContainerHandlerFactory) CanHandle(name string) bool { func (self *mockContainerHandlerFactory) CanHandle(name string) (bool, error) {
return self.CanHandleValue return self.CanHandleValue, nil
} }
func (self *mockContainerHandlerFactory) NewContainerHandler(name string) (ContainerHandler, error) { func (self *mockContainerHandlerFactory) NewContainerHandler(name string) (ContainerHandler, error) {

View File

@ -46,8 +46,8 @@ func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHan
} }
// The raw factory can handle any container. // The raw factory can handle any container.
func (self *rawFactory) CanHandle(name string) bool { func (self *rawFactory) CanHandle(name string) (bool, error) {
return true return true, nil
} }
func Register(machineInfoFactory info.MachineInfoFactory) error { func Register(machineInfoFactory info.MachineInfoFactory) error {