cleanup karmada ctl code

Signed-off-by: guoyao <1015105054@qq.com>
This commit is contained in:
guoyao 2021-11-11 09:35:06 +08:00
parent ec60c2d103
commit 3672d6d0a3
2 changed files with 11 additions and 43 deletions

View File

@ -8,7 +8,6 @@ import (
"io" "io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
@ -16,6 +15,7 @@ import (
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/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"
) )
var ( var (
@ -60,7 +60,7 @@ func NewCmdCordon(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string)
} }
flags := cmd.Flags() flags := cmd.Flags()
opts.AddFlags(flags) opts.GlobalCommandOptions.AddFlags(flags)
return cmd return cmd
} }
@ -93,15 +93,8 @@ func NewCmdUncordon(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string
// CommandCordonOption holds all command options for cordon and uncordon // CommandCordonOption holds all command options for cordon and uncordon
type CommandCordonOption struct { type CommandCordonOption struct {
// KubeConfig holds the control plane KUBECONFIG file path. // global flags
KubeConfig string options.GlobalCommandOptions
// ClusterContext is the name of the cluster context in control plane KUBECONFIG file.
// Default value is the current-context.
KarmadaContext string
// DryRun tells if run the command in dry-run mode, without making any server requests.
DryRun bool
// ClusterName is the cluster's name that we are going to join with. // ClusterName is the cluster's name that we are going to join with.
ClusterName string ClusterName string
@ -117,13 +110,6 @@ func (o *CommandCordonOption) Complete(args []string) error {
return nil return nil
} }
// AddFlags adds flags to the specified FlagSet.
func (o *CommandCordonOption) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.KubeConfig, "kubeconfig", "", "Path to the control plane kubeconfig file.")
flags.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in control plane kubeconfig file.")
flags.BoolVar(&o.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.")
}
// CordonHelper wraps functionality to cordon/uncordon cluster // CordonHelper wraps functionality to cordon/uncordon cluster
type CordonHelper struct { type CordonHelper struct {
cluster *clusterv1alpha1.Cluster cluster *clusterv1alpha1.Cluster

View File

@ -92,10 +92,8 @@ type CommandGetOptions struct {
Clusters []string Clusters []string
PrintFlags *get.PrintFlags PrintFlags *get.PrintFlags
ToPrinter func(*meta.RESTMapping, *bool, bool, bool) (printers.ResourcePrinterFunc, error) ToPrinter func(*meta.RESTMapping, *bool, bool, bool) (printers.ResourcePrinterFunc, error)
IsHumanReadablePrinter bool
PrintWithOpenAPICols bool
CmdParent string CmdParent string
@ -112,8 +110,6 @@ type CommandGetOptions struct {
Namespace string Namespace string
ExplicitNamespace bool ExplicitNamespace bool
ServerPrint bool
NoHeaders bool NoHeaders bool
Sort bool Sort bool
IgnoreNotFound bool IgnoreNotFound bool
@ -126,20 +122,15 @@ type CommandGetOptions struct {
func NewCommandGetOptions(parent string, streams genericclioptions.IOStreams) *CommandGetOptions { func NewCommandGetOptions(parent string, streams genericclioptions.IOStreams) *CommandGetOptions {
return &CommandGetOptions{ return &CommandGetOptions{
PrintFlags: get.NewGetPrintFlags(), PrintFlags: get.NewGetPrintFlags(),
CmdParent: parent,
CmdParent: parent, IOStreams: streams,
ChunkSize: 500,
IOStreams: streams,
ChunkSize: 500,
ServerPrint: true,
} }
} }
// Complete takes the command arguments and infers any remaining options. // Complete takes the command arguments and infers any remaining options.
func (g *CommandGetOptions) Complete(cmd *cobra.Command, args []string) error { func (g *CommandGetOptions) Complete(cmd *cobra.Command, args []string) error {
newScheme := gclient.NewSchema() newScheme := gclient.NewSchema()
// human readable printers have special conversion rules, so we determine if we're using one.
g.IsHumanReadablePrinter = true
// check karmada config path // check karmada config path
env := os.Getenv("KUBECONFIG") env := os.Getenv("KUBECONFIG")
@ -174,9 +165,8 @@ func (g *CommandGetOptions) Complete(cmd *cobra.Command, args []string) error {
return nil, err return nil, err
} }
if g.ServerPrint { printer = &get.TablePrinter{Delegate: printer}
printer = &get.TablePrinter{Delegate: printer}
}
return printer.PrintObj, nil return printer.PrintObj, nil
} }
return nil return nil
@ -437,14 +427,6 @@ func getFactory(clusterName string, clusterInfos map[string]*ClusterInfo) cmduti
} }
func (g *CommandGetOptions) transformRequests(req *rest.Request) { func (g *CommandGetOptions) transformRequests(req *rest.Request) {
// We need full objects if printing with openapi columns
if g.PrintWithOpenAPICols {
return
}
if !g.ServerPrint || !g.IsHumanReadablePrinter {
return
}
req.SetHeader("Accept", strings.Join([]string{ req.SetHeader("Accept", strings.Join([]string{
fmt.Sprintf("application/json;as=Table;v=%s;g=%s", metav1.SchemeGroupVersion.Version, metav1.GroupName), fmt.Sprintf("application/json;as=Table;v=%s;g=%s", metav1.SchemeGroupVersion.Version, metav1.GroupName),
fmt.Sprintf("application/json;as=Table;v=%s;g=%s", metav1beta1.SchemeGroupVersion.Version, metav1beta1.GroupName), fmt.Sprintf("application/json;as=Table;v=%s;g=%s", metav1beta1.SchemeGroupVersion.Version, metav1beta1.GroupName),