Add the ability to opt-out from config lock file

This commit is contained in:
Maciej Szulik 2020-06-25 17:29:32 +02:00
parent 180af4240c
commit 3574d88e22
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4

View File

@ -58,6 +58,15 @@ type PathOptions struct {
LoadingRules *ClientConfigLoadingRules LoadingRules *ClientConfigLoadingRules
} }
var (
// UseModifyConfigLock ensures that access to kubeconfig file using ModifyConfig method
// is being guarded by a lock file.
// This variable is intentionaly made public so other consumers of this library
// can modify its default behavior, but be caution when disabling it since
// this will make your code not threadsafe.
UseModifyConfigLock = true
)
func (o *PathOptions) GetEnvVarFiles() []string { func (o *PathOptions) GetEnvVarFiles() []string {
if len(o.EnvVar) == 0 { if len(o.EnvVar) == 0 {
return []string{} return []string{}
@ -156,15 +165,17 @@ func NewDefaultPathOptions() *PathOptions {
// that means that this code will only write into a single file. If you want to relativizePaths, you must provide a fully qualified path in any // that means that this code will only write into a single file. If you want to relativizePaths, you must provide a fully qualified path in any
// modified element. // modified element.
func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, relativizePaths bool) error { func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, relativizePaths bool) error {
possibleSources := configAccess.GetLoadingPrecedence() if UseModifyConfigLock {
// sort the possible kubeconfig files so we always "lock" in the same order possibleSources := configAccess.GetLoadingPrecedence()
// to avoid deadlock (note: this can fail w/ symlinks, but... come on). // sort the possible kubeconfig files so we always "lock" in the same order
sort.Strings(possibleSources) // to avoid deadlock (note: this can fail w/ symlinks, but... come on).
for _, filename := range possibleSources { sort.Strings(possibleSources)
if err := lockFile(filename); err != nil { for _, filename := range possibleSources {
return err if err := lockFile(filename); err != nil {
return err
}
defer unlockFile(filename)
} }
defer unlockFile(filename)
} }
startingConfig, err := configAccess.GetStartingConfig() startingConfig, err := configAccess.GetStartingConfig()