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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/kubectl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var createCmd = &cobra.Command{
},
}

func newCreateCmd() *cobra.Command {
createCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
func newCreateCmd(defaultNs string) *cobra.Command {
createCmd.Flags().StringVarP(&namespace, "namespace", "n", defaultNs, "The parent namespace for the new subnamespace")
return createCmd
}
4 changes: 2 additions & 2 deletions internal/kubectl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ var deleteCmd = &cobra.Command{
},
}

func newDeleteCmd() *cobra.Command {
deleteCmd.Flags().StringP("namespace", "n", "", "The parent namespace for the new subnamespace")
func newDeleteCmd(defaultNs string) *cobra.Command {
deleteCmd.Flags().StringVarP(&namespace, "namespace", "n", defaultNs, "The parent namespace for the new subnamespace")
deleteCmd.Flags().BoolP("allowCascadingDeletion", "a", false, "Allows cascading deletion of its subnamespaces.")
return deleteCmd
}
8 changes: 6 additions & 2 deletions internal/kubectl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func init() {
client = &realClient{}

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

rootCmd = &cobra.Command{
// We should use "kubectl hns" instead of "kubectl-hns" to invoke the plugin.
Expand Down Expand Up @@ -102,8 +106,8 @@ func init() {
rootCmd.AddCommand(newSetCmd())
rootCmd.AddCommand(newDescribeCmd())
rootCmd.AddCommand(newTreeCmd())
rootCmd.AddCommand(newCreateCmd())
rootCmd.AddCommand(newDeleteCmd())
rootCmd.AddCommand(newCreateCmd(defaultNs))
rootCmd.AddCommand(newDeleteCmd(defaultNs))
rootCmd.AddCommand(newConfigCmd())
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newHrqCmd())
Expand Down