Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update kubeconfig user when we have user info #11584

Merged
merged 1 commit into from
May 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/kubeconfig/kubecfg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
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 b.User == b.Context {
authInfo := config.AuthInfos[b.Context]
Expand All @@ -126,13 +130,17 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
if b.KubeUser != "" && b.KubePassword != "" {
authInfo.Username = b.KubeUser
authInfo.Password = b.KubePassword

haveUserInfo = true
}

if b.ClientCert != nil && b.ClientKey != nil {
authInfo.ClientCertificate = ""
authInfo.ClientCertificateData = b.ClientCert
authInfo.ClientKey = ""
authInfo.ClientKeyData = b.ClientKey

haveUserInfo = true
}

if len(b.AuthenticationExec) != 0 {
Expand All @@ -141,12 +149,16 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
Command: b.AuthenticationExec[0],
Args: b.AuthenticationExec[1:],
}

haveUserInfo = true
}

if config.AuthInfos == nil {
config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo)
if haveUserInfo {
if config.AuthInfos == nil {
config.AuthInfos = make(map[string]*clientcmdapi.AuthInfo)
}
config.AuthInfos[b.Context] = authInfo
}
config.AuthInfos[b.Context] = authInfo
} else if b.User != "" {
if config.AuthInfos[b.User] == nil {
return fmt.Errorf("could not find user %q", b.User)
Expand Down Expand Up @@ -179,8 +191,10 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
}

context.Cluster = b.Context
if b.User != "" {
context.AuthInfo = b.User
if haveUserInfo {
if b.User != "" {
context.AuthInfo = b.User
}
}

if b.Namespace != "" {
Expand All @@ -199,6 +213,6 @@ func (b *KubeconfigBuilder) WriteKubecfg(configAccess clientcmd.ConfigAccess) er
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
}