diff --git a/pkg/karmadactl/cmdinit/karmada/deploy.go b/pkg/karmadactl/cmdinit/karmada/deploy.go index db9412a59..6c28cac85 100644 --- a/pkg/karmadactl/cmdinit/karmada/deploy.go +++ b/pkg/karmadactl/cmdinit/karmada/deploy.go @@ -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) } diff --git a/pkg/karmadactl/register.go b/pkg/karmadactl/register.go index 613a7e81c..91c2149d7 100644 --- a/pkg/karmadactl/register.go +++ b/pkg/karmadactl/register.go @@ -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, diff --git a/pkg/karmadactl/token.go b/pkg/karmadactl/token.go index d12416d86..aa0579be6 100644 --- a/pkg/karmadactl/token.go +++ b/pkg/karmadactl/token.go @@ -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) } diff --git a/pkg/karmadactl/util/bootstraptoken/bootstraptoken.go b/pkg/karmadactl/util/bootstraptoken/bootstraptoken.go index c3bed7c5a..2e25ff1b9 100644 --- a/pkg/karmadactl/util/bootstraptoken/bootstraptoken.go +++ b/pkg/karmadactl/util/bootstraptoken/bootstraptoken.go @@ -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 }