cluster: Check name uniqueness among pending containers.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2015-10-06 16:33:17 -07:00
parent c64ae5168a
commit 91279c8256
1 changed files with 16 additions and 8 deletions

View File

@ -122,9 +122,9 @@ func (c *Cluster) createContainer(config *cluster.ContainerConfig, name string,
c.scheduler.Lock() c.scheduler.Lock()
// Ensure the name is available // Ensure the name is available
if cID := c.getIDFromName(name); cID != "" { if !c.checkNameUniqueness(name) {
c.scheduler.Unlock() 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. // Associate a Swarm ID to the container we are creating.
@ -532,10 +532,10 @@ func (c *Cluster) Containers() cluster.Containers {
return out return out
} }
func (c *Cluster) getIDFromName(name string) string { func (c *Cluster) checkNameUniqueness(name string) bool {
// Abort immediately if the name is empty. // Abort immediately if the name is empty.
if len(name) == 0 { if len(name) == 0 {
return "" return true
} }
c.RLock() c.RLock()
@ -544,12 +544,20 @@ func (c *Cluster) getIDFromName(name string) string {
for _, c := range e.Containers() { for _, c := range e.Containers() {
for _, cname := range c.Names { for _, cname := range c.Names {
if cname == name || cname == "/"+name { 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 // Container returns the container with IDOrName in the cluster
@ -693,8 +701,8 @@ func (c *Cluster) RenameContainer(container *cluster.Container, newName string)
defer c.RUnlock() defer c.RUnlock()
// check new name whether available // check new name whether available
if cID := c.getIDFromName(newName); cID != "" { if !c.checkNameUniqueness(newName) {
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) 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 // call engine rename