Merge pull request #25888 from vdemeester/listcontainerfornode-refactoring

Move ListContainersForNode into cluster package
This commit is contained in:
Vincent Demeester 2016-08-25 19:10:18 +02:00 committed by GitHub
commit 29232a5d8e
3 changed files with 22 additions and 13 deletions

View file

@ -554,7 +554,11 @@ func (c *Cluster) Leave(force bool) error {
}
c.Unlock()
if nodeID := node.NodeID(); nodeID != "" {
for _, id := range c.config.Backend.ListContainersForNode(nodeID) {
nodeContainers, err := c.listContainerForNode(nodeID)
if err != nil {
return err
}
for _, id := range nodeContainers {
if err := c.config.Backend.ContainerRm(id, &apitypes.ContainerRmConfig{ForceRemove: true}); err != nil {
logrus.Errorf("error removing %v: %v", id, err)
}
@ -568,6 +572,22 @@ func (c *Cluster) Leave(force bool) error {
return nil
}
func (c *Cluster) listContainerForNode(nodeID string) ([]string, error) {
var ids []string
filters := filters.NewArgs()
filters.Add("label", fmt.Sprintf("com.docker.swarm.node.id=%s", nodeID))
containers, err := c.config.Backend.Containers(&apitypes.ContainerListOptions{
Filter: filters,
})
if err != nil {
return []string{}, err
}
for _, c := range containers {
ids = append(ids, c.ID)
}
return ids, nil
}
func (c *Cluster) clearState() error {
// todo: backup this data instead of removing?
if err := os.RemoveAll(c.root); err != nil {

View file

@ -34,7 +34,7 @@ type Backend interface {
ContainerKill(name string, sig uint64) error
SystemInfo() (*types.Info, error)
VolumeCreate(name, driverName string, opts, labels map[string]string) (*types.Volume, error)
ListContainersForNode(nodeID string) []string
Containers(config *types.ContainerListOptions) ([]*types.Container, error)
SetNetworkBootstrapKeys([]*networktypes.EncryptionKey) error
SetClusterProvider(provider cluster.Provider)
IsSwarmCompatible() error

View file

@ -102,17 +102,6 @@ func (daemon *Daemon) Containers(config *types.ContainerListOptions) ([]*types.C
return daemon.reduceContainers(config, daemon.transformContainer)
}
// ListContainersForNode returns all containerID that match the specified nodeID
func (daemon *Daemon) ListContainersForNode(nodeID string) []string {
var ids []string
for _, c := range daemon.List() {
if c.Config.Labels["com.docker.swarm.node.id"] == nodeID {
ids = append(ids, c.ID)
}
}
return ids
}
func (daemon *Daemon) filterByNameIDMatches(ctx *listContext) []*container.Container {
idSearch := false
names := ctx.filters.Get("name")