mirror of https://github.com/k3s-io/k3s
Merge pull request #7000 from deads2k/deads-allow-many-modifications
allow multiple changes in kubeconfig modifyconfigpull/6/head
commit
29875191ad
|
@ -186,7 +186,7 @@ func (o *PathOptions) GetExplicitFile() string {
|
||||||
// ModifyConfig takes a Config object, iterates through Clusters, AuthInfos, and Contexts, uses the LocationOfOrigin if specified or
|
// ModifyConfig takes a Config object, iterates through Clusters, AuthInfos, and Contexts, uses the LocationOfOrigin if specified or
|
||||||
// uses the default destination file to write the results into. This results in multiple file reads, but it's very easy to follow.
|
// uses the default destination file to write the results into. This results in multiple file reads, but it's very easy to follow.
|
||||||
// Preferences and CurrentContext should always be set in the default destination file. Since we can't distinguish between empty and missing values
|
// Preferences and CurrentContext should always be set in the default destination file. Since we can't distinguish between empty and missing values
|
||||||
// (no nil strings), we're forced have separate handling for them. In all the currently known cases, newConfig should have, at most, one difference,
|
// (no nil strings), we're forced have separate handling for them. In the kubeconfig cases, newConfig should have at most one difference,
|
||||||
// that means that this code will only write into a single file.
|
// that means that this code will only write into a single file.
|
||||||
func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config) error {
|
func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config) error {
|
||||||
startingConfig, err := configAccess.GetStartingConfig()
|
startingConfig, err := configAccess.GetStartingConfig()
|
||||||
|
@ -194,124 +194,123 @@ func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// at this point, config and startingConfig should have, at most, one difference. We need to chase the difference until we find it
|
// We need to find all differences, locate their original files, read a partial config to modify only that stanza and write out the file.
|
||||||
// then we'll build a partial config object to call write upon. Special case the test for current context and preferences since those
|
// Special case the test for current context and preferences since those always write to the default file.
|
||||||
// always write to the default file.
|
if reflect.DeepEqual(*startingConfig, newConfig) {
|
||||||
switch {
|
|
||||||
case reflect.DeepEqual(*startingConfig, newConfig):
|
|
||||||
// nothing to do
|
// nothing to do
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
case startingConfig.CurrentContext != newConfig.CurrentContext:
|
if startingConfig.CurrentContext != newConfig.CurrentContext {
|
||||||
if err := writeCurrentContext(configAccess, newConfig.CurrentContext); err != nil {
|
if err := writeCurrentContext(configAccess, newConfig.CurrentContext); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case !reflect.DeepEqual(startingConfig.Preferences, newConfig.Preferences):
|
if !reflect.DeepEqual(startingConfig.Preferences, newConfig.Preferences) {
|
||||||
if err := writePreferences(configAccess, newConfig.Preferences); err != nil {
|
if err := writePreferences(configAccess, newConfig.Preferences); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
// Search every cluster, authInfo, and context. First from new to old for differences, then from old to new for deletions
|
||||||
// something is different. Search every cluster, authInfo, and context. First from new to old for differences, then from old to new for deletions
|
for key, cluster := range newConfig.Clusters {
|
||||||
for key, cluster := range newConfig.Clusters {
|
startingCluster, exists := startingConfig.Clusters[key]
|
||||||
startingCluster, exists := startingConfig.Clusters[key]
|
if !reflect.DeepEqual(cluster, startingCluster) || !exists {
|
||||||
if !reflect.DeepEqual(cluster, startingCluster) || !exists {
|
destinationFile := cluster.LocationOfOrigin
|
||||||
destinationFile := cluster.LocationOfOrigin
|
if len(destinationFile) == 0 {
|
||||||
if len(destinationFile) == 0 {
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
}
|
||||||
}
|
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
configToWrite.Clusters[key] = cluster
|
configToWrite.Clusters[key] = cluster
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for key, context := range newConfig.Contexts {
|
for key, context := range newConfig.Contexts {
|
||||||
startingContext, exists := startingConfig.Contexts[key]
|
startingContext, exists := startingConfig.Contexts[key]
|
||||||
if !reflect.DeepEqual(context, startingContext) || !exists {
|
if !reflect.DeepEqual(context, startingContext) || !exists {
|
||||||
destinationFile := context.LocationOfOrigin
|
destinationFile := context.LocationOfOrigin
|
||||||
if len(destinationFile) == 0 {
|
if len(destinationFile) == 0 {
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
}
|
}
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
configToWrite.Contexts[key] = context
|
configToWrite.Contexts[key] = context
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for key, authInfo := range newConfig.AuthInfos {
|
for key, authInfo := range newConfig.AuthInfos {
|
||||||
startingAuthInfo, exists := startingConfig.AuthInfos[key]
|
startingAuthInfo, exists := startingConfig.AuthInfos[key]
|
||||||
if !reflect.DeepEqual(authInfo, startingAuthInfo) || !exists {
|
if !reflect.DeepEqual(authInfo, startingAuthInfo) || !exists {
|
||||||
destinationFile := authInfo.LocationOfOrigin
|
destinationFile := authInfo.LocationOfOrigin
|
||||||
if len(destinationFile) == 0 {
|
if len(destinationFile) == 0 {
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
}
|
}
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
configToWrite.AuthInfos[key] = authInfo
|
configToWrite.AuthInfos[key] = authInfo
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for key, cluster := range startingConfig.Clusters {
|
for key, cluster := range startingConfig.Clusters {
|
||||||
if _, exists := newConfig.Clusters[key]; !exists {
|
if _, exists := newConfig.Clusters[key]; !exists {
|
||||||
destinationFile := cluster.LocationOfOrigin
|
destinationFile := cluster.LocationOfOrigin
|
||||||
if len(destinationFile) == 0 {
|
if len(destinationFile) == 0 {
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
}
|
}
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
delete(configToWrite.Clusters, key)
|
delete(configToWrite.Clusters, key)
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for key, context := range startingConfig.Contexts {
|
for key, context := range startingConfig.Contexts {
|
||||||
if _, exists := newConfig.Contexts[key]; !exists {
|
if _, exists := newConfig.Contexts[key]; !exists {
|
||||||
destinationFile := context.LocationOfOrigin
|
destinationFile := context.LocationOfOrigin
|
||||||
if len(destinationFile) == 0 {
|
if len(destinationFile) == 0 {
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
}
|
}
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
delete(configToWrite.Contexts, key)
|
delete(configToWrite.Contexts, key)
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for key, authInfo := range startingConfig.AuthInfos {
|
for key, authInfo := range startingConfig.AuthInfos {
|
||||||
if _, exists := newConfig.AuthInfos[key]; !exists {
|
if _, exists := newConfig.AuthInfos[key]; !exists {
|
||||||
destinationFile := authInfo.LocationOfOrigin
|
destinationFile := authInfo.LocationOfOrigin
|
||||||
if len(destinationFile) == 0 {
|
if len(destinationFile) == 0 {
|
||||||
destinationFile = configAccess.GetDefaultFilename()
|
destinationFile = configAccess.GetDefaultFilename()
|
||||||
}
|
}
|
||||||
|
|
||||||
configToWrite := getConfigFromFileOrDie(destinationFile)
|
configToWrite := getConfigFromFileOrDie(destinationFile)
|
||||||
delete(configToWrite.AuthInfos, key)
|
delete(configToWrite.AuthInfos, key)
|
||||||
|
|
||||||
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
if err := clientcmd.WriteToFile(*configToWrite, destinationFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue