Skip to content

Commit 945991b

Browse files
committed
clientcmd: fix NPE in NewNonInteractiveDeferredLoadingClientConfig with nil overrides
1 parent a730ad5 commit 945991b

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

staging/src/k8s.io/client-go/tools/clientcmd/client_config.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
159159
clientConfig.Proxy = http.ProxyURL(u)
160160
}
161161

162-
if len(config.overrides.Timeout) > 0 {
162+
if config.overrides != nil && len(config.overrides.Timeout) > 0 {
163163
timeout, err := ParseTimeout(config.overrides.Timeout)
164164
if err != nil {
165165
return nil, err
@@ -381,7 +381,7 @@ func (config *DirectClientConfig) ConfirmUsable() error {
381381
// getContextName returns the default, or user-set context name, and a boolean that indicates
382382
// whether the default context name has been overwritten by a user-set flag, or left as its default value
383383
func (config *DirectClientConfig) getContextName() (string, bool) {
384-
if len(config.overrides.CurrentContext) != 0 {
384+
if config.overrides != nil && len(config.overrides.CurrentContext) != 0 {
385385
return config.overrides.CurrentContext, true
386386
}
387387
if len(config.contextName) != 0 {
@@ -395,7 +395,7 @@ func (config *DirectClientConfig) getContextName() (string, bool) {
395395
// and a boolean indicating whether the default authInfo name is overwritten by a user-set flag, or
396396
// left as its default value
397397
func (config *DirectClientConfig) getAuthInfoName() (string, bool) {
398-
if len(config.overrides.Context.AuthInfo) != 0 {
398+
if config.overrides != nil && len(config.overrides.Context.AuthInfo) != 0 {
399399
return config.overrides.Context.AuthInfo, true
400400
}
401401
context, _ := config.getContext()
@@ -406,7 +406,7 @@ func (config *DirectClientConfig) getAuthInfoName() (string, bool) {
406406
// indicating whether the default clusterName has been overwritten by a user-set flag, or left as
407407
// its default value
408408
func (config *DirectClientConfig) getClusterName() (string, bool) {
409-
if len(config.overrides.Context.Cluster) != 0 {
409+
if config.overrides != nil && len(config.overrides.Context.Cluster) != 0 {
410410
return config.overrides.Context.Cluster, true
411411
}
412412
context, _ := config.getContext()
@@ -424,7 +424,9 @@ func (config *DirectClientConfig) getContext() (clientcmdapi.Context, error) {
424424
} else if required {
425425
return clientcmdapi.Context{}, fmt.Errorf("context %q does not exist", contextName)
426426
}
427-
mergo.MergeWithOverwrite(mergedContext, config.overrides.Context)
427+
if config.overrides != nil {
428+
mergo.MergeWithOverwrite(mergedContext, config.overrides.Context)
429+
}
428430

429431
return *mergedContext, nil
430432
}
@@ -440,7 +442,9 @@ func (config *DirectClientConfig) getAuthInfo() (clientcmdapi.AuthInfo, error) {
440442
} else if required {
441443
return clientcmdapi.AuthInfo{}, fmt.Errorf("auth info %q does not exist", authInfoName)
442444
}
443-
mergo.MergeWithOverwrite(mergedAuthInfo, config.overrides.AuthInfo)
445+
if config.overrides != nil {
446+
mergo.MergeWithOverwrite(mergedAuthInfo, config.overrides.AuthInfo)
447+
}
444448

445449
return *mergedAuthInfo, nil
446450
}
@@ -451,30 +455,37 @@ func (config *DirectClientConfig) getCluster() (clientcmdapi.Cluster, error) {
451455
clusterInfoName, required := config.getClusterName()
452456

453457
mergedClusterInfo := clientcmdapi.NewCluster()
454-
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterDefaults)
458+
if config.overrides != nil {
459+
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterDefaults)
460+
}
455461
if configClusterInfo, exists := clusterInfos[clusterInfoName]; exists {
456462
mergo.MergeWithOverwrite(mergedClusterInfo, configClusterInfo)
457463
} else if required {
458464
return clientcmdapi.Cluster{}, fmt.Errorf("cluster %q does not exist", clusterInfoName)
459465
}
460-
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterInfo)
466+
if config.overrides != nil {
467+
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterInfo)
468+
}
469+
461470
// * An override of --insecure-skip-tls-verify=true and no accompanying CA/CA data should clear already-set CA/CA data
462471
// otherwise, a kubeconfig containing a CA reference would return an error that "CA and insecure-skip-tls-verify couldn't both be set".
463472
// * An override of --certificate-authority should also override TLS skip settings and CA data, otherwise existing CA data will take precedence.
464-
caLen := len(config.overrides.ClusterInfo.CertificateAuthority)
465-
caDataLen := len(config.overrides.ClusterInfo.CertificateAuthorityData)
466-
if config.overrides.ClusterInfo.InsecureSkipTLSVerify || caLen > 0 || caDataLen > 0 {
467-
mergedClusterInfo.InsecureSkipTLSVerify = config.overrides.ClusterInfo.InsecureSkipTLSVerify
468-
mergedClusterInfo.CertificateAuthority = config.overrides.ClusterInfo.CertificateAuthority
469-
mergedClusterInfo.CertificateAuthorityData = config.overrides.ClusterInfo.CertificateAuthorityData
470-
}
471-
472-
// if the --tls-server-name has been set in overrides, use that value.
473-
// if the --server has been set in overrides, then use the value of --tls-server-name specified on the CLI too. This gives the property
474-
// that setting a --server will effectively clear the KUBECONFIG value of tls-server-name if it is specified on the command line which is
475-
// usually correct.
476-
if config.overrides.ClusterInfo.TLSServerName != "" || config.overrides.ClusterInfo.Server != "" {
477-
mergedClusterInfo.TLSServerName = config.overrides.ClusterInfo.TLSServerName
473+
if config.overrides != nil {
474+
caLen := len(config.overrides.ClusterInfo.CertificateAuthority)
475+
caDataLen := len(config.overrides.ClusterInfo.CertificateAuthorityData)
476+
if config.overrides.ClusterInfo.InsecureSkipTLSVerify || caLen > 0 || caDataLen > 0 {
477+
mergedClusterInfo.InsecureSkipTLSVerify = config.overrides.ClusterInfo.InsecureSkipTLSVerify
478+
mergedClusterInfo.CertificateAuthority = config.overrides.ClusterInfo.CertificateAuthority
479+
mergedClusterInfo.CertificateAuthorityData = config.overrides.ClusterInfo.CertificateAuthorityData
480+
}
481+
482+
// if the --tls-server-name has been set in overrides, use that value.
483+
// if the --server has been set in overrides, then use the value of --tls-server-name specified on the CLI too. This gives the property
484+
// that setting a --server will effectively clear the KUBECONFIG value of tls-server-name if it is specified on the command line which is
485+
// usually correct.
486+
if config.overrides.ClusterInfo.TLSServerName != "" || config.overrides.ClusterInfo.Server != "" {
487+
mergedClusterInfo.TLSServerName = config.overrides.ClusterInfo.TLSServerName
488+
}
478489
}
479490

480491
return *mergedClusterInfo, nil

staging/src/k8s.io/client-go/tools/clientcmd/merged_client_builder.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ func (config *DeferredLoadingClientConfig) createClientConfig() (ClientConfig, e
7171
return nil, err
7272
}
7373

74+
var currentContext string
75+
if config.overrides != nil {
76+
currentContext = config.overrides.CurrentContext
77+
}
7478
if config.fallbackReader != nil {
75-
config.clientConfig = NewInteractiveClientConfig(*mergedConfig, config.overrides.CurrentContext, config.overrides, config.fallbackReader, config.loader)
79+
config.clientConfig = NewInteractiveClientConfig(*mergedConfig, currentContext, config.overrides, config.fallbackReader, config.loader)
7680
} else {
77-
config.clientConfig = NewNonInteractiveClientConfig(*mergedConfig, config.overrides.CurrentContext, config.overrides, config.loader)
81+
config.clientConfig = NewNonInteractiveClientConfig(*mergedConfig, currentContext, config.overrides, config.loader)
7882
}
7983
return config.clientConfig, nil
8084
}

0 commit comments

Comments
 (0)