From 9b191d3983fb0a48bcb1839f5d7bd2a0bfdd77ed Mon Sep 17 00:00:00 2001 From: Dan Ramich Date: Thu, 17 May 2018 13:34:16 -0700 Subject: [PATCH] Wait for namespace to be ready Problem: An app creates a namespace to deploy into and depending on the system it might not be ready when attempting to install the app Solution: Poll the namespace to ensure it's active before proceeding with installing the app --- cmd/app.go | 21 ++++++++++++++++++++- cmd/namespace.go | 3 +++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/app.go b/cmd/app.go index 7442b347..0d9f791d 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -16,6 +16,7 @@ import ( clusterClient "github.com/rancher/types/client/cluster/v3" managementClient "github.com/rancher/types/client/management/v3" projectClient "github.com/rancher/types/client/project/v3" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -687,10 +688,28 @@ func createNamespace(c *cliclient.MasterClient, n string) error { ProjectID: c.UserConfig.Project, } - _, err = c.ClusterClient.Namespace.Create(newNamespace) + ns, err := c.ClusterClient.Namespace.Create(newNamespace) if err != nil { return err } + + startTime := time.Now() + for { + logrus.Debug(fmt.Sprintf("Namespace create wait - Name: %s, State: %s, Transitioning: %s", ns.Name, ns.State, ns.Transitioning)) + if time.Since(startTime)/time.Second > 30 { + return fmt.Errorf("timed out waiting for new namespace %s", ns.Name) + } + ns, err = c.ClusterClient.Namespace.ByID(ns.ID) + if err != nil { + return err + } + + if ns.State == "active" { + break + } + + time.Sleep(500 * time.Millisecond) + } } else { if namespaces.Data[0].ProjectID != c.UserConfig.Project { return fmt.Errorf("namespace %s already exists", n) diff --git a/cmd/namespace.go b/cmd/namespace.go index 2ed8c204..25aa1197 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -19,6 +19,9 @@ func NamespaceCommand() cli.Command { Aliases: []string{"namespace"}, Usage: "Operations on namespaces", Action: defaultAction(namespaceLs), + Flags: []cli.Flag{ + quietFlag, + }, Subcommands: []cli.Command{ { Name: "ls",