mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 07:28:14 +00:00
Merge pull request #92138 from ash2k/ash2k/client-cleanups
Client library cleanups Kubernetes-commit: 667deec758fbbd9576df1c5f081a4e239996b074
This commit is contained in:
commit
77dfe4dff7
@ -198,13 +198,13 @@ func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mergo.MergeWithOverwrite(clientConfig, userAuthPartialConfig)
|
mergo.Merge(clientConfig, userAuthPartialConfig, mergo.WithOverride)
|
||||||
|
|
||||||
serverAuthPartialConfig, err := getServerIdentificationPartialConfig(configAuthInfo, configClusterInfo)
|
serverAuthPartialConfig, err := getServerIdentificationPartialConfig(configAuthInfo, configClusterInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mergo.MergeWithOverwrite(clientConfig, serverAuthPartialConfig)
|
mergo.Merge(clientConfig, serverAuthPartialConfig, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientConfig, nil
|
return clientConfig, nil
|
||||||
@ -225,7 +225,7 @@ func getServerIdentificationPartialConfig(configAuthInfo clientcmdapi.AuthInfo,
|
|||||||
configClientConfig.CAData = configClusterInfo.CertificateAuthorityData
|
configClientConfig.CAData = configClusterInfo.CertificateAuthorityData
|
||||||
configClientConfig.Insecure = configClusterInfo.InsecureSkipTLSVerify
|
configClientConfig.Insecure = configClusterInfo.InsecureSkipTLSVerify
|
||||||
configClientConfig.ServerName = configClusterInfo.TLSServerName
|
configClientConfig.ServerName = configClusterInfo.TLSServerName
|
||||||
mergo.MergeWithOverwrite(mergedConfig, configClientConfig)
|
mergo.Merge(mergedConfig, configClientConfig, mergo.WithOverride)
|
||||||
|
|
||||||
return mergedConfig, nil
|
return mergedConfig, nil
|
||||||
}
|
}
|
||||||
@ -294,8 +294,8 @@ func (config *DirectClientConfig) getUserIdentificationPartialConfig(configAuthI
|
|||||||
promptedConfig := makeUserIdentificationConfig(*promptedAuthInfo)
|
promptedConfig := makeUserIdentificationConfig(*promptedAuthInfo)
|
||||||
previouslyMergedConfig := mergedConfig
|
previouslyMergedConfig := mergedConfig
|
||||||
mergedConfig = &restclient.Config{}
|
mergedConfig = &restclient.Config{}
|
||||||
mergo.MergeWithOverwrite(mergedConfig, promptedConfig)
|
mergo.Merge(mergedConfig, promptedConfig, mergo.WithOverride)
|
||||||
mergo.MergeWithOverwrite(mergedConfig, previouslyMergedConfig)
|
mergo.Merge(mergedConfig, previouslyMergedConfig, mergo.WithOverride)
|
||||||
config.promptedCredentials.username = mergedConfig.Username
|
config.promptedCredentials.username = mergedConfig.Username
|
||||||
config.promptedCredentials.password = mergedConfig.Password
|
config.promptedCredentials.password = mergedConfig.Password
|
||||||
}
|
}
|
||||||
@ -463,12 +463,12 @@ func (config *DirectClientConfig) getContext() (clientcmdapi.Context, error) {
|
|||||||
|
|
||||||
mergedContext := clientcmdapi.NewContext()
|
mergedContext := clientcmdapi.NewContext()
|
||||||
if configContext, exists := contexts[contextName]; exists {
|
if configContext, exists := contexts[contextName]; exists {
|
||||||
mergo.MergeWithOverwrite(mergedContext, configContext)
|
mergo.Merge(mergedContext, configContext, mergo.WithOverride)
|
||||||
} else if required {
|
} else if required {
|
||||||
return clientcmdapi.Context{}, fmt.Errorf("context %q does not exist", contextName)
|
return clientcmdapi.Context{}, fmt.Errorf("context %q does not exist", contextName)
|
||||||
}
|
}
|
||||||
if config.overrides != nil {
|
if config.overrides != nil {
|
||||||
mergo.MergeWithOverwrite(mergedContext, config.overrides.Context)
|
mergo.Merge(mergedContext, config.overrides.Context, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
return *mergedContext, nil
|
return *mergedContext, nil
|
||||||
@ -481,12 +481,12 @@ func (config *DirectClientConfig) getAuthInfo() (clientcmdapi.AuthInfo, error) {
|
|||||||
|
|
||||||
mergedAuthInfo := clientcmdapi.NewAuthInfo()
|
mergedAuthInfo := clientcmdapi.NewAuthInfo()
|
||||||
if configAuthInfo, exists := authInfos[authInfoName]; exists {
|
if configAuthInfo, exists := authInfos[authInfoName]; exists {
|
||||||
mergo.MergeWithOverwrite(mergedAuthInfo, configAuthInfo)
|
mergo.Merge(mergedAuthInfo, configAuthInfo, mergo.WithOverride)
|
||||||
} else if required {
|
} else if required {
|
||||||
return clientcmdapi.AuthInfo{}, fmt.Errorf("auth info %q does not exist", authInfoName)
|
return clientcmdapi.AuthInfo{}, fmt.Errorf("auth info %q does not exist", authInfoName)
|
||||||
}
|
}
|
||||||
if config.overrides != nil {
|
if config.overrides != nil {
|
||||||
mergo.MergeWithOverwrite(mergedAuthInfo, config.overrides.AuthInfo)
|
mergo.Merge(mergedAuthInfo, config.overrides.AuthInfo, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
return *mergedAuthInfo, nil
|
return *mergedAuthInfo, nil
|
||||||
@ -499,15 +499,15 @@ func (config *DirectClientConfig) getCluster() (clientcmdapi.Cluster, error) {
|
|||||||
|
|
||||||
mergedClusterInfo := clientcmdapi.NewCluster()
|
mergedClusterInfo := clientcmdapi.NewCluster()
|
||||||
if config.overrides != nil {
|
if config.overrides != nil {
|
||||||
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterDefaults)
|
mergo.Merge(mergedClusterInfo, config.overrides.ClusterDefaults, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
if configClusterInfo, exists := clusterInfos[clusterInfoName]; exists {
|
if configClusterInfo, exists := clusterInfos[clusterInfoName]; exists {
|
||||||
mergo.MergeWithOverwrite(mergedClusterInfo, configClusterInfo)
|
mergo.Merge(mergedClusterInfo, configClusterInfo, mergo.WithOverride)
|
||||||
} else if required {
|
} else if required {
|
||||||
return clientcmdapi.Cluster{}, fmt.Errorf("cluster %q does not exist", clusterInfoName)
|
return clientcmdapi.Cluster{}, fmt.Errorf("cluster %q does not exist", clusterInfoName)
|
||||||
}
|
}
|
||||||
if config.overrides != nil {
|
if config.overrides != nil {
|
||||||
mergo.MergeWithOverwrite(mergedClusterInfo, config.overrides.ClusterInfo)
|
mergo.Merge(mergedClusterInfo, config.overrides.ClusterInfo, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
// * An override of --insecure-skip-tls-verify=true and no accompanying CA/CA data should clear already-set CA/CA data
|
// * An override of --insecure-skip-tls-verify=true and no accompanying CA/CA data should clear already-set CA/CA data
|
||||||
@ -548,11 +548,12 @@ func (config *inClusterClientConfig) RawConfig() (clientcmdapi.Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config *inClusterClientConfig) ClientConfig() (*restclient.Config, error) {
|
func (config *inClusterClientConfig) ClientConfig() (*restclient.Config, error) {
|
||||||
if config.inClusterConfigProvider == nil {
|
inClusterConfigProvider := config.inClusterConfigProvider
|
||||||
config.inClusterConfigProvider = restclient.InClusterConfig
|
if inClusterConfigProvider == nil {
|
||||||
|
inClusterConfigProvider = restclient.InClusterConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
icc, err := config.inClusterConfigProvider()
|
icc, err := inClusterConfigProvider()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -572,7 +573,7 @@ func (config *inClusterClientConfig) ClientConfig() (*restclient.Config, error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return icc, err
|
return icc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *inClusterClientConfig) Namespace() (string, bool, error) {
|
func (config *inClusterClientConfig) Namespace() (string, bool, error) {
|
||||||
|
@ -63,7 +63,7 @@ func TestMergoSemantics(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, data := range testDataStruct {
|
for _, data := range testDataStruct {
|
||||||
err := mergo.MergeWithOverwrite(&data.dst, &data.src)
|
err := mergo.Merge(&data.dst, &data.src, mergo.WithOverride)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error while merging: %s", err)
|
t.Errorf("error while merging: %s", err)
|
||||||
}
|
}
|
||||||
@ -92,7 +92,7 @@ func TestMergoSemantics(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, data := range testDataMap {
|
for _, data := range testDataMap {
|
||||||
err := mergo.MergeWithOverwrite(&data.dst, &data.src)
|
err := mergo.Merge(&data.dst, &data.src, mergo.WithOverride)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error while merging: %s", err)
|
t.Errorf("error while merging: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,8 @@ package clientcmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
@ -48,24 +46,24 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
RecommendedConfigDir = path.Join(homedir.HomeDir(), RecommendedHomeDir)
|
RecommendedConfigDir = filepath.Join(homedir.HomeDir(), RecommendedHomeDir)
|
||||||
RecommendedHomeFile = path.Join(RecommendedConfigDir, RecommendedFileName)
|
RecommendedHomeFile = filepath.Join(RecommendedConfigDir, RecommendedFileName)
|
||||||
RecommendedSchemaFile = path.Join(RecommendedConfigDir, RecommendedSchemaName)
|
RecommendedSchemaFile = filepath.Join(RecommendedConfigDir, RecommendedSchemaName)
|
||||||
)
|
)
|
||||||
|
|
||||||
// currentMigrationRules returns a map that holds the history of recommended home directories used in previous versions.
|
// currentMigrationRules returns a map that holds the history of recommended home directories used in previous versions.
|
||||||
// Any future changes to RecommendedHomeFile and related are expected to add a migration rule here, in order to make
|
// Any future changes to RecommendedHomeFile and related are expected to add a migration rule here, in order to make
|
||||||
// sure existing config files are migrated to their new locations properly.
|
// sure existing config files are migrated to their new locations properly.
|
||||||
func currentMigrationRules() map[string]string {
|
func currentMigrationRules() map[string]string {
|
||||||
oldRecommendedHomeFile := path.Join(os.Getenv("HOME"), "/.kube/.kubeconfig")
|
var oldRecommendedHomeFileName string
|
||||||
oldRecommendedWindowsHomeFile := path.Join(os.Getenv("HOME"), RecommendedHomeDir, RecommendedFileName)
|
|
||||||
|
|
||||||
migrationRules := map[string]string{}
|
|
||||||
migrationRules[RecommendedHomeFile] = oldRecommendedHomeFile
|
|
||||||
if goruntime.GOOS == "windows" {
|
if goruntime.GOOS == "windows" {
|
||||||
migrationRules[RecommendedHomeFile] = oldRecommendedWindowsHomeFile
|
oldRecommendedHomeFileName = RecommendedFileName
|
||||||
|
} else {
|
||||||
|
oldRecommendedHomeFileName = ".kubeconfig"
|
||||||
|
}
|
||||||
|
return map[string]string{
|
||||||
|
RecommendedHomeFile: filepath.Join(os.Getenv("HOME"), RecommendedHomeDir, oldRecommendedHomeFileName),
|
||||||
}
|
}
|
||||||
return migrationRules
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConfigLoader interface {
|
type ClientConfigLoader interface {
|
||||||
@ -227,7 +225,7 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
|
|||||||
mapConfig := clientcmdapi.NewConfig()
|
mapConfig := clientcmdapi.NewConfig()
|
||||||
|
|
||||||
for _, kubeconfig := range kubeconfigs {
|
for _, kubeconfig := range kubeconfigs {
|
||||||
mergo.MergeWithOverwrite(mapConfig, kubeconfig)
|
mergo.Merge(mapConfig, kubeconfig, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge all of the struct values in the reverse order so that priority is given correctly
|
// merge all of the struct values in the reverse order so that priority is given correctly
|
||||||
@ -235,14 +233,14 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
|
|||||||
nonMapConfig := clientcmdapi.NewConfig()
|
nonMapConfig := clientcmdapi.NewConfig()
|
||||||
for i := len(kubeconfigs) - 1; i >= 0; i-- {
|
for i := len(kubeconfigs) - 1; i >= 0; i-- {
|
||||||
kubeconfig := kubeconfigs[i]
|
kubeconfig := kubeconfigs[i]
|
||||||
mergo.MergeWithOverwrite(nonMapConfig, kubeconfig)
|
mergo.Merge(nonMapConfig, kubeconfig, mergo.WithOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
// since values are overwritten, but maps values are not, we can merge the non-map config on top of the map config and
|
// since values are overwritten, but maps values are not, we can merge the non-map config on top of the map config and
|
||||||
// get the values we expect.
|
// get the values we expect.
|
||||||
config := clientcmdapi.NewConfig()
|
config := clientcmdapi.NewConfig()
|
||||||
mergo.MergeWithOverwrite(config, mapConfig)
|
mergo.Merge(config, mapConfig, mergo.WithOverride)
|
||||||
mergo.MergeWithOverwrite(config, nonMapConfig)
|
mergo.Merge(config, nonMapConfig, mergo.WithOverride)
|
||||||
|
|
||||||
if rules.ResolvePaths() {
|
if rules.ResolvePaths() {
|
||||||
if err := ResolveLocalPaths(config); err != nil {
|
if err := ResolveLocalPaths(config); err != nil {
|
||||||
@ -283,20 +281,15 @@ func (rules *ClientConfigLoadingRules) Migrate() error {
|
|||||||
return fmt.Errorf("cannot migrate %v to %v because it is a directory", source, destination)
|
return fmt.Errorf("cannot migrate %v to %v because it is a directory", source, destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
in, err := os.Open(source)
|
data, err := ioutil.ReadFile(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer in.Close()
|
// destination is created with mode 0666 before umask
|
||||||
out, err := os.Create(destination)
|
err = ioutil.WriteFile(destination, data, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer out.Close()
|
|
||||||
|
|
||||||
if _, err = io.Copy(out, in); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user