Adopt kubectl describe command refactor

Signed-off-by: RainbowMango <qdurenhongcai@gmail.com>
This commit is contained in:
RainbowMango 2023-01-29 14:41:13 +08:00
parent c0515acf81
commit 68828d0a48
1 changed files with 11 additions and 23 deletions

View File

@ -5,10 +5,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
kubectldescribe "k8s.io/kubectl/pkg/cmd/describe"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/describe"
"k8s.io/kubectl/pkg/util/templates"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
@ -49,17 +46,8 @@ var (
// NewCmdDescribe new describe command.
func NewCmdDescribe(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
o := &CommandDescribeOptions{
KubectlDescribeOptions: &kubectldescribe.DescribeOptions{
FilenameOptions: &resource.FilenameOptions{},
DescriberSettings: &describe.DescriberSettings{
ShowEvents: true,
ChunkSize: cmdutil.DefaultChunkSize,
},
CmdParent: parentCommand,
IOStreams: streams,
},
}
o := &CommandDescribeOptions{}
kubedescribeFlags := kubectldescribe.NewDescribeFlags(f, streams)
cmd := &cobra.Command{
Use: "describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) (-C CLUSTER)",
@ -69,7 +57,7 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericcliopti
DisableFlagsInUseLine: true,
Example: fmt.Sprintf(describeExample, parentCommand),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, cmd, args); err != nil {
if err := o.Complete(f, args, kubedescribeFlags, parentCommand); err != nil {
return err
}
if err := o.Run(); err != nil {
@ -83,15 +71,10 @@ func NewCmdDescribe(f util.Factory, parentCommand string, streams genericcliopti
}
flags := cmd.Flags()
kubedescribeFlags.AddFlags(cmd)
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")
return cmd
@ -105,7 +88,7 @@ type CommandDescribeOptions struct {
}
// Complete ensures that options are valid and marshals them if necessary
func (o *CommandDescribeOptions) Complete(f util.Factory, cmd *cobra.Command, args []string) error {
func (o *CommandDescribeOptions) Complete(f util.Factory, args []string, describeFlag *kubectldescribe.DescribeFlags, parentCommand string) error {
if len(o.Cluster) == 0 {
return fmt.Errorf("must specify a cluster")
}
@ -114,7 +97,12 @@ func (o *CommandDescribeOptions) Complete(f util.Factory, cmd *cobra.Command, ar
if err != nil {
return err
}
return o.KubectlDescribeOptions.Complete(memberFactory, cmd, args)
describeFlag.Factory = memberFactory
o.KubectlDescribeOptions, err = describeFlag.ToOptions(parentCommand, args)
if err != nil {
return err
}
return nil
}
// Run describe information of resources