From 0f5754ad144381b7ac40aca51f652d0398c36c6a Mon Sep 17 00:00:00 2001 From: helen Date: Fri, 23 Sep 2022 00:40:02 +0800 Subject: [PATCH] karmadactl apply uses factory to access member cluster Signed-off-by: helen --- pkg/karmadactl/apply.go | 41 +++++++++++++--------------------- pkg/karmadactl/karmadactl.go | 2 +- pkg/karmadactl/util/factory.go | 1 - 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/pkg/karmadactl/apply.go b/pkg/karmadactl/apply.go index 5608e7e12..9f68db4df 100644 --- a/pkg/karmadactl/apply.go +++ b/pkg/karmadactl/apply.go @@ -12,14 +12,12 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" - restclient "k8s.io/client-go/rest" kubectlapply "k8s.io/kubectl/pkg/cmd/apply" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/templates" 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" ) @@ -27,11 +25,8 @@ var metadataAccessor = meta.NewAccessor() // CommandApplyOptions contains the input to the apply command. type CommandApplyOptions struct { - // global flags - options.GlobalCommandOptions // apply flags KubectlApplyFlags *kubectlapply.ApplyFlags - Namespace string AllClusters bool Clusters []string @@ -48,8 +43,8 @@ var ( JSON and YAML formats are accepted. Alpha Disclaimer: the --prune functionality is not yet complete. Do not use unless you are aware of what the current state is. See https://issues.k8s.io/34274. - - Note: It implements the function of 'kubectl apply' by default. + + Note: It implements the function of 'kubectl apply' by default. If you want to propagate them into member clusters, please use %[1]s apply --all-clusters'.`) applyExample = templates.Examples(` @@ -68,7 +63,7 @@ var ( ) // NewCmdApply creates the `apply` command -func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command { +func NewCmdApply(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command { o := &CommandApplyOptions{ KubectlApplyFlags: kubectlapply.NewApplyFlags(nil, streams), } @@ -80,7 +75,7 @@ func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string, streams gene DisableFlagsInUseLine: true, Example: fmt.Sprintf(applyExample, parentCommand), RunE: func(cmd *cobra.Command, args []string) error { - if err := o.Complete(karmadaConfig, cmd, parentCommand, args); err != nil { + if err := o.Complete(f, cmd, parentCommand, args); err != nil { return err } if err := o.Validate(cmd, args); err != nil { @@ -90,30 +85,24 @@ func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string, streams gene }, } - o.GlobalCommandOptions.AddFlags(cmd.Flags()) o.KubectlApplyFlags.AddFlags(cmd) - cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request") - cmd.Flags().BoolVarP(&o.AllClusters, "all-clusters", "", o.AllClusters, "If present, propagates a group of resources to all member clusters.") - cmd.Flags().StringSliceVarP(&o.Clusters, "cluster", "C", o.Clusters, "If present, propagates a group of resources to specified clusters.") + flags := cmd.Flags() + 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 } // Complete completes all the required options -func (o *CommandApplyOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, parentCommand string, args []string) error { - restConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig) - if err != nil { - return err - } - karmadaClient, err := karmadaclientset.NewForConfig(restConfig) +func (o *CommandApplyOptions) Complete(f util.Factory, cmd *cobra.Command, parentCommand string, args []string) error { + karmadaClient, err := f.KarmadaClientSet() if err != nil { return err } o.karmadaClient = karmadaClient - - kubeConfigFlags := NewConfigFlags(true).WithDeprecatedPasswordFlag() - kubeConfigFlags.Namespace = &o.Namespace - kubeConfigFlags.WrapConfigFn = func(config *restclient.Config) *restclient.Config { return restConfig } - o.KubectlApplyFlags.Factory = cmdutil.NewFactory(kubeConfigFlags) + o.KubectlApplyFlags.Factory = f kubectlApplyOptions, err := o.KubectlApplyFlags.ToOptions(cmd, parentCommand, args) if err != nil { return err @@ -179,7 +168,7 @@ func (o *CommandApplyOptions) generateAndInjectPolices() error { if err != nil { return fmt.Errorf("unable to recognize resource: %v", err) } - client, err := o.KubectlApplyFlags.Factory.ClientForMapping(mapping) + client, err := o.KubectlApplyFlags.Factory.UnstructuredClientForMapping(mapping) if err != nil { return fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err) } diff --git a/pkg/karmadactl/karmadactl.go b/pkg/karmadactl/karmadactl.go index c79e2bb6e..ff72336d9 100644 --- a/pkg/karmadactl/karmadactl.go +++ b/pkg/karmadactl/karmadactl.go @@ -95,7 +95,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command { { Message: "Advanced Commands:", Commands: []*cobra.Command{ - NewCmdApply(karmadaConfig, parentCommand, ioStreams), + NewCmdApply(f, parentCommand, ioStreams), NewCmdPromote(karmadaConfig, parentCommand), }, }, diff --git a/pkg/karmadactl/util/factory.go b/pkg/karmadactl/util/factory.go index 9c7453424..8e4884b3e 100644 --- a/pkg/karmadactl/util/factory.go +++ b/pkg/karmadactl/util/factory.go @@ -21,7 +21,6 @@ type Factory interface { // KarmadaClientSet returns a karmada clientset KarmadaClientSet() (karmadaclientset.Interface, error) - // FactoryForMemberCluster returns a cmdutil.Factory for the member cluster FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error) }