karmadactl describe uses factory to access member cluster
Signed-off-by: carlory <baofa.fan@daocloud.io>
This commit is contained in:
parent
9b817708c5
commit
682924be23
|
@ -11,7 +11,7 @@ import (
|
|||
"k8s.io/kubectl/pkg/describe"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
|
||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
||||
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -34,7 +34,7 @@ var (
|
|||
)
|
||||
|
||||
// NewCmdDescribe new describe command.
|
||||
func NewCmdDescribe(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
|
||||
func NewCmdDescribe(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
|
||||
o := &DescribeOptions{
|
||||
KubectlDescribeOptions: &kubectldescribe.DescribeOptions{
|
||||
FilenameOptions: &resource.FilenameOptions{},
|
||||
|
@ -53,7 +53,7 @@ func NewCmdDescribe(karmadaConfig KarmadaConfig, parentCommand string, streams g
|
|||
SilenceUsage: true,
|
||||
Example: fmt.Sprintf(describeExample, parentCommand),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := o.Complete(karmadaConfig, cmd, args); err != nil {
|
||||
if err := o.Complete(f, cmd, args); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := o.Run(); err != nil {
|
||||
|
@ -63,47 +63,40 @@ func NewCmdDescribe(karmadaConfig KarmadaConfig, parentCommand string, streams g
|
|||
},
|
||||
}
|
||||
|
||||
o.GlobalCommandOptions.AddFlags(cmd.Flags())
|
||||
flags := cmd.Flags()
|
||||
|
||||
usage := "containing the resource to describe"
|
||||
cmdutil.AddFilenameOptionFlags(cmd, o.KubectlDescribeOptions.FilenameOptions, usage)
|
||||
cmd.Flags().StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
|
||||
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
|
||||
cmd.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)")
|
||||
cmd.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.")
|
||||
cmd.Flags().BoolVar(&o.KubectlDescribeOptions.DescriberSettings.ShowEvents, "show-events", o.KubectlDescribeOptions.DescriberSettings.ShowEvents, "If true, display events related to the described object.")
|
||||
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")
|
||||
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
|
||||
}
|
||||
|
||||
// DescribeOptions contains the input to the describe command.
|
||||
type DescribeOptions struct {
|
||||
// global flags
|
||||
options.GlobalCommandOptions
|
||||
// flags specific to describe
|
||||
KubectlDescribeOptions *kubectldescribe.DescribeOptions
|
||||
Namespace string
|
||||
Cluster string
|
||||
}
|
||||
|
||||
// Complete ensures that options are valid and marshals them if necessary
|
||||
func (o *DescribeOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, args []string) error {
|
||||
func (o *DescribeOptions) Complete(f util.Factory, cmd *cobra.Command, args []string) error {
|
||||
if len(o.Cluster) == 0 {
|
||||
return fmt.Errorf("must specify a cluster")
|
||||
}
|
||||
|
||||
karmadaRestConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
|
||||
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)
|
||||
memberFactory, err := f.FactoryForMemberCluster(o.Cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f := getFactory(o.Cluster, clusterInfo, o.Namespace)
|
||||
return o.KubectlDescribeOptions.Complete(f, cmd, args)
|
||||
return o.KubectlDescribeOptions.Complete(memberFactory, cmd, args)
|
||||
}
|
||||
|
||||
// Run describe information of resources
|
||||
|
|
|
@ -89,7 +89,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
|
|||
Commands: []*cobra.Command{
|
||||
NewCmdLogs(f, parentCommand, ioStreams),
|
||||
NewCmdExec(f, parentCommand, ioStreams),
|
||||
NewCmdDescribe(karmadaConfig, parentCommand, ioStreams),
|
||||
NewCmdDescribe(f, parentCommand, ioStreams),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
package karmadactl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/rest"
|
||||
kubectllogs "k8s.io/kubectl/pkg/cmd/logs"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
|
||||
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
||||
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||
)
|
||||
|
||||
|
@ -128,32 +123,3 @@ func (o *LogsOptions) Validate() error {
|
|||
func (o *LogsOptions) Run() error {
|
||||
return o.KubectlLogsOptions.RunLogs()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
clusterClient := karmadaclientset.NewForConfigOrDie(karmadaRestConfig).ClusterV1alpha1().Clusters()
|
||||
|
||||
// check if the cluster exist in karmada control plane
|
||||
_, err := clusterClient.Get(context.TODO(), clusterName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clusterInfos := make(map[string]*ClusterInfo)
|
||||
clusterInfos[clusterName] = &ClusterInfo{}
|
||||
|
||||
clusterInfos[clusterName].APIEndpoint = karmadaRestConfig.Host + fmt.Sprintf(proxyURL, clusterName)
|
||||
clusterInfos[clusterName].KubeConfig = kubeConfig
|
||||
clusterInfos[clusterName].Context = karmadaContext
|
||||
if clusterInfos[clusterName].KubeConfig == "" {
|
||||
env := os.Getenv("KUBECONFIG")
|
||||
if env != "" {
|
||||
clusterInfos[clusterName].KubeConfig = env
|
||||
} else {
|
||||
clusterInfos[clusterName].KubeConfig = defaultKubeConfig
|
||||
}
|
||||
}
|
||||
|
||||
return clusterInfos, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue