Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Add default for the --namespace when it is not specify. #348

Merged

Conversation

yanggangtony
Copy link

@yanggangtony yanggangtony commented Nov 15, 2023

Fixes issue 347.
Add default for the --namespace when it is not specify.

Before is:
image

After update :
image

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 15, 2023
@superbrothers
Copy link
Contributor

@yanggangtony This is not what is expected, as it uses the "default" namespace if --namespace is not specified. #347 says it wants to use the namespace specified in kubeconfig. This change also differs from the behavior of kubectl.

@yanggangtony
Copy link
Author

yanggangtony commented Nov 15, 2023

@yanggangtony This is not what is expected, as it uses the "default" namespace if --namespace is not specified. #347 says it wants to use the namespace specified in kubeconfig. This change also differs from the behavior of kubectl.

@superbrothers

you means

contexts:
- context:
    cluster: development
    namespace: frontend
    user: developer
  name: dev-frontend

The namespace : frontend for the current context used?

@superbrothers
Copy link
Contributor

The namespace : frontend for the current context used?

Yes.

@yanggangtony
Copy link
Author

The namespace : frontend for the current context used?

Yes.

@superbrothers
Thanks for quick review.
I will update later.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 15, 2023
@yanggangtony
Copy link
Author

yanggangtony commented Nov 15, 2023

@superbrothers
image

Like the test pic , is that ok for this update?

@superbrothers
Copy link
Contributor

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 {

Copy link
Contributor

@rjbez17 rjbez17 left a 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?

@rjbez17
Copy link
Contributor

rjbez17 commented Jan 29, 2024

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Jan 29, 2024
@yanggangtony
Copy link
Author

Hey, friendly ping .
This pr is for a long time .
Would you put it for progress?

@rjbez17 @adrianludwin @srampal

@rjbez17
Copy link
Contributor

rjbez17 commented Feb 26, 2024

Sorry I've been travling the last couple of weeks. I'll re-review this week!

@yanggangtony
Copy link
Author

Thanks.

@rjbez17
Copy link
Contributor

rjbez17 commented Mar 8, 2024

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 8, 2024
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 8, 2024
@k8s-ci-robot k8s-ci-robot merged commit c3087b5 into kubernetes-retired:master Mar 8, 2024
@yanggangtony
Copy link
Author

thanks.

@yanggangtony yanggangtony deleted the add-default-ns-parms branch March 9, 2024 00:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use the default namespace in the kubeconfig
4 participants