karmadactl token uses factory to access cluster

Signed-off-by: lonelyCZ <531187475@qq.com>
This commit is contained in:
lonelyCZ 2022-10-21 22:20:03 +08:00
parent dbb24d3b62
commit b2d3bbdf39
2 changed files with 11 additions and 22 deletions

View File

@ -72,7 +72,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
addons.NewCommandAddons(parentCommand),
NewCmdJoin(karmadaConfig, parentCommand),
NewCmdUnjoin(karmadaConfig, parentCommand),
NewCmdToken(karmadaConfig, parentCommand, ioStreams),
NewCmdToken(f, parentCommand, ioStreams),
NewCmdRegister(parentCommand),
},
},

View File

@ -51,7 +51,7 @@ var (
)
// NewCmdToken returns cobra.Command for token management
func NewCmdToken(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
func NewCmdToken(f util.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
opts := &CommandTokenOptions{
parentCommand: parentCommand,
TTL: &metav1.Duration{
@ -69,9 +69,9 @@ func NewCmdToken(karmadaConfig KarmadaConfig, parentCommand string, streams gene
},
}
cmd.AddCommand(NewCmdTokenCreate(karmadaConfig, streams.Out, opts))
cmd.AddCommand(NewCmdTokenList(karmadaConfig, streams.Out, streams.ErrOut, opts))
cmd.AddCommand(NewCmdTokenDelete(karmadaConfig, streams.Out, opts))
cmd.AddCommand(NewCmdTokenCreate(f, streams.Out, opts))
cmd.AddCommand(NewCmdTokenList(f, streams.Out, streams.ErrOut, opts))
cmd.AddCommand(NewCmdTokenDelete(f, streams.Out, opts))
return cmd
}
@ -92,7 +92,7 @@ type CommandTokenOptions struct {
}
// NewCmdTokenCreate returns cobra.Command to create token
func NewCmdTokenCreate(karmadaConfig KarmadaConfig, out io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
func NewCmdTokenCreate(f util.Factory, out io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create bootstrap tokens on the server",
@ -106,7 +106,7 @@ func NewCmdTokenCreate(karmadaConfig KarmadaConfig, out io.Writer, tokenOpts *Co
DisableFlagsInUseLine: true,
RunE: func(Cmd *cobra.Command, args []string) error {
// Get control plane kube-apiserver client
client, err := tokenOpts.getClientSet(karmadaConfig)
client, err := f.KubernetesClientSet()
if err != nil {
return err
}
@ -137,14 +137,14 @@ func NewCmdTokenCreate(karmadaConfig KarmadaConfig, out io.Writer, tokenOpts *Co
}
// NewCmdTokenList returns cobra.Command to list tokens
func NewCmdTokenList(karmadaConfig KarmadaConfig, out io.Writer, errW io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
func NewCmdTokenList(f util.Factory, out io.Writer, errW io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List bootstrap tokens on the server",
Long: "This command will list all bootstrap tokens for you.",
RunE: func(tokenCmd *cobra.Command, args []string) error {
// Get control plane kube-apiserver client
client, err := tokenOpts.getClientSet(karmadaConfig)
client, err := f.KubernetesClientSet()
if err != nil {
return err
}
@ -169,7 +169,7 @@ func NewCmdTokenList(karmadaConfig KarmadaConfig, out io.Writer, errW io.Writer,
}
// NewCmdTokenDelete returns cobra.Command to delete tokens
func NewCmdTokenDelete(karmadaConfig KarmadaConfig, out io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
func NewCmdTokenDelete(f util.Factory, out io.Writer, tokenOpts *CommandTokenOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [token-value] ...",
DisableFlagsInUseLine: true,
@ -186,7 +186,7 @@ func NewCmdTokenDelete(karmadaConfig KarmadaConfig, out io.Writer, tokenOpts *Co
}
// Get control plane kube-apiserver client
client, err := tokenOpts.getClientSet(karmadaConfig)
client, err := f.KubernetesClientSet()
if err != nil {
return err
}
@ -325,17 +325,6 @@ func (o *CommandTokenOptions) runDeleteTokens(out io.Writer, client kubeclient.I
return nil
}
// getClientSet get clientset of karmada control plane
func (o *CommandTokenOptions) getClientSet(karmadaConfig KarmadaConfig) (kubeclient.Interface, error) {
// Get control plane karmada-apiserver client
controlPlaneRestConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v",
o.KarmadaContext, o.KubeConfig, err)
}
return kubeclient.NewForConfigOrDie(controlPlaneRestConfig), nil
}
// constructTokenTableRow construct token table row
func constructTokenTableRow(token tokenutil.BootstrapToken) metav1.TableRow {
var row metav1.TableRow