[lxc-devel] [lxd/master] retry generating petnames

tych0 on Github lxc-bot at linuxcontainers.org
Tue Aug 23 13:58:15 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 500 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160823/05356e81/attachment.bin>
-------------- next part --------------
From 59cf54df5164db543c03df9b64e5d10d505d2527 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Tue, 23 Aug 2016 09:56:33 -0400
Subject: [PATCH] retry generating petnames

newer versions of petname have a smaller name list, which increases the
liklehood of conflicts. let's retry a few times to avoid them.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 lxd/containers_post.go | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lxd/containers_post.go b/lxd/containers_post.go
index fe44fd5..10bac2f 100644
--- a/lxd/containers_post.go
+++ b/lxd/containers_post.go
@@ -394,7 +394,23 @@ func containersPost(d *Daemon, r *http.Request) Response {
 	}
 
 	if req.Name == "" {
-		req.Name = strings.ToLower(petname.Generate(2, "-"))
+		cs, err := dbContainersList(d.db, cTypeRegular)
+		if err != nil {
+			return InternalError(err)
+		}
+
+		i := 0
+		for {
+			i++
+			req.Name = strings.ToLower(petname.Generate(2, "-"))
+			if !shared.StringInSlice(req.Name, cs) {
+				break
+			}
+
+			if i > 100 {
+				return InternalError(fmt.Errorf("couldn't generate a new unique name after 100 tries"))
+			}
+		}
 		shared.Debugf("No name provided, creating %s", req.Name)
 	}
 


More information about the lxc-devel mailing list