New Factory global flags merge.

Signed-off-by: helen <haitao.zhang@daocloud.io>
This commit is contained in:
helen 2022-11-07 17:58:05 +08:00
parent d7bd490f94
commit 3e774496cf
13 changed files with 72 additions and 75 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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"
)
@ -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
}

View File

@ -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"
)
@ -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
}

View File

@ -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{})
}

View File

@ -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

View File

@ -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{
{

View File

@ -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"
)
@ -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)

View File

@ -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")
}

View File

@ -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"
@ -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
}

View File

@ -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

View File

@ -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)
}

View File

@ -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
}