Merge pull request #823 from vieux/containers

create containers.go
This commit is contained in:
Victor Vieux 2015-05-26 13:05:26 -07:00
commit 9db6bce98b
5 changed files with 134 additions and 57 deletions

View File

@ -24,9 +24,11 @@ type Cluster interface {
RemoveImages(name string) ([]*dockerclient.ImageDelete, error)
// Return all containers
Containers() []*Container
Containers() Containers
// Return container the matching `IDOrName`
// TODO: remove this method from the interface as we can use
// cluster.Containers().Get(IDOrName)
Container(IDOrName string) *Container
// Pull images

View File

@ -1,6 +1,11 @@
package cluster
import "github.com/samalba/dockerclient"
import (
"strings"
"github.com/docker/docker/pkg/stringid"
"github.com/samalba/dockerclient"
)
// Container is exported
type Container struct {
@ -15,3 +20,65 @@ type Container struct {
func (c *Container) Refresh() error {
return c.Engine.refreshContainer(c.Id, true)
}
// Containers represents a list a containers
type Containers []*Container
// Get returns a container using it's ID or Name
func (containers Containers) Get(IDOrName string) *Container {
// Abort immediately if the name is empty.
if len(IDOrName) == 0 {
return nil
}
// Match exact or short Container ID.
for _, container := range containers {
if container.Id == IDOrName || stringid.TruncateID(container.Id) == IDOrName {
return container
}
}
// Match exact Swarm ID.
for _, container := range containers {
if swarmID := container.Config.SwarmID(); swarmID == IDOrName || stringid.TruncateID(swarmID) == IDOrName {
return container
}
}
candidates := []*Container{}
// Match name, /name or engine/name.
for _, container := range containers {
for _, name := range container.Names {
if name == IDOrName || name == "/"+IDOrName || container.Engine.ID+name == IDOrName || container.Engine.Name+name == IDOrName {
return container
}
}
}
if size := len(candidates); size == 1 {
return candidates[0]
} else if size > 1 {
return nil
}
// Match Container ID prefix.
for _, container := range containers {
if strings.HasPrefix(container.Id, IDOrName) {
candidates = append(candidates, container)
}
}
// Match Swarm ID prefix.
for _, container := range containers {
if strings.HasPrefix(container.Config.SwarmID(), IDOrName) {
candidates = append(candidates, container)
}
}
if len(candidates) == 1 {
return candidates[0]
}
return nil
}

58
cluster/container_test.go Normal file
View File

@ -0,0 +1,58 @@
package cluster
import (
"testing"
"github.com/samalba/dockerclient"
"github.com/stretchr/testify/assert"
)
func TestContainersGet(t *testing.T) {
containers := Containers([]*Container{{
Container: dockerclient.Container{
Id: "container1-id",
Names: []string{"/container1-name1", "/container1-name2"},
},
Engine: &Engine{ID: "test-engine"},
Config: BuildContainerConfig(dockerclient.ContainerConfig{
Labels: map[string]string{
"com.docker.swarm.id": "swarm1-id",
},
}),
}, {
Container: dockerclient.Container{
Id: "container2-id",
Names: []string{"/con"},
},
Engine: &Engine{ID: "test-engine"},
Config: BuildContainerConfig(dockerclient.ContainerConfig{
Labels: map[string]string{
"com.docker.swarm.id": "swarm2-id",
},
}),
}})
// Invalid lookup
assert.Nil(t, containers.Get("invalid-id"))
assert.Nil(t, containers.Get(""))
// Container ID lookup.
assert.NotNil(t, containers.Get("container1-id"))
// Container ID prefix lookup.
assert.NotNil(t, containers.Get("container1-"))
assert.Nil(t, containers.Get("container"))
// Container name lookup.
assert.NotNil(t, containers.Get("container1-name1"))
assert.NotNil(t, containers.Get("container1-name2"))
// Container engine/name matching.
assert.NotNil(t, containers.Get("test-engine/container1-name1"))
assert.NotNil(t, containers.Get("test-engine/container1-name2"))
// Swarm ID lookup.
assert.NotNil(t, containers.Get("swarm1-id"))
// Swarm ID prefix lookup.
assert.NotNil(t, containers.Get("swarm1-"))
assert.Nil(t, containers.Get("swarm"))
// Get name before ID prefix
cc := containers.Get("con")
assert.NotNil(t, cc)
assert.Equal(t, cc.Id, "container2-id")
}

View File

@ -452,9 +452,9 @@ func (e *Engine) RegisterEventHandler(h EventHandler) error {
}
// Containers returns all the containers in the engine.
func (e *Engine) Containers() []*Container {
func (e *Engine) Containers() Containers {
e.RLock()
containers := make([]*Container, 0, len(e.containers))
containers := Containers{}
for _, container := range e.containers {
containers = append(containers, container)
}

View File

@ -378,11 +378,11 @@ func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)
}
// Containers returns all the containers in the cluster.
func (c *Cluster) Containers() []*cluster.Container {
func (c *Cluster) Containers() cluster.Containers {
c.RLock()
defer c.RUnlock()
out := []*cluster.Container{}
out := cluster.Containers{}
for _, n := range c.engines {
out = append(out, n.Containers()...)
}
@ -420,58 +420,8 @@ func (c *Cluster) Container(IDOrName string) *cluster.Container {
c.RLock()
defer c.RUnlock()
containers := c.Containers()
return cluster.Containers(c.Containers()).Get(IDOrName)
// Match exact or short Container ID.
for _, container := range containers {
if container.Id == IDOrName || stringid.TruncateID(container.Id) == IDOrName {
return container
}
}
// Match exact Swarm ID.
for _, container := range containers {
if swarmID := container.Config.SwarmID(); swarmID == IDOrName || stringid.TruncateID(swarmID) == IDOrName {
return container
}
}
candidates := []*cluster.Container{}
// Match name, /name or engine/name.
for _, container := range containers {
for _, name := range container.Names {
if name == IDOrName || name == "/"+IDOrName || container.Engine.ID+name == IDOrName || container.Engine.Name+name == IDOrName {
return container
}
}
}
if size := len(candidates); size == 1 {
return candidates[0]
} else if size > 1 {
return nil
}
// Match Container ID prefix.
for _, container := range containers {
if strings.HasPrefix(container.Id, IDOrName) {
candidates = append(candidates, container)
}
}
// Match Swarm ID prefix.
for _, container := range containers {
if strings.HasPrefix(container.Config.SwarmID(), IDOrName) {
candidates = append(candidates, container)
}
}
if len(candidates) == 1 {
return candidates[0]
}
return nil
}
// listNodes returns all the engines in the cluster.