-
Notifications
You must be signed in to change notification settings - Fork 114
Add default for the --namespace when it is not specify. #348
Add default for the --namespace when it is not specify. #348
Conversation
@yanggangtony This is not what is expected, as it uses the "default" namespace if |
you means
The |
Yes. |
@superbrothers |
690fb00
to
8b36a34
Compare
Like the test pic , is that ok for this update? |
Yes, looks good. But the namespace specified for the current context will be changed. It is not a good idea to set a value that can be changed to a default value (I do not know the reviewer's opinion). diff --git a/internal/kubectl/create.go b/internal/kubectl/create.go
index 314f2dd8..41500eba 100644
--- a/internal/kubectl/create.go
+++ b/internal/kubectl/create.go
@@ -16,9 +16,6 @@ limitations under the License.
package kubectl
import (
- "fmt"
- "os"
-
"github.com/spf13/cobra"
)
@@ -29,8 +26,7 @@ var createCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
parent, _ := cmd.Flags().GetString("namespace")
if parent == "" {
- fmt.Println("Error: parent must be set via --namespace or -n")
- os.Exit(1)
+ parent = defaultNamespace
}
// Create the anchor, the custom resource representing the subnamespace.
client.createAnchor(parent, args[0])
diff --git a/internal/kubectl/root.go b/internal/kubectl/root.go
index 8e35e1ec..4a0a451b 100644
--- a/internal/kubectl/root.go
+++ b/internal/kubectl/root.go
@@ -41,6 +41,7 @@ var k8sClient *kubernetes.Clientset
var hncClient *rest.RESTClient
var rootCmd *cobra.Command
var client Client
+var defaultNamespace string
type realClient struct{}
type anchorStatus map[string]string
@@ -77,6 +78,11 @@ func init() {
return err
}
+ // Get the namespace set to current context in kubeconfig
+ if defaultNamespace, _, err = kubecfgFlags.ToRawKubeConfigLoader().Namespace(); err != nil {
+ return err
+ }
+
// create the K8s clientset
k8sClient, err = kubernetes.NewForConfig(config)
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence with this one. It is nice to allow tools like KNS
to work in general, but part of the requirement of the flag was to prevent accidental creation.
@adrianludwin any thoughts?
/ok-to-test |
Signed-off-by: yanggang <[email protected]>
8b36a34
to
20fdfc2
Compare
Hey, friendly ping . |
Sorry I've been travling the last couple of weeks. I'll re-review this week! |
Thanks. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: rjbez17, yanggangtony The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
thanks. |
Fixes issue 347.
Add default for the --namespace when it is not specify.
Before is:

After update :
