Skip to content

Commit

Permalink
List namespace right after namespace has been created (#6922)
Browse files Browse the repository at this point in the history
* List namespace right after namespace has been created

Signed-off-by: Parthvi Vala <[email protected]>

* Add sleep after listing namespaces

Signed-off-by: Parthvi Vala <[email protected]>

* Error out when timeout is reached

Signed-off-by: Parthvi Vala <[email protected]>

* Modify spinner messages

Signed-off-by: Parthvi Vala <[email protected]>

* Attempt at fixing doc tests

Signed-off-by: Parthvi Vala <[email protected]>

---------

Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi authored Jun 27, 2023
1 parent 324d73f commit 3bf5ffc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
53 changes: 37 additions & 16 deletions pkg/odo/cli/create/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/helper/helper_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/cmd_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3bf5ffc

Please sign in to comment.