Skip to content

Commit

Permalink
kubeconfig: Use names more explicit than kubeconf{1,2}
Browse files Browse the repository at this point in the history
This improves readability, no need to always try to remember if
'kubeconf1' is the global kubeconfig file, or the file we want to merge
in it
  • Loading branch information
vyasgun authored and cfergeau committed Jan 5, 2024
1 parent b047b1b commit 9f8f5e9
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/crc/machine/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,32 +246,33 @@ func contains(arr []string, str string) bool {
}

func mergeKubeConfigFile(kubeConfigFile string) error {
globalConfigPath, kubeConf1, err := getGlobalKubeConfig()
globalConfigPath, globalConf, err := getGlobalKubeConfig()
if err != nil {
return err
}
kubeConf2, err := clientcmd.LoadFromFile(kubeConfigFile)

currentConf, err := clientcmd.LoadFromFile(kubeConfigFile)
if err != nil {
return err
}
// Merge the kubeConf2 to globalConfig
mergedConfig, err := clientcmd.NewDefaultClientConfig(*kubeConf1, &clientcmd.ConfigOverrides{}).ConfigAccess().GetStartingConfig()
// Merge the currentConf to globalConfig
mergedConfig, err := clientcmd.NewDefaultClientConfig(*globalConf, &clientcmd.ConfigOverrides{}).ConfigAccess().GetStartingConfig()
if err != nil {
return err
}

for name, cluster := range kubeConf2.Clusters {
for name, cluster := range currentConf.Clusters {
mergedConfig.Clusters[name] = cluster
}

for name, authInfo := range kubeConf2.AuthInfos {
for name, authInfo := range currentConf.AuthInfos {
mergedConfig.AuthInfos[name] = authInfo
}

for name, context := range kubeConf2.Contexts {
for name, context := range currentConf.Contexts {
mergedConfig.Contexts[name] = context
}

mergedConfig.CurrentContext = kubeConf2.CurrentContext
mergedConfig.CurrentContext = currentConf.CurrentContext
return clientcmd.WriteToFile(*mergedConfig, globalConfigPath)
}

0 comments on commit 9f8f5e9

Please sign in to comment.