Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

karmadactl token uses factory to access cluster #2668

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
Expand Down
31 changes: 10 additions & 21 deletions pkg/karmadactl/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
Expand All @@ -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",
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down