From 3e774496cfb4bf1d0824dedb941920e81c5d8c5e Mon Sep 17 00:00:00 2001 From: helen Date: Mon, 7 Nov 2022 17:58:05 +0800 Subject: [PATCH] New Factory global flags merge. Signed-off-by: helen --- pkg/karmadactl/apply.go | 6 +++--- pkg/karmadactl/cordon.go | 9 +++------ pkg/karmadactl/describe.go | 14 +++++++------- pkg/karmadactl/exec.go | 17 ++++++++--------- pkg/karmadactl/get.go | 11 +++++------ pkg/karmadactl/join.go | 6 ++---- pkg/karmadactl/karmadactl.go | 6 +----- pkg/karmadactl/logs.go | 20 ++++++++++---------- pkg/karmadactl/options/global.go | 16 +++++++++++++++- pkg/karmadactl/promote.go | 15 +++++++-------- pkg/karmadactl/taint.go | 5 ++--- pkg/karmadactl/token.go | 12 +++++------- pkg/karmadactl/unjoin.go | 10 ++++------ 13 files changed, 72 insertions(+), 75 deletions(-) diff --git a/pkg/karmadactl/apply.go b/pkg/karmadactl/apply.go index 04efb416e..667ece79c 100644 --- a/pkg/karmadactl/apply.go +++ b/pkg/karmadactl/apply.go @@ -17,6 +17,7 @@ import ( policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" "github.com/karmada-io/karmada/pkg/util/names" ) @@ -90,11 +91,10 @@ func NewCmdApply(f util.Factory, parentCommand string, streams genericclioptions o.KubectlApplyFlags.AddFlags(cmd) flags := cmd.Flags() + options.AddKubeConfigFlags(flags) + flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") flags.BoolVarP(&o.AllClusters, "all-clusters", "", o.AllClusters, "If present, propagates a group of resources to all member clusters.") flags.StringSliceVarP(&o.Clusters, "cluster", "C", o.Clusters, "If present, propagates a group of resources to specified clusters.") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } diff --git a/pkg/karmadactl/cordon.go b/pkg/karmadactl/cordon.go index c7f2fe610..1126474cd 100644 --- a/pkg/karmadactl/cordon.go +++ b/pkg/karmadactl/cordon.go @@ -15,6 +15,7 @@ import ( clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" ) @@ -66,10 +67,8 @@ func NewCmdCordon(f util.Factory, parentCommand string) *cobra.Command { } flags := cmd.Flags() + options.AddKubeConfigFlags(flags) flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } @@ -99,10 +98,8 @@ func NewCmdUncordon(f util.Factory, parentCommand string) *cobra.Command { } flags := cmd.Flags() + options.AddKubeConfigFlags(flags) flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } diff --git a/pkg/karmadactl/describe.go b/pkg/karmadactl/describe.go index e3c997820..96db70fdc 100644 --- a/pkg/karmadactl/describe.go +++ b/pkg/karmadactl/describe.go @@ -11,6 +11,7 @@ import ( "k8s.io/kubectl/pkg/describe" "k8s.io/kubectl/pkg/util/templates" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" ) @@ -31,16 +32,16 @@ var ( describeExample = templates.Examples(` # Describe a pod in cluster(member1) %[1]s describe pods/nginx -C=member1 - + # Describe all pods in cluster(member1) %[1]s describe pods -C=member1 - + # Describe a pod identified by type and name in "pod.json" in cluster(member1) %[1]s describe -f pod.json -C=member1 - + # Describe pods by label name=myLabel in cluster(member1) %[1]s describe po -l name=myLabel -C=member1 - + # Describe all pods managed by the 'frontend' replication controller in cluster(member1) # (rc-created pods get the name of the rc as a prefix in the pod name) %[1]s describe pods frontend -C=member1`) @@ -83,6 +84,8 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericcliopti flags := cmd.Flags() + options.AddKubeConfigFlags(flags) + flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") usage := "containing the resource to describe" cmdutil.AddFilenameOptionFlags(cmd, o.KubectlDescribeOptions.FilenameOptions, usage) flags.StringVarP(&o.KubectlDescribeOptions.Selector, "selector", "l", o.KubectlDescribeOptions.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") @@ -90,9 +93,6 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericcliopti flags.BoolVar(&o.KubectlDescribeOptions.DescriberSettings.ShowEvents, "show-events", o.KubectlDescribeOptions.DescriberSettings.ShowEvents, "If true, display events related to the described object.") cmdutil.AddChunkSizeFlag(cmd, &o.KubectlDescribeOptions.DescriberSettings.ChunkSize) flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } diff --git a/pkg/karmadactl/exec.go b/pkg/karmadactl/exec.go index b57550c5c..ddb91adb2 100644 --- a/pkg/karmadactl/exec.go +++ b/pkg/karmadactl/exec.go @@ -10,6 +10,7 @@ import ( cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" ) @@ -21,20 +22,20 @@ var ( execExample = templates.Examples(` # Get output from running the 'date' command from pod mypod, using the first container by default in cluster(member1) %[1]s exec mypod -C=member1 -- date - + # Get output from running the 'date' command in ruby-container from pod mypod in cluster(member1) %[1]s exec mypod -c ruby-container -C=member1 -- date - + # Get output from running the 'date' command in ruby-container from pod mypod in cluster(member1) %[1]sexec mypod -c ruby-container -C=member1 -- date - + # Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod in cluster(member1) # and sends stdout/stderr from 'bash' back to the client %[1]s exec mypod -c ruby-container -C=member1 -i -t -- bash -il - + # Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default in cluster(member1) %[1]s exec deploy/mydeployment -C=member1 -- date - + # Get output from running 'date' command from the first pod of the service myservice, using the first container by default in cluster(member1) %[1]s exec svc/myservice -C=member1 -- date`) ) @@ -76,7 +77,8 @@ func NewCmdExec(f util.Factory, parentCommand string, streams genericclioptions. } flags := cmd.Flags() - + options.AddKubeConfigFlags(flags) + flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodExecTimeout) cmdutil.AddJsonFilenameFlag(flags, &o.KubectlExecOptions.FilenameOptions.Filenames, "to use to exec into the resource") cmdutil.AddContainerVarFlags(cmd, &o.KubectlExecOptions.ContainerName, o.KubectlExecOptions.ContainerName) @@ -85,9 +87,6 @@ func NewCmdExec(f util.Factory, parentCommand string, streams genericclioptions. flags.BoolVarP(&o.KubectlExecOptions.TTY, "tty", "t", o.KubectlExecOptions.TTY, "Stdin is a TTY") flags.BoolVarP(&o.KubectlExecOptions.Quiet, "quiet", "q", o.KubectlExecOptions.Quiet, "Only print output from the remote session") flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } diff --git a/pkg/karmadactl/get.go b/pkg/karmadactl/get.go index 7ad39c111..acccde364 100644 --- a/pkg/karmadactl/get.go +++ b/pkg/karmadactl/get.go @@ -34,6 +34,7 @@ import ( workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" "github.com/karmada-io/karmada/pkg/util/gclient" "github.com/karmada-io/karmada/pkg/util/helper" @@ -120,7 +121,8 @@ func NewCmdGet(f util.Factory, parentCommand string, streams genericclioptions.I o.PrintFlags.AddFlags(cmd) flags := cmd.Flags() - + options.AddKubeConfigFlags(flags) + flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") flags.StringVarP(&o.LabelSelector, "labels", "l", "", "-l=label or -l label") flags.StringSliceVarP(&o.Clusters, "clusters", "C", []string{}, "-C=member1,member2") flags.BoolVarP(&o.AllNamespaces, "all-namespaces", "A", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.") @@ -128,9 +130,6 @@ func NewCmdGet(f util.Factory, parentCommand string, streams genericclioptions.I flags.BoolVarP(&o.Watch, "watch", "w", o.Watch, "After listing/getting the requested object, watch for changes. Uninitialized objects are excluded if no object name is provided.") flags.BoolVar(&o.WatchOnly, "watch-only", o.WatchOnly, "Watch for changes to the requested object(s), without listing/getting first.") flags.BoolVar(&o.OutputWatchEvents, "output-watch-events", o.OutputWatchEvents, "Output watch event objects when --watch or --watch-only is used. Existing objects are output as initial ADDED events.") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") return cmd } @@ -422,7 +421,7 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin func (g *CommandGetOptions) printIfNotFindResource(written int, allErrs *[]error, allResourcesNamespaced bool) { if written == 0 && !g.IgnoreNotFound && len(*allErrs) == 0 { if allResourcesNamespaced { - fmt.Fprintf(g.ErrOut, "No resources found in %s namespace.\n", *defaultConfigFlags.Namespace) + fmt.Fprintf(g.ErrOut, "No resources found in %s namespace.\n", *options.DefaultConfigFlags.Namespace) } else { fmt.Fprintln(g.ErrOut, "No resources found") } @@ -893,7 +892,7 @@ func (g *CommandGetOptions) getRBInKarmada(gclient karmadaclientset.Interface) e var err error if !g.AllNamespaces { - rbList, err = gclient.WorkV1alpha2().ResourceBindings(*defaultConfigFlags.Namespace).List(context.TODO(), metav1.ListOptions{}) + rbList, err = gclient.WorkV1alpha2().ResourceBindings(*options.DefaultConfigFlags.Namespace).List(context.TODO(), metav1.ListOptions{}) } else { rbList, err = gclient.WorkV1alpha2().ResourceBindings("").List(context.TODO(), metav1.ListOptions{}) } diff --git a/pkg/karmadactl/join.go b/pkg/karmadactl/join.go index 3d2328948..1b5b6c354 100644 --- a/pkg/karmadactl/join.go +++ b/pkg/karmadactl/join.go @@ -59,9 +59,7 @@ func NewCmdJoin(f cmdutil.Factory, parentCommand string) *cobra.Command { flags := cmd.Flags() opts.AddFlags(flags) - - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(flags) return cmd } @@ -149,7 +147,7 @@ func RunJoin(f cmdutil.Factory, opts CommandJoinOption) error { controlPlaneRestConfig, err := f.ToRawKubeConfigLoader().ClientConfig() if err != nil { return fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v", - *defaultConfigFlags.Context, *defaultConfigFlags.KubeConfig, err) + *options.DefaultConfigFlags.Context, *options.DefaultConfigFlags.KubeConfig, err) } // Get cluster config diff --git a/pkg/karmadactl/karmadactl.go b/pkg/karmadactl/karmadactl.go index 0ec945537..24418785b 100644 --- a/pkg/karmadactl/karmadactl.go +++ b/pkg/karmadactl/karmadactl.go @@ -22,9 +22,6 @@ import ( var ( rootCmdShort = "%s controls a Kubernetes Cluster Federation." rootCmdLong = "%s controls a Kubernetes Cluster Federation." - - // It composes the set of values necessary for obtaining a REST client config with default values set. - defaultConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0) ) // NewKarmadaCtlCommand creates the `karmadactl` command. @@ -52,8 +49,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command { // Prevent klog errors about logging before parsing. _ = flag.CommandLine.Parse(nil) - - f := util.NewFactory(defaultConfigFlags) + f := util.NewFactory(options.DefaultConfigFlags) ioStreams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr} groups := templates.CommandGroups{ { diff --git a/pkg/karmadactl/logs.go b/pkg/karmadactl/logs.go index d75dfbfd4..32fb1383d 100644 --- a/pkg/karmadactl/logs.go +++ b/pkg/karmadactl/logs.go @@ -9,6 +9,7 @@ import ( cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" ) @@ -26,25 +27,25 @@ var ( logsExample = templates.Examples(` # Return snapshot logs from pod nginx with only one container in cluster(member1) %[1]s logs nginx -C=member1 - + # Return snapshot logs from pod nginx with multi containers in cluster(member1) %[1]s logs nginx --all-containers=true -C=member1 - + # Return snapshot logs from all containers in pods defined by label app=nginx in cluster(member1) %[1]s logs -l app=nginx --all-containers=true -C=member1 - + # Return snapshot of previous terminated ruby container logs from pod web-1 in cluster(member1) %[1]s logs -p -c ruby web-1 -C=member1 - + # Begin streaming the logs of the ruby container in pod web-1 in cluster(member1) %[1]s logs -f -c ruby web-1 -C=member1 - + # Begin streaming the logs from all containers in pods defined by label app=nginx in cluster(member1) %[1]s logs -f -l app=nginx --all-containers=true -C=member1 - + # Display only the most recent 20 lines of output in pod nginx in cluster(member1) %[1]s logs --tail=20 nginx -C=member1 - + # Show all logs from pod nginx written in the last hour in cluster(member1) %[1]s logs --since=1h nginx -C=member1`) ) @@ -80,9 +81,8 @@ func NewCmdLogs(f util.Factory, parentCommand string, streams genericclioptions. } flags := cmd.Flags() - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") + options.AddKubeConfigFlags(flags) + flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster") o.KubectlLogsOptions.AddFlags(cmd) diff --git a/pkg/karmadactl/options/global.go b/pkg/karmadactl/options/global.go index e9a66f7a5..af21624eb 100644 --- a/pkg/karmadactl/options/global.go +++ b/pkg/karmadactl/options/global.go @@ -1,9 +1,23 @@ package options -import "time" +import ( + "time" + + "github.com/spf13/pflag" + "k8s.io/cli-runtime/pkg/genericclioptions" +) // DefaultKarmadaClusterNamespace defines the default namespace where the member cluster secrets are stored. const DefaultKarmadaClusterNamespace = "karmada-cluster" // DefaultKarmadactlCommandDuration defines the default timeout for karmadactl execute const DefaultKarmadactlCommandDuration = 60 * time.Second + +// DefaultConfigFlags It composes the set of values necessary for obtaining a REST client config with default values set. +var DefaultConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0) + +// AddKubeConfigFlags adds flags to the specified FlagSet. +func AddKubeConfigFlags(flags *pflag.FlagSet) { + flags.StringVar(DefaultConfigFlags.KubeConfig, "kubeconfig", *DefaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") + flags.StringVar(DefaultConfigFlags.Context, "karmada-context", *DefaultConfigFlags.Context, "The name of the kubeconfig context to use") +} diff --git a/pkg/karmadactl/promote.go b/pkg/karmadactl/promote.go index 841722188..da01ffc2a 100644 --- a/pkg/karmadactl/promote.go +++ b/pkg/karmadactl/promote.go @@ -23,6 +23,7 @@ import ( policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" "github.com/karmada-io/karmada/pkg/resourceinterpreter/defaultinterpreter/prune" "github.com/karmada-io/karmada/pkg/util/gclient" @@ -36,19 +37,19 @@ var ( promoteExample = templates.Examples(` # Promote deployment(default/nginx) from cluster1 to Karmada %[1]s promote deployment nginx -n default -C cluster1 - + # Promote deployment(default/nginx) with gvk from cluster1 to Karmada %[1]s promote deployment.v1.apps nginx -n default -C cluster1 - + # Dumps the artifacts but does not deploy them to Karmada, same as 'dry run' %[1]s promote deployment nginx -n default -C cluster1 -o yaml|json - + # Promote secret(default/default-token) from cluster1 to Karmada %[1]s promote secret default-token -n default -C cluster1 - + # Support to use '--cluster-kubeconfig' to specify the configuration of member cluster %[1]s promote deployment nginx -n default -C cluster1 --cluster-kubeconfig= - + # Support to use '--cluster-kubeconfig' and '--cluster-context' to specify the configuration of member cluster %[1]s promote deployment nginx -n default -C cluster1 --cluster-kubeconfig= --cluster-context=`) ) @@ -84,9 +85,7 @@ func NewCmdPromote(f util.Factory, parentCommand string) *cobra.Command { flag := cmd.Flags() opts.AddFlags(flag) - - flag.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flag.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(flag) return cmd } diff --git a/pkg/karmadactl/taint.go b/pkg/karmadactl/taint.go index e76d9b943..e986dd822 100644 --- a/pkg/karmadactl/taint.go +++ b/pkg/karmadactl/taint.go @@ -20,6 +20,7 @@ import ( clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/scheme" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" "github.com/karmada-io/karmada/pkg/util/lifted" ) @@ -156,11 +157,9 @@ func (o *CommandTaintOption) Validate() error { // AddFlags adds flags to the specified FlagSet. func (o *CommandTaintOption) AddFlags(flags *pflag.FlagSet) { + options.AddKubeConfigFlags(flags) flags.BoolVar(&o.overwrite, "overwrite", o.overwrite, "If true, allow taints to be overwritten, otherwise reject taint updates that overwrite existing taints.") flags.BoolVar(&o.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") - flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") } // RunTaint set taints for the clusters diff --git a/pkg/karmadactl/token.go b/pkg/karmadactl/token.go index aa0579be6..66953e825 100644 --- a/pkg/karmadactl/token.go +++ b/pkg/karmadactl/token.go @@ -20,6 +20,7 @@ import ( "k8s.io/kubectl/pkg/cmd/get" "k8s.io/kubectl/pkg/util/templates" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/util" tokenutil "github.com/karmada-io/karmada/pkg/karmadactl/util/bootstraptoken" ) @@ -111,8 +112,7 @@ func NewCmdTokenCreate(f util.Factory, out io.Writer, tokenOpts *CommandTokenOpt Args: cobra.NoArgs, } - cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(cmd.Flags()) cmd.Flags().BoolVar(&tokenOpts.PrintRegisterCommand, "print-register-command", false, fmt.Sprintf("Instead of printing only the token, print the full '%s register' flag needed to register the member cluster using the token.", tokenOpts.parentCommand)) cmd.Flags().DurationVar(&tokenOpts.TTL.Duration, "ttl", tokenutil.DefaultTokenDuration, "The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire") cmd.Flags().StringSliceVar(&tokenOpts.Usages, "usages", tokenutil.DefaultUsages, fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(bootstrapapi.KnownTokenUsages, ","))) @@ -140,8 +140,7 @@ func NewCmdTokenList(f util.Factory, out io.Writer, errW io.Writer, tokenOpts *C Args: cobra.NoArgs, } - cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(cmd.Flags()) return cmd } @@ -173,8 +172,7 @@ func NewCmdTokenDelete(f util.Factory, out io.Writer, tokenOpts *CommandTokenOpt }, } - cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(cmd.Flags()) return cmd } @@ -196,7 +194,7 @@ func (o *CommandTokenOptions) runCreateToken(out io.Writer, client kubeclient.In // if --print-register-command was specified, print a machine-readable full `karmadactl register` command // otherwise, just print the token if o.PrintRegisterCommand { - joinCommand, err := tokenutil.GenerateRegisterCommand(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr, *defaultConfigFlags.Context) + joinCommand, err := tokenutil.GenerateRegisterCommand(*options.DefaultConfigFlags.KubeConfig, o.parentCommand, tokenStr, *options.DefaultConfigFlags.Context) if err != nil { return fmt.Errorf("failed to get register command, err: %w", err) } diff --git a/pkg/karmadactl/unjoin.go b/pkg/karmadactl/unjoin.go index 01b7bec70..956e919b1 100644 --- a/pkg/karmadactl/unjoin.go +++ b/pkg/karmadactl/unjoin.go @@ -29,10 +29,10 @@ var ( unjoinExample = templates.Examples(` # Unjoin cluster from karmada control plane, but not to remove resources created by karmada in the unjoining cluster %[1]s unjoin CLUSTER_NAME - + # Unjoin cluster from karmada control plane and attempt to remove resources created by karmada in the unjoining cluster %[1]s unjoin CLUSTER_NAME --cluster-kubeconfig= - + # Unjoin cluster from karmada control plane with timeout %[1]s unjoin CLUSTER_NAME --cluster-kubeconfig= --wait 2m`) ) @@ -67,9 +67,7 @@ func NewCmdUnjoin(f cmdutil.Factory, parentCommand string) *cobra.Command { flags := cmd.Flags() opts.AddFlags(flags) - - flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") - flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use") + options.AddKubeConfigFlags(flags) return cmd } @@ -148,7 +146,7 @@ func RunUnjoin(f cmdutil.Factory, opts CommandUnjoinOption) error { controlPlaneRestConfig, err := f.ToRawKubeConfigLoader().ClientConfig() if err != nil { klog.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v", - defaultConfigFlags.Context, defaultConfigFlags.KubeConfig, err) + options.DefaultConfigFlags.Context, options.DefaultConfigFlags.KubeConfig, err) return err }