From eac4981da8f082ec44fed581e98a74f6c74b73f5 Mon Sep 17 00:00:00 2001 From: Ezra Silvera Date: Wed, 11 May 2016 12:15:04 +0300 Subject: [PATCH 1/2] check unique container name Signed-off-by: Ezra Silvera --- cluster/mesos/cluster.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index e1c5544682..59922fb5cd 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -196,6 +196,10 @@ func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, return nil, errResourcesNeeded } + if !c.checkNameUniqueness(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) + } + task, err := task.NewTask(config, name, c.taskCreationTimeout) if err != nil { return nil, err @@ -678,3 +682,27 @@ func (c *Cluster) BuildImage(buildContext io.Reader, buildImage *types.ImageBuil func (c *Cluster) TagImage(IDOrName string, ref string, force bool) error { return errNotSupported } + +func (c *Cluster) checkNameUniqueness(name string) bool { + // Abort immediately if the name is empty. + if len(name) == 0 { + return true + } + + c.RLock() + defer c.RUnlock() + + for _, s := range c.agents { + for _, container := range s.engine.Containers() { + for _, cname := range container.Names { + log.Debugf("*************** cont:%v name:%v", container.Info.ID, cname) + if cname == name || cname == "/"+name { + return false + } + } + } + } + + + return true +} From 265c55023c31aed4922bbda7c0e60b388d543c06 Mon Sep 17 00:00:00 2001 From: Ezra Silvera Date: Wed, 11 May 2016 13:55:54 +0300 Subject: [PATCH 2/2] Check in pending tasks as well Signed-off-by: Ezra Silvera --- cluster/mesos/cluster.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index 59922fb5cd..63af49c7ba 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -197,7 +197,7 @@ func (c *Cluster) CreateContainer(config *cluster.ContainerConfig, name string, } if !c.checkNameUniqueness(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) + return nil, fmt.Errorf("Conflict: The name %s is already assigned or in pending tasks. You have to delete (or rename) that container to be able to assign %s to a container again.", name, name) } task, err := task.NewTask(config, name, c.taskCreationTimeout) @@ -386,6 +386,11 @@ func (c *Cluster) Import(source string, ref string, tag string, imageReader io.R // RenameContainer renames a container func (c *Cluster) RenameContainer(container *cluster.Container, newName string) error { //FIXME this doesn't work as the next refreshcontainer will erase this change (this change is in-memory only) + + if !c.checkNameUniqueness(newName) { + return fmt.Errorf("Conflict: The name %s is already assigned, or in pending tasks. You have to delete (or rename) that container to be able to assign %s to a container again.", newName, newName) + } + container.Config.Labels[cluster.SwarmLabelNamespace+".mesos.name"] = newName return nil @@ -695,7 +700,6 @@ func (c *Cluster) checkNameUniqueness(name string) bool { for _, s := range c.agents { for _, container := range s.engine.Containers() { for _, cname := range container.Names { - log.Debugf("*************** cont:%v name:%v", container.Info.ID, cname) if cname == name || cname == "/"+name { return false } @@ -703,6 +707,16 @@ func (c *Cluster) checkNameUniqueness(name string) bool { } } + for _, task := range c.pendingTasks.Tasks { + config := task.GetConfig() + if config.Labels != nil { + if tname, ok := config.Labels[cluster.SwarmLabelNamespace+".mesos.name"]; ok { + if tname == name || tname == "/"+name { + return false + } + } + } + } return true }