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

Commit 20fdfc2

Browse files
author
yanggang
committed
Add default for the --namespace when it is not specify.
Signed-off-by: yanggang <[email protected]>
1 parent d367fc0 commit 20fdfc2

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

internal/kubectl/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var createCmd = &cobra.Command{
3737
},
3838
}
3939

40-
func newCreateCmd() *cobra.Command {
41-
createCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
40+
func newCreateCmd(defaultNs string) *cobra.Command {
41+
createCmd.Flags().StringVarP(&namespace, "namespace", "n", defaultNs, "The parent namespace for the new subnamespace")
4242
return createCmd
4343
}

internal/kubectl/delete.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ var deleteCmd = &cobra.Command{
5656
},
5757
}
5858

59-
func newDeleteCmd() *cobra.Command {
60-
deleteCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
59+
func newDeleteCmd(defaultNs string) *cobra.Command {
60+
deleteCmd.Flags().StringVarP(&namespace, "namespace", "n", defaultNs, "The parent namespace for the new subnamespace")
6161
deleteCmd.Flags().BoolP("allowCascadingDeletion", "a", false, "Allows cascading deletion of its subnamespaces.")
6262
return deleteCmd
6363
}

internal/kubectl/root.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func init() {
6262
client = &realClient{}
6363

6464
kubecfgFlags := genericclioptions.NewConfigFlags(false)
65+
defaultNs, _, err := kubecfgFlags.ToRawKubeConfigLoader().Namespace()
66+
if err != nil {
67+
fmt.Printf("Error reading namespace in default kubeconfig :%s\n", err.Error())
68+
}
6569

6670
rootCmd = &cobra.Command{
6771
// We should use "kubectl hns" instead of "kubectl-hns" to invoke the plugin.
@@ -102,8 +106,8 @@ func init() {
102106
rootCmd.AddCommand(newSetCmd())
103107
rootCmd.AddCommand(newDescribeCmd())
104108
rootCmd.AddCommand(newTreeCmd())
105-
rootCmd.AddCommand(newCreateCmd())
106-
rootCmd.AddCommand(newDeleteCmd())
109+
rootCmd.AddCommand(newCreateCmd(defaultNs))
110+
rootCmd.AddCommand(newDeleteCmd(defaultNs))
107111
rootCmd.AddCommand(newConfigCmd())
108112
rootCmd.AddCommand(newVersionCmd())
109113
rootCmd.AddCommand(newHrqCmd())

0 commit comments

Comments
 (0)