karmadactl apply uses factory to access member cluster
Signed-off-by: helen <helenfrank@protonmail.com>
This commit is contained in:
parent
2a0610eb00
commit
0f5754ad14
|
@ -12,14 +12,12 @@ import (
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
restclient "k8s.io/client-go/rest"
|
|
||||||
kubectlapply "k8s.io/kubectl/pkg/cmd/apply"
|
kubectlapply "k8s.io/kubectl/pkg/cmd/apply"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
|
|
||||||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
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"
|
"github.com/karmada-io/karmada/pkg/util/names"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,11 +25,8 @@ var metadataAccessor = meta.NewAccessor()
|
||||||
|
|
||||||
// CommandApplyOptions contains the input to the apply command.
|
// CommandApplyOptions contains the input to the apply command.
|
||||||
type CommandApplyOptions struct {
|
type CommandApplyOptions struct {
|
||||||
// global flags
|
|
||||||
options.GlobalCommandOptions
|
|
||||||
// apply flags
|
// apply flags
|
||||||
KubectlApplyFlags *kubectlapply.ApplyFlags
|
KubectlApplyFlags *kubectlapply.ApplyFlags
|
||||||
Namespace string
|
|
||||||
AllClusters bool
|
AllClusters bool
|
||||||
Clusters []string
|
Clusters []string
|
||||||
|
|
||||||
|
@ -68,7 +63,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewCmdApply creates the `apply` command
|
// 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{
|
o := &CommandApplyOptions{
|
||||||
KubectlApplyFlags: kubectlapply.NewApplyFlags(nil, streams),
|
KubectlApplyFlags: kubectlapply.NewApplyFlags(nil, streams),
|
||||||
}
|
}
|
||||||
|
@ -80,7 +75,7 @@ func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string, streams gene
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
Example: fmt.Sprintf(applyExample, parentCommand),
|
Example: fmt.Sprintf(applyExample, parentCommand),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
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
|
return err
|
||||||
}
|
}
|
||||||
if err := o.Validate(cmd, args); err != nil {
|
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)
|
o.KubectlApplyFlags.AddFlags(cmd)
|
||||||
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
|
flags := cmd.Flags()
|
||||||
cmd.Flags().BoolVarP(&o.AllClusters, "all-clusters", "", o.AllClusters, "If present, propagates a group of resources to all member clusters.")
|
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.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
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete completes all the required options
|
// Complete completes all the required options
|
||||||
func (o *CommandApplyOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, parentCommand string, args []string) error {
|
func (o *CommandApplyOptions) Complete(f util.Factory, cmd *cobra.Command, parentCommand string, args []string) error {
|
||||||
restConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
|
karmadaClient, err := f.KarmadaClientSet()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
karmadaClient, err := karmadaclientset.NewForConfig(restConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
o.karmadaClient = karmadaClient
|
o.karmadaClient = karmadaClient
|
||||||
|
o.KubectlApplyFlags.Factory = f
|
||||||
kubeConfigFlags := NewConfigFlags(true).WithDeprecatedPasswordFlag()
|
|
||||||
kubeConfigFlags.Namespace = &o.Namespace
|
|
||||||
kubeConfigFlags.WrapConfigFn = func(config *restclient.Config) *restclient.Config { return restConfig }
|
|
||||||
o.KubectlApplyFlags.Factory = cmdutil.NewFactory(kubeConfigFlags)
|
|
||||||
kubectlApplyOptions, err := o.KubectlApplyFlags.ToOptions(cmd, parentCommand, args)
|
kubectlApplyOptions, err := o.KubectlApplyFlags.ToOptions(cmd, parentCommand, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -179,7 +168,7 @@ func (o *CommandApplyOptions) generateAndInjectPolices() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to recognize resource: %v", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
|
return fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
|
||||||
{
|
{
|
||||||
Message: "Advanced Commands:",
|
Message: "Advanced Commands:",
|
||||||
Commands: []*cobra.Command{
|
Commands: []*cobra.Command{
|
||||||
NewCmdApply(karmadaConfig, parentCommand, ioStreams),
|
NewCmdApply(f, parentCommand, ioStreams),
|
||||||
NewCmdPromote(karmadaConfig, parentCommand),
|
NewCmdPromote(karmadaConfig, parentCommand),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -21,7 +21,6 @@ type Factory interface {
|
||||||
|
|
||||||
// KarmadaClientSet returns a karmada clientset
|
// KarmadaClientSet returns a karmada clientset
|
||||||
KarmadaClientSet() (karmadaclientset.Interface, error)
|
KarmadaClientSet() (karmadaclientset.Interface, error)
|
||||||
|
|
||||||
// FactoryForMemberCluster returns a cmdutil.Factory for the member cluster
|
// FactoryForMemberCluster returns a cmdutil.Factory for the member cluster
|
||||||
FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error)
|
FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue