allow to read namespace from the context field of karmada config

Signed-off-by: carlory <baofa.fan@daocloud.io>
This commit is contained in:
carlory 2022-07-07 21:03:11 +08:00
parent 5ecb4ba1bb
commit 796383dc63
1 changed files with 11 additions and 3 deletions

View File

@ -71,7 +71,7 @@ func NewCmdGet(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command
SilenceUsage: true,
Example: getExample(parentCommand),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(); err != nil {
if err := o.Complete(karmadaConfig); err != nil {
return err
}
if err := o.Validate(cmd); err != nil {
@ -87,7 +87,7 @@ func NewCmdGet(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command
o.GlobalCommandOptions.AddFlags(cmd.Flags())
o.PrintFlags.AddFlags(cmd)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", "default", "-n=namespace or -n namespace")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "-n=namespace or -n namespace")
cmd.Flags().StringVarP(&o.LabelSelector, "labels", "l", "", "-l=label or -l label")
cmd.Flags().StringSliceVarP(&o.Clusters, "clusters", "C", []string{}, "-C=member1,member2")
cmd.Flags().StringVar(&o.ClusterNamespace, "cluster-namespace", options.DefaultKarmadaClusterNamespace, "Namespace in the control plane where member cluster secrets are stored.")
@ -152,9 +152,17 @@ func NewCommandGetOptions(parent string, streams genericclioptions.IOStreams) *C
}
// Complete takes the command arguments and infers any remaining options.
func (g *CommandGetOptions) Complete() error {
func (g *CommandGetOptions) Complete(karmadaConfig KarmadaConfig) error {
newScheme := gclient.NewSchema()
if g.Namespace == "" {
namespace, _, err := karmadaConfig.GetClientConfig(g.KarmadaContext, g.KubeConfig).Namespace()
if err != nil {
return err
}
g.Namespace = namespace
}
templateArg := ""
if g.PrintFlags.TemplateFlags != nil && g.PrintFlags.TemplateFlags.TemplateArgument != nil {
templateArg = *g.PrintFlags.TemplateFlags.TemplateArgument