diff --git a/cmd/kubeadm/app/cmd/phases/init/kubeletfinalize.go b/cmd/kubeadm/app/cmd/phases/init/kubeletfinalize.go index db72b541e83..837c8dc70d3 100644 --- a/cmd/kubeadm/app/cmd/phases/init/kubeletfinalize.go +++ b/cmd/kubeadm/app/cmd/phases/init/kubeletfinalize.go @@ -114,7 +114,17 @@ func runKubeletFinalizeCertRotation(c workflow.RunData) error { } // Perform basic validation. The errors here can only happen if the kubelet.conf was corrupted. - userName := fmt.Sprintf("%s%s", kubeadmconstants.NodesUserPrefix, cfg.NodeRegistration.Name) + if len(kubeconfig.CurrentContext) == 0 { + return errors.Errorf("the file %q does not have current context set", kubeconfigPath) + } + currentContext, ok := kubeconfig.Contexts[kubeconfig.CurrentContext] + if !ok { + return errors.Errorf("the file %q is not a valid kubeconfig: %q set as current-context, but not found in context list", kubeconfigPath, kubeconfig.CurrentContext) + } + userName := currentContext.AuthInfo + if len(userName) == 0 { + return errors.Errorf("the file %q is not a valid kubeconfig: empty username for current context", kubeconfigPath) + } info, ok := kubeconfig.AuthInfos[userName] if !ok { return errors.Errorf("the file %q does not contain authentication for user %q", kubeconfigPath, cfg.NodeRegistration.Name)