List namespace right after namespace has been created (#6922)

* List namespace right after namespace has been created

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add sleep after listing namespaces

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Error out when timeout is reached

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Modify spinner messages

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Attempt at fixing doc tests

Signed-off-by: Parthvi Vala <pvala@redhat.com>

---------

Signed-off-by: Parthvi Vala <pvala@redhat.com>
This commit is contained in:
Parthvi Vala
2023-06-27 19:32:54 +05:30
committed by GitHub
parent 324d73fb37
commit 3bf5ffc369
7 changed files with 58 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
```console
$ odo create namespace odo-dev
✓ Creating the namespace "odo-dev" [1s]
✓ Namespace "odo-dev" is ready for use
✓ New namespace created and now using namespace: odo-dev
```

View File

@@ -1,5 +1,6 @@
```console
$ odo create project odo-dev
✓ Creating the project "odo-dev" [1s]
✓ Project "odo-dev" is ready for use
✓ New project created and now using project: odo-dev
```

View File

@@ -1,5 +1,6 @@
```console
$ odo create namespace odo-dev
✓ Creating the namespace "odo-dev" [1s]
✓ Namespace "odo-dev" is ready for use
✓ New namespace created and now using namespace: odo-dev
```

View File

@@ -1,5 +1,6 @@
```console
$ odo create project odo-dev
✓ Creating the project "odo-dev" [1s]
✓ Project "odo-dev" is ready for use
✓ New project created and now using project: odo-dev
```

View File

@@ -3,10 +3,12 @@ package namespace
import (
"context"
"fmt"
"os"
"github.com/redhat-developer/odo/pkg/project"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/klog"
"os"
"time"
dfutil "github.com/devfile/library/v2/pkg/util"
"github.com/spf13/cobra"
@@ -80,32 +82,51 @@ func (nco *NamespaceCreateOptions) Validate(ctx context.Context) error {
// Run runs the namespace create command
func (nco *NamespaceCreateOptions) Run(ctx context.Context) (err error) {
// Create the "spinner"
s := &log.Status{}
// If the --wait parameter has been passed, we add a spinner..
if nco.waitFlag {
s = log.Spinnerf("Waiting for %s to come up", nco.commandName)
defer s.End(false)
}
createSpinner := log.Spinnerf("Creating the %s %q", nco.commandName, nco.namespaceName)
defer createSpinner.End(false)
// Create the namespace & end the spinner (if there is any..)
err = nco.clientset.ProjectClient.Create(nco.namespaceName, nco.waitFlag)
if err != nil {
return err
}
s.End(true)
createSpinner.End(true)
caser := cases.Title(language.Und)
successMessage := fmt.Sprintf(`%s %q is ready for use`, caser.String(nco.commandName), nco.namespaceName)
log.Successf(successMessage)
// If the --wait parameter has been passed, we add a spinner..
if nco.waitFlag {
waitSpinner := log.Spinnerf("Waiting for the %s to come up", nco.commandName)
defer waitSpinner.End(false)
timeOut := time.After(nco.clientset.PreferenceClient.GetTimeout())
L:
for {
select {
case <-timeOut:
return fmt.Errorf("timeout while waiting for %s %q to be ready; you can change the timeout preference by running `odo preference set timeout <duration>`", nco.commandName, nco.namespaceName)
default:
var nsList project.ProjectList
nsList, err = nco.clientset.ProjectClient.List()
if err != nil {
klog.V(4).Infof("Failed to list %ss", nco.commandName)
}
for _, ns := range nsList.Items {
if ns.Name == nco.namespaceName {
break L
}
}
time.Sleep(50 * time.Millisecond)
}
}
waitSpinner.End(true)
}
// Set the current namespace when created
err = nco.clientset.ProjectClient.SetCurrent(nco.namespaceName)
if err != nil {
return err
}
caser := cases.Title(language.Und)
successMessage := fmt.Sprintf(`%s %q is ready for use`, caser.String(nco.commandName), nco.namespaceName)
log.Successf(successMessage)
log.Successf("New %[1]s created and now using %[1]s: %v", nco.commandName, nco.namespaceName)
return nil
@@ -134,7 +155,7 @@ func NewCmdNamespaceCreate(name, fullName string, testClientset clientset.Client
namespaceCreateCmd.Flags().BoolVarP(&o.waitFlag, "wait", "w", false, "Wait until the namespace is ready")
clientset.Add(namespaceCreateCmd, clientset.KUBERNETES, clientset.PROJECT)
clientset.Add(namespaceCreateCmd, clientset.KUBERNETES, clientset.PROJECT, clientset.PREFERENCE)
util.SetCommandGroup(namespaceCreateCmd, util.MainGroup)
return namespaceCreateCmd

View File

@@ -41,7 +41,9 @@ func StripSpinner(docString string) (returnString string) {
if (strings.HasPrefix(line, "• Downloading") ||
strings.HasPrefix(line, "• Syncing") ||
strings.HasPrefix(line, "• Building") ||
strings.HasPrefix(line, "• Waiting for the application")) &&
strings.HasPrefix(line, "• Waiting for the application") ||
strings.HasPrefix(line, "• Creating the namespace") ||
strings.HasPrefix(line, "• Creating the project")) &&
strings.HasSuffix(line, "...") {
continue
}

View File

@@ -25,6 +25,20 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() {
AfterEach(func() {
helper.CommonAfterEach(commonVar)
})
When("namespace is created with -w", func() {
// Ref: https://github.com/redhat-developer/odo/issues/6827
var namespace string
BeforeEach(func() {
namespace = helper.GetProjectName()
helper.Cmd("odo", "create", "namespace", namespace, "--wait").ShouldPass()
})
It("should list the new namespace when listing namespace", func() {
out := helper.Cmd("odo", "list", "namespace").ShouldPass().Out()
Expect(out).To(ContainSubstring(namespace))
})
})
for _, commandName := range []string{"namespace", "project"} {
// this is a workaround to ensure that the for loop works with `It` blocks
commandName := commandName