karmadactl token:print correct register command according to the karmada-context flag

Signed-off-by: 宋文杰 <wenjie.song@daocloud.io>
This commit is contained in:
宋文杰 2022-10-28 19:18:00 +08:00
parent 7c8f237d29
commit 39e4b34fe8
4 changed files with 13 additions and 10 deletions

View File

@ -132,7 +132,7 @@ func InitKarmadaBootstrapToken(dir string) (string, error) {
tokenStr := bootstrapToken.Token.ID + "." + bootstrapToken.Token.Secret
registerCommand, err := tokenutil.GenerateRegisterCommand(filepath.Join(dir, options.KarmadaKubeConfigName), "", tokenStr)
registerCommand, err := tokenutil.GenerateRegisterCommand(filepath.Join(dir, options.KarmadaKubeConfigName), "", tokenStr, "")
if err != nil {
return "", fmt.Errorf("failed to get register command, err: %w", err)
}

View File

@ -436,7 +436,7 @@ func (o *CommandRegisterOption) discoveryBootstrapConfigAndClusterInfo(bootstrap
}
klog.V(1).Info("[discovery] Using provided TLSBootstrapToken as authentication credentials for the join process")
clusterinfo := tokenutil.GetClusterFromKubeConfig(config)
clusterinfo := tokenutil.GetClusterFromKubeConfig(config, "")
tlsBootstrapCfg := CreateWithToken(
clusterinfo.Server,
DefaultClusterName,

View File

@ -196,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
// otherwise, just print the token
if o.PrintRegisterCommand {
joinCommand, err := tokenutil.GenerateRegisterCommand(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr)
joinCommand, err := tokenutil.GenerateRegisterCommand(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr, *defaultConfigFlags.Context)
if err != nil {
return fmt.Errorf("failed to get register command, err: %w", err)
}

View File

@ -84,7 +84,7 @@ type Token struct {
}
// GenerateRegisterCommand generate register command that will be printed
func GenerateRegisterCommand(kubeConfig, parentCommand, token string) (string, error) {
func GenerateRegisterCommand(kubeConfig, parentCommand, token string, karmadaContext string) (string, error) {
klog.V(1).Info("print register command")
// load the kubeconfig file to get the CA certificate and endpoint
config, err := clientcmd.LoadFromFile(kubeConfig)
@ -92,8 +92,8 @@ func GenerateRegisterCommand(kubeConfig, parentCommand, token string) (string, e
return "", fmt.Errorf("failed to load kubeconfig, err: %w", err)
}
// load the default cluster config
clusterConfig := GetClusterFromKubeConfig(config)
// load the cluster config with the given karmada-context
clusterConfig := GetClusterFromKubeConfig(config, karmadaContext)
if clusterConfig == nil {
return "", fmt.Errorf("failed to get default cluster config")
}
@ -125,14 +125,17 @@ func GenerateRegisterCommand(kubeConfig, parentCommand, token string) (string, e
token, strings.Join(publicKeyPins, ",")), nil
}
// GetClusterFromKubeConfig returns the default Cluster of the specified KubeConfig
func GetClusterFromKubeConfig(config *clientcmdapi.Config) *clientcmdapi.Cluster {
// GetClusterFromKubeConfig returns the Cluster of the specified KubeConfig, if karmada-context unset, it will use the current-context
func GetClusterFromKubeConfig(config *clientcmdapi.Config, karmadaContext string) *clientcmdapi.Cluster {
// If there is an unnamed cluster object, use it
if config.Clusters[""] != nil {
return config.Clusters[""]
}
if config.Contexts[config.CurrentContext] != nil {
return config.Clusters[config.Contexts[config.CurrentContext].Cluster]
if karmadaContext == "" {
karmadaContext = config.CurrentContext
}
if config.Contexts[karmadaContext] != nil {
return config.Clusters[config.Contexts[karmadaContext].Cluster]
}
return nil
}