karmadactl token:print correct register command according to the karmada-context flag
Signed-off-by: 宋文杰 <wenjie.song@daocloud.io>
This commit is contained in:
parent
7c8f237d29
commit
39e4b34fe8
|
@ -132,7 +132,7 @@ func InitKarmadaBootstrapToken(dir string) (string, error) {
|
||||||
|
|
||||||
tokenStr := bootstrapToken.Token.ID + "." + bootstrapToken.Token.Secret
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -436,7 +436,7 @@ func (o *CommandRegisterOption) discoveryBootstrapConfigAndClusterInfo(bootstrap
|
||||||
}
|
}
|
||||||
|
|
||||||
klog.V(1).Info("[discovery] Using provided TLSBootstrapToken as authentication credentials for the join process")
|
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(
|
tlsBootstrapCfg := CreateWithToken(
|
||||||
clusterinfo.Server,
|
clusterinfo.Server,
|
||||||
DefaultClusterName,
|
DefaultClusterName,
|
||||||
|
|
|
@ -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
|
// 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(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr)
|
joinCommand, err := tokenutil.GenerateRegisterCommand(*defaultConfigFlags.KubeConfig, o.parentCommand, tokenStr, *defaultConfigFlags.Context)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ type Token struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateRegisterCommand generate register command that will be printed
|
// 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")
|
klog.V(1).Info("print register command")
|
||||||
// load the kubeconfig file to get the CA certificate and endpoint
|
// load the kubeconfig file to get the CA certificate and endpoint
|
||||||
config, err := clientcmd.LoadFromFile(kubeConfig)
|
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)
|
return "", fmt.Errorf("failed to load kubeconfig, err: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the default cluster config
|
// load the cluster config with the given karmada-context
|
||||||
clusterConfig := GetClusterFromKubeConfig(config)
|
clusterConfig := GetClusterFromKubeConfig(config, karmadaContext)
|
||||||
if clusterConfig == nil {
|
if clusterConfig == nil {
|
||||||
return "", fmt.Errorf("failed to get default cluster config")
|
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
|
token, strings.Join(publicKeyPins, ",")), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClusterFromKubeConfig returns the default Cluster of the specified KubeConfig
|
// GetClusterFromKubeConfig returns the Cluster of the specified KubeConfig, if karmada-context unset, it will use the current-context
|
||||||
func GetClusterFromKubeConfig(config *clientcmdapi.Config) *clientcmdapi.Cluster {
|
func GetClusterFromKubeConfig(config *clientcmdapi.Config, karmadaContext string) *clientcmdapi.Cluster {
|
||||||
// If there is an unnamed cluster object, use it
|
// If there is an unnamed cluster object, use it
|
||||||
if config.Clusters[""] != nil {
|
if config.Clusters[""] != nil {
|
||||||
return config.Clusters[""]
|
return config.Clusters[""]
|
||||||
}
|
}
|
||||||
if config.Contexts[config.CurrentContext] != nil {
|
if karmadaContext == "" {
|
||||||
return config.Clusters[config.Contexts[config.CurrentContext].Cluster]
|
karmadaContext = config.CurrentContext
|
||||||
|
}
|
||||||
|
if config.Contexts[karmadaContext] != nil {
|
||||||
|
return config.Clusters[config.Contexts[karmadaContext].Cluster]
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue