From aa5aec946cc68e625f3c1d807d77a2f26e2637eb Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Tue, 6 Oct 2015 16:33:17 -0700 Subject: [PATCH] cluster: Check name uniqueness among pending containers. Signed-off-by: Andrea Luzzardi --- cluster/swarm/cluster.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 2330684d88..a37d429f27 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -121,9 +121,9 @@ func (c *Cluster) createContainer(config *cluster.ContainerConfig, name string, c.scheduler.Lock() // Ensure the name is available - if cID := c.getIDFromName(name); cID != "" { + if !c.checkNameUniqueness(name) { c.scheduler.Unlock() - return nil, fmt.Errorf("Conflict, The name %s is already assigned to %s. You have to delete (or rename) that container to be able to assign %s to a container again.", name, cID, name) + return nil, fmt.Errorf("Conflict, The name %s is already assigned. You have to delete (or rename) that container to be able to assign %s to a container again.", name, name) } // Associate a Swarm ID to the container we are creating. @@ -531,10 +531,10 @@ func (c *Cluster) Containers() cluster.Containers { return out } -func (c *Cluster) getIDFromName(name string) string { +func (c *Cluster) checkNameUniqueness(name string) bool { // Abort immediately if the name is empty. if len(name) == 0 { - return "" + return true } c.RLock() @@ -543,12 +543,20 @@ func (c *Cluster) getIDFromName(name string) string { for _, c := range e.Containers() { for _, cname := range c.Names { if cname == name || cname == "/"+name { - return c.Id + return false } } } } - return "" + + // check pending containers. + for _, c := range c.pendingContainers { + if c.Name == name { + return false + } + } + + return true } // Container returns the container with IDOrName in the cluster @@ -692,8 +700,8 @@ func (c *Cluster) RenameContainer(container *cluster.Container, newName string) defer c.RUnlock() // check new name whether available - if cID := c.getIDFromName(newName); cID != "" { - return fmt.Errorf("Conflict, The name %s is already assigned to %s. You have to delete (or rename) that container to be able to assign %s to a container again.", newName, cID, newName) + if !c.checkNameUniqueness(newName) { + return fmt.Errorf("Conflict, The name %s is already assigned. You have to delete (or rename) that container to be able to assign %s to a container again.", newName, newName) } // call engine rename