Merge pull request #2730 from lonelyCZ/pr-fix-token
Fix karmadactl token that can't set `--kubeconfig`
This commit is contained in:
commit
7c8f237d29
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -21,7 +20,6 @@ import (
|
||||||
"k8s.io/kubectl/pkg/cmd/get"
|
"k8s.io/kubectl/pkg/cmd/get"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
|
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||||
tokenutil "github.com/karmada-io/karmada/pkg/karmadactl/util/bootstraptoken"
|
tokenutil "github.com/karmada-io/karmada/pkg/karmadactl/util/bootstraptoken"
|
||||||
)
|
)
|
||||||
|
@ -78,9 +76,6 @@ func NewCmdToken(f util.Factory, parentCommand string, streams genericclioptions
|
||||||
|
|
||||||
// CommandTokenOptions holds all command options for token
|
// CommandTokenOptions holds all command options for token
|
||||||
type CommandTokenOptions struct {
|
type CommandTokenOptions struct {
|
||||||
// global flags
|
|
||||||
options.GlobalCommandOptions
|
|
||||||
|
|
||||||
TTL *metav1.Duration
|
TTL *metav1.Duration
|
||||||
Description string
|
Description string
|
||||||
Groups []string
|
Groups []string
|
||||||
|
@ -116,17 +111,8 @@ func NewCmdTokenCreate(f util.Factory, out io.Writer, tokenOpts *CommandTokenOpt
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenOpts.GlobalCommandOptions.AddFlags(cmd.Flags())
|
cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
|
||||||
|
cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use")
|
||||||
if tokenOpts.KubeConfig == "" {
|
|
||||||
env := os.Getenv("KUBECONFIG")
|
|
||||||
if env != "" {
|
|
||||||
tokenOpts.KubeConfig = env
|
|
||||||
} else {
|
|
||||||
tokenOpts.KubeConfig = defaultKubeConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Flags().BoolVar(&tokenOpts.PrintRegisterCommand, "print-register-command", false, fmt.Sprintf("Instead of printing only the token, print the full '%s register' flag needed to register the member cluster using the token.", tokenOpts.parentCommand))
|
cmd.Flags().BoolVar(&tokenOpts.PrintRegisterCommand, "print-register-command", false, fmt.Sprintf("Instead of printing only the token, print the full '%s register' flag needed to register the member cluster using the token.", tokenOpts.parentCommand))
|
||||||
cmd.Flags().DurationVar(&tokenOpts.TTL.Duration, "ttl", tokenutil.DefaultTokenDuration, "The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire")
|
cmd.Flags().DurationVar(&tokenOpts.TTL.Duration, "ttl", tokenutil.DefaultTokenDuration, "The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire")
|
||||||
cmd.Flags().StringSliceVar(&tokenOpts.Usages, "usages", tokenutil.DefaultUsages, fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(bootstrapapi.KnownTokenUsages, ",")))
|
cmd.Flags().StringSliceVar(&tokenOpts.Usages, "usages", tokenutil.DefaultUsages, fmt.Sprintf("Describes the ways in which this token can be used. You can pass --usages multiple times or provide a comma separated list of options. Valid options: [%s]", strings.Join(bootstrapapi.KnownTokenUsages, ",")))
|
||||||
|
@ -154,16 +140,8 @@ func NewCmdTokenList(f util.Factory, out io.Writer, errW io.Writer, tokenOpts *C
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenOpts.GlobalCommandOptions.AddFlags(cmd.Flags())
|
cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
|
||||||
|
cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use")
|
||||||
if tokenOpts.KubeConfig == "" {
|
|
||||||
env := os.Getenv("KUBECONFIG")
|
|
||||||
if env != "" {
|
|
||||||
tokenOpts.KubeConfig = env
|
|
||||||
} else {
|
|
||||||
tokenOpts.KubeConfig = defaultKubeConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -195,16 +173,8 @@ func NewCmdTokenDelete(f util.Factory, out io.Writer, tokenOpts *CommandTokenOpt
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenOpts.GlobalCommandOptions.AddFlags(cmd.Flags())
|
cmd.Flags().StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
|
||||||
|
cmd.Flags().StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use")
|
||||||
if tokenOpts.KubeConfig == "" {
|
|
||||||
env := os.Getenv("KUBECONFIG")
|
|
||||||
if env != "" {
|
|
||||||
tokenOpts.KubeConfig = env
|
|
||||||
} else {
|
|
||||||
tokenOpts.KubeConfig = defaultKubeConfig
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -226,7 +196,7 @@ func (o *CommandTokenOptions) runCreateToken(out io.Writer, client kubeclient.In
|
||||||
// if --print-register-command was specified, print a machine-readable full `karmadactl register` command
|
// if --print-register-command was specified, print a machine-readable full `karmadactl register` command
|
||||||
// otherwise, just print the token
|
// otherwise, just print the token
|
||||||
if o.PrintRegisterCommand {
|
if o.PrintRegisterCommand {
|
||||||
joinCommand, err := tokenutil.GenerateRegisterCommand(o.KubeConfig, o.parentCommand, tokenStr)
|
joinCommand, err := tokenutil.GenerateRegisterCommand(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get register command, err: %w", err)
|
return fmt.Errorf("failed to get register command, err: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue