Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
cluster: Check name uniqueness among pending containers.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <[email protected]>
  • Loading branch information
aluzzardi committed Oct 7, 2015
1 parent ba075e0 commit b7260f1
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cluster/swarm/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,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.
Expand Down Expand Up @@ -532,10 +532,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()
Expand All @@ -544,12 +544,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
Expand Down Expand Up @@ -693,8 +701,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
Expand Down

0 comments on commit b7260f1

Please sign in to comment.