Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List namespace right after namespace has been created #6922

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
rm3l marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a sleep for let's say 50ms at this point, to not respawn the List operation too fast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea.

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