Merge pull request #11584 from justinsb/fix_11537_kubecfg

Only update kubeconfig user when we have user info
This commit is contained in:
Kubernetes Prow Robot 2021-05-23 15:59:38 -07:00 committed by GitHub
commit e730a0d149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -116,6 +116,10 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
config.Clusters[b.Context] = cluster config.Clusters[b.Context] = cluster
} }
// We avoid changing the user unless we're actually writing something
// Issue #11537
haveUserInfo := false
// If the user has the same name as the context, it is the admin user // If the user has the same name as the context, it is the admin user
if b.User == b.Context { if b.User == b.Context {
authInfo := config.AuthInfos[b.Context] authInfo := config.AuthInfos[b.Context]
@ -126,6 +130,8 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
if b.KubeUser != "" && b.KubePassword != "" { if b.KubeUser != "" && b.KubePassword != "" {
authInfo.Username = b.KubeUser authInfo.Username = b.KubeUser
authInfo.Password = b.KubePassword authInfo.Password = b.KubePassword
haveUserInfo = true
} }
if b.ClientCert != nil && b.ClientKey != nil { if b.ClientCert != nil && b.ClientKey != nil {
@ -133,6 +139,8 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
authInfo.ClientCertificateData = b.ClientCert authInfo.ClientCertificateData = b.ClientCert
authInfo.ClientKey = "" authInfo.ClientKey = ""
authInfo.ClientKeyData = b.ClientKey authInfo.ClientKeyData = b.ClientKey
haveUserInfo = true
} }
if len(b.AuthenticationExec) != 0 { if len(b.AuthenticationExec) != 0 {
@ -141,12 +149,16 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
Command: b.AuthenticationExec[0], Command: b.AuthenticationExec[0],
Args: b.AuthenticationExec[1:], Args: b.AuthenticationExec[1:],
} }
haveUserInfo = true
} }
if haveUserInfo {
if config.AuthInfos == nil { if config.AuthInfos == nil {
config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo) config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo)
} }
config.AuthInfos[b.Context] = authInfo config.AuthInfos[b.Context] = authInfo
}
} else if b.User != "" { } else if b.User != "" {
if config.AuthInfos[b.User] == nil { if config.AuthInfos[b.User] == nil {
return fmt.Errorf("could not find user %q", b.User) return fmt.Errorf("could not find user %q", b.User)
@ -179,9 +191,11 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
} }
context.Cluster = b.Context context.Cluster = b.Context
if haveUserInfo {
if b.User != "" { if b.User != "" {
context.AuthInfo = b.User context.AuthInfo = b.User
} }
}
if b.Namespace != "" { if b.Namespace != "" {
context.Namespace = b.Namespace context.Namespace = b.Namespace
@ -199,6 +213,6 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
return err return err
} }
fmt.Printf("kops has set your kubectl context to %s\n", b.Context) fmt.Printf("kOps has set your kubectl context to %s\n", b.Context)
return nil return nil
} }