Skip to content

Commit

Permalink
New Factory global flags merge.
Browse files Browse the repository at this point in the history
Signed-off-by: helen <haitao.zhang@daocloud.io>
  • Loading branch information
helen committed Nov 9, 2022
1 parent abad935 commit 601ddca
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 75 deletions.
6 changes: 3 additions & 3 deletions pkg/karmadactl/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 3 additions & 6 deletions pkg/karmadactl/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -99,9 +98,7 @@ func NewCmdUncordon(f util.Factory, parentCommand string) *cobra.Command {
}

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)

return cmd
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/karmadactl/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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`)
Expand Down Expand Up @@ -83,16 +84,15 @@ 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)")
flags.BoolVarP(&o.KubectlDescribeOptions.AllNamespaces, "all-namespaces", "A", o.KubectlDescribeOptions.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
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
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/karmadactl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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`)
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/karmadactl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -120,17 +121,15 @@ 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.")
flags.BoolVar(&o.IgnoreNotFound, "ignore-not-found", o.IgnoreNotFound, "If the requested object does not exist the command will return exit code 0.")
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
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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{})
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/karmadactl/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
{
Expand Down
20 changes: 10 additions & 10 deletions pkg/karmadactl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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`)
)
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 15 additions & 1 deletion pkg/karmadactl/options/global.go
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 7 additions & 8 deletions pkg/karmadactl/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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=<CLUSTER_KUBECONFIG_PATH>
# 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_KUBECONFIG_PATH> --cluster-context=<CLUSTER_CONTEXT>`)
)
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 601ddca

Please sign in to comment.