Closed yanggangtony closed 6 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 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?
The
namespace : frontend
for the current context used?
Yes.
The
namespace : frontend
for the current context used?Yes.
@superbrothers Thanks for quick review. I will update later.
@superbrothers
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 {
/ok-to-test
Hey, friendly ping . This pr is for a long time . Would you put it for progress?
@rjbez17 @adrianludwin @srampal
Sorry I've been travling the last couple of weeks. I'll re-review this week!
Thanks.
/lgtm /approve
[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
thanks.
Fixes issue 347. Add default for the --namespace when it is not specify.
Before is:![image](https://github.com/kubernetes-sigs/hierarchical-namespaces/assets/12080746/979eaa0e-f5f8-474a-a1eb-3ffc95caa4c9)
After update :