forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
odo set namespace/project
(redhat-developer#5744)
* Implement 'set namespace/project' * Add doc for 'set namespace/project' * Add integration tests * Group tests in cmd_namespace_test.go to make them easier to understand and debug * Restructure 'set namespace' tests * Check for namespace/project existence with '{oc,kubectl} get {namespace,project} <name>' For some reason, checking by listing all namespaces/projects could end up returning an outdated list.
- Loading branch information
Showing
8 changed files
with
359 additions
and
59 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
docs/website/versioned_docs/version-3.0.0/command-reference/set-namespace.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: odo set namespace | ||
sidebar_position: 3 | ||
--- | ||
|
||
`odo set namespace` lets you set a namespace/project as the current active one in your local `kubeconfig` configuration. | ||
|
||
Executing this command inside a component directory will not update the namespace or project of the existing component. | ||
|
||
To set the current active namespace you can run `odo set namespace <name>`: | ||
```shell | ||
odo set namespace mynamespace | ||
``` | ||
|
||
Example: | ||
```shell | ||
odo set namespace mynamespace | ||
✓ Current active namespace set to "mynamespace" | ||
``` | ||
|
||
Optionally, you can also use `project` as an alias to `namespace`. | ||
|
||
To set the current active project you can run `odo set project <name>`: | ||
```shell | ||
odo set project myproject | ||
``` | ||
|
||
Example: | ||
```shell | ||
odo set project myproject | ||
✓ Current active project set to "myproject" | ||
``` | ||
|
||
:::note | ||
This command updates your current `kubeconfig` configuration, using either of the aliases. | ||
So running either `odo set project` or `odo set namespace` performs the exact same operation in your configuration. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package namespace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
dfutil "github.com/devfile/library/pkg/util" | ||
|
||
"github.com/redhat-developer/odo/pkg/devfile/location" | ||
"github.com/redhat-developer/odo/pkg/log" | ||
"github.com/redhat-developer/odo/pkg/odo/cmdline" | ||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions" | ||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset" | ||
scontext "github.com/redhat-developer/odo/pkg/segment/context" | ||
|
||
"k8s.io/klog" | ||
ktemplates "k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const RecommendedCommandName = "namespace" | ||
|
||
var ( | ||
setExample = ktemplates.Examples(` | ||
# Set the specified namespace as the current active namespace in the config | ||
%[1]s my-namespace | ||
`) | ||
|
||
setLongDesc = ktemplates.LongDesc(`Set the current active namespace. | ||
If executed inside a component directory, this command will not update the namespace of the existing component. | ||
`) | ||
|
||
setShortDesc = `Set the current active namespace` | ||
) | ||
|
||
// SetOptions encapsulates the options for the odo namespace create command | ||
type SetOptions struct { | ||
// Context | ||
*genericclioptions.Context | ||
|
||
// Clients | ||
clientset *clientset.Clientset | ||
|
||
// Destination directory | ||
contextDir string | ||
|
||
// Parameters | ||
namespaceName string | ||
|
||
// value can be either 'project' or 'namespace', depending on what command is called | ||
commandName string | ||
} | ||
|
||
// NewSetOptions creates a SetOptions instance | ||
func NewSetOptions() *SetOptions { | ||
return &SetOptions{} | ||
} | ||
|
||
func (so *SetOptions) SetClientset(clientset *clientset.Clientset) { | ||
so.clientset = clientset | ||
} | ||
|
||
// Complete completes SetOptions after they've been created | ||
func (so *SetOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) { | ||
so.namespaceName = args[0] | ||
so.Context, err = genericclioptions.New(genericclioptions.NewCreateParameters(cmdline)) | ||
if err != nil { | ||
return err | ||
} | ||
so.contextDir, err = so.clientset.FS.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
if scontext.GetTelemetryStatus(cmdline.Context()) { | ||
scontext.SetClusterType(cmdline.Context(), so.KClient) | ||
} | ||
return nil | ||
} | ||
|
||
// Validate validates the parameters of the SetOptions | ||
func (so *SetOptions) Validate() error { | ||
return dfutil.ValidateK8sResourceName("namespace name", so.namespaceName) | ||
} | ||
|
||
// Run runs the 'set namespace' command | ||
func (so *SetOptions) Run(ctx context.Context) error { | ||
devfilePresent, err := location.DirectoryContainsDevfile(so.clientset.FS, so.contextDir) | ||
if err != nil { | ||
// Purposely ignoring the error, as it is not mandatory for this command | ||
klog.V(2).Infof("Unexpected error while checking if running inside a component directory: %v", err) | ||
} | ||
if devfilePresent { | ||
log.Warningf("This is being executed inside a component directory. This will not update the %s of the existing component", | ||
so.commandName) | ||
} | ||
|
||
err = so.clientset.ProjectClient.SetCurrent(so.namespaceName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Successf("Current active %[1]s set to %q", so.commandName, so.namespaceName) | ||
|
||
return nil | ||
} | ||
|
||
// NewCmdNamespaceSet creates the 'set namespace' command | ||
func NewCmdNamespaceSet(name, fullName string) *cobra.Command { | ||
o := NewSetOptions() | ||
// To help the UI messages deal better with namespace vs project | ||
o.commandName = name | ||
if len(os.Args) > 2 { | ||
o.commandName = os.Args[2] | ||
} | ||
|
||
namespaceSetCmd := &cobra.Command{ | ||
Use: name, | ||
Short: setShortDesc, | ||
Long: setLongDesc, | ||
Example: fmt.Sprintf(setExample, fullName), | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
genericclioptions.GenericRun(o, cmd, args) | ||
}, | ||
Annotations: map[string]string{"command": "main"}, | ||
Aliases: []string{"project"}, | ||
} | ||
|
||
clientset.Add(namespaceSetCmd, clientset.FILESYSTEM, clientset.PROJECT) | ||
|
||
return namespaceSetCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package set | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/redhat-developer/odo/pkg/odo/cli/set/namespace" | ||
"github.com/redhat-developer/odo/pkg/odo/util" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RecommendedCommandName is the recommended namespace command name | ||
const RecommendedCommandName = "set" | ||
|
||
// NewCmdSet implements the namespace odo command | ||
func NewCmdSet(name, fullName string) *cobra.Command { | ||
|
||
namespaceSetCmd := namespace.NewCmdNamespaceSet(namespace.RecommendedCommandName, | ||
util.GetFullName(fullName, namespace.RecommendedCommandName)) | ||
setCmd := &cobra.Command{ | ||
Use: name + " [options]", | ||
Short: "Perform set operation", | ||
Long: "Perform set operation", | ||
Example: fmt.Sprintf("%s\n", | ||
namespaceSetCmd.Example, | ||
), | ||
Annotations: map[string]string{"command": "main"}, | ||
} | ||
|
||
setCmd.AddCommand(namespaceSetCmd) | ||
|
||
// Add a defined annotation in order to appear in the help menu | ||
setCmd.SetUsageTemplate(util.CmdUsageTemplate) | ||
|
||
return setCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.