kubernetes-sigs / hierarchical-namespaces

Home of the Hierarchical Namespace Controller (HNC). Adds hierarchical policies and delegated creation to Kubernetes namespaces for improved in-cluster multitenancy.
Apache License 2.0
607 stars 105 forks source link

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

Closed yanggangtony closed 6 months ago

yanggangtony commented 9 months ago

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

Before is: image

After update :

image
superbrothers commented 9 months ago

@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 commented 9 months ago

@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 commented 9 months ago

The namespace : frontend for the current context used?

Yes.

yanggangtony commented 9 months ago

The namespace : frontend for the current context used?

Yes.

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

yanggangtony commented 9 months ago

@superbrothers

image

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

superbrothers commented 9 months ago

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 {
rjbez17 commented 7 months ago

/ok-to-test

yanggangtony commented 6 months ago

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

@rjbez17 @adrianludwin @srampal

rjbez17 commented 6 months ago

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

yanggangtony commented 6 months ago

Thanks.

rjbez17 commented 6 months ago

/lgtm /approve

k8s-ci-robot commented 6 months ago

[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: - ~~[OWNERS](https://github.com/kubernetes-sigs/hierarchical-namespaces/blob/master/OWNERS)~~ [rjbez17] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
yanggangtony commented 6 months ago

thanks.