karmadactl logs uses factory to access member cluster

Signed-off-by: carlory <baofa.fan@daocloud.io>

Co-authored-by: Hongcai Ren <renhongcai@huawei.com>
This commit is contained in:
carlory 2022-08-09 15:23:53 +08:00
parent 44e3c5e351
commit eb31ff4740
3 changed files with 21 additions and 21 deletions

View File

@ -16,12 +16,16 @@ import (
"github.com/karmada-io/karmada/pkg/karmadactl/addons" "github.com/karmada-io/karmada/pkg/karmadactl/addons"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit" "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit"
"github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/version/sharedcommand" "github.com/karmada-io/karmada/pkg/version/sharedcommand"
) )
var ( var (
rootCmdShort = "%s controls a Kubernetes Cluster Federation." rootCmdShort = "%s controls a Kubernetes Cluster Federation."
rootCmdLong = "%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. // NewKarmadaCtlCommand creates the `karmadactl` command.
@ -51,6 +55,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
_ = flag.CommandLine.Parse(nil) _ = flag.CommandLine.Parse(nil)
karmadaConfig := NewKarmadaConfig(clientcmd.NewDefaultPathOptions()) karmadaConfig := NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
f := util.NewFactory(defaultConfigFlags)
ioStreams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr} ioStreams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
groups := templates.CommandGroups{ groups := templates.CommandGroups{
{ {
@ -82,7 +87,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
{ {
Message: "Troubleshooting and Debugging Commands:", Message: "Troubleshooting and Debugging Commands:",
Commands: []*cobra.Command{ Commands: []*cobra.Command{
NewCmdLogs(karmadaConfig, parentCommand, ioStreams), NewCmdLogs(f, parentCommand, ioStreams),
NewCmdExec(karmadaConfig, parentCommand, ioStreams), NewCmdExec(karmadaConfig, parentCommand, ioStreams),
NewCmdDescribe(karmadaConfig, parentCommand, ioStreams), NewCmdDescribe(karmadaConfig, parentCommand, ioStreams),
}, },

View File

@ -14,7 +14,7 @@ import (
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
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"
) )
const ( const (
@ -50,7 +50,7 @@ var (
) )
// NewCmdLogs new logs command. // NewCmdLogs new logs command.
func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command { func NewCmdLogs(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
o := &LogsOptions{ o := &LogsOptions{
KubectlLogsOptions: kubectllogs.NewLogsOptions(streams, false), KubectlLogsOptions: kubectllogs.NewLogsOptions(streams, false),
} }
@ -61,7 +61,7 @@ func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams gener
SilenceUsage: true, SilenceUsage: true,
Example: fmt.Sprintf(logsExample, parentCommand), Example: fmt.Sprintf(logsExample, parentCommand),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(karmadaConfig, cmd, args); err != nil { if err := o.Complete(cmd, args, f); err != nil {
return err return err
} }
if err := o.Validate(); err != nil { if err := o.Validate(); err != nil {
@ -74,25 +74,25 @@ func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams gener
}, },
} }
o.GlobalCommandOptions.AddFlags(cmd.Flags()) 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")
flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
o.KubectlLogsOptions.AddFlags(cmd) o.KubectlLogsOptions.AddFlags(cmd)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
cmd.Flags().StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
return cmd return cmd
} }
// LogsOptions contains the input to the logs command. // LogsOptions contains the input to the logs command.
type LogsOptions struct { type LogsOptions struct {
// global flags
options.GlobalCommandOptions
// flags specific to logs // flags specific to logs
KubectlLogsOptions *kubectllogs.LogsOptions KubectlLogsOptions *kubectllogs.LogsOptions
Namespace string
Cluster string Cluster string
} }
// Complete ensures that options are valid and marshals them if necessary // Complete ensures that options are valid and marshals them if necessary
func (o *LogsOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, args []string) error { func (o *LogsOptions) Complete(cmd *cobra.Command, args []string, f util.Factory) error {
if o.Cluster == "" { if o.Cluster == "" {
return fmt.Errorf("must specify a cluster") return fmt.Errorf("must specify a cluster")
} }
@ -112,17 +112,11 @@ func (o *LogsOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command,
return cmdutil.UsageErrorf(cmd, "%s", logsUsageErrStr) return cmdutil.UsageErrorf(cmd, "%s", logsUsageErrStr)
} }
karmadaRestConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig) memberFactory, err := f.FactoryForMemberCluster(o.Cluster)
if err != nil {
return fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v",
o.KarmadaContext, o.KubeConfig, err)
}
clusterInfo, err := getClusterInfo(karmadaRestConfig, o.Cluster, o.KubeConfig, o.KarmadaContext)
if err != nil { if err != nil {
return err return err
} }
f := getFactory(o.Cluster, clusterInfo, o.Namespace) return o.KubectlLogsOptions.Complete(memberFactory, cmd, args)
return o.KubectlLogsOptions.Complete(f, cmd, args)
} }
// Validate checks to the LogsOptions to see if there is sufficient information run the command // Validate checks to the LogsOptions to see if there is sufficient information run the command
@ -136,6 +130,7 @@ func (o *LogsOptions) Run() error {
} }
// getClusterInfo get information of cluster // getClusterInfo get information of cluster
// TODO(@carlory): remove it when all sub command accepts factory as input parameter.
func getClusterInfo(karmadaRestConfig *rest.Config, clusterName, kubeConfig, karmadaContext string) (map[string]*ClusterInfo, error) { func getClusterInfo(karmadaRestConfig *rest.Config, clusterName, kubeConfig, karmadaContext string) (map[string]*ClusterInfo, error) {
clusterClient := karmadaclientset.NewForConfigOrDie(karmadaRestConfig).ClusterV1alpha1().Clusters() clusterClient := karmadaclientset.NewForConfigOrDie(karmadaRestConfig).ClusterV1alpha1().Clusters()

View File

@ -22,7 +22,7 @@ type Factory interface {
// KarmadaClientSet returns a karmada clientset // KarmadaClientSet returns a karmada clientset
KarmadaClientSet() (karmadaclientset.Interface, error) KarmadaClientSet() (karmadaclientset.Interface, error)
// FacotryForMemberCluster 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)
} }
@ -56,7 +56,7 @@ func (f *factoryImpl) KarmadaClientSet() (karmadaclientset.Interface, error) {
return karmadaclientset.NewForConfig(clientConfig) return karmadaclientset.NewForConfig(clientConfig)
} }
// FacotryForMemberCluster returns a cmdutil.Factory for the member cluster // FactoryForMemberCluster returns a cmdutil.Factory for the member cluster
func (f *factoryImpl) FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error) { func (f *factoryImpl) FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error) {
// Get client config of the karmada, and use it to create a cmdutil.Factory for the member cluster later. // Get client config of the karmada, and use it to create a cmdutil.Factory for the member cluster later.
clientConfig, err := f.ToRESTConfig() clientConfig, err := f.ToRESTConfig()