Change the interface of CanHandle to return error information.
This commit is contained in:
parent
797a4986ff
commit
adce0c5433
@ -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) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user