From 18374dcbea121060c6c54a2fbededa6bb994aca2 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Thu, 25 Jun 2020 17:29:32 +0200 Subject: [PATCH] Add the ability to opt-out from config lock file Kubernetes-commit: 3574d88e22d68fc3291bf1c8432326d0e14fa3bd --- tools/clientcmd/config.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tools/clientcmd/config.go b/tools/clientcmd/config.go index 0babd45f..5f1660bf 100644 --- a/tools/clientcmd/config.go +++ b/tools/clientcmd/config.go @@ -58,6 +58,15 @@ type PathOptions struct { 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 { if len(o.EnvVar) == 0 { 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 // modified element. func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, relativizePaths bool) error { - possibleSources := configAccess.GetLoadingPrecedence() - // sort the possible kubeconfig files so we always "lock" in the same order - // to avoid deadlock (note: this can fail w/ symlinks, but... come on). - sort.Strings(possibleSources) - for _, filename := range possibleSources { - if err := lockFile(filename); err != nil { - return err + if UseModifyConfigLock { + possibleSources := configAccess.GetLoadingPrecedence() + // sort the possible kubeconfig files so we always "lock" in the same order + // to avoid deadlock (note: this can fail w/ symlinks, but... come on). + sort.Strings(possibleSources) + for _, filename := range possibleSources { + if err := lockFile(filename); err != nil { + return err + } + defer unlockFile(filename) } - defer unlockFile(filename) } startingConfig, err := configAccess.GetStartingConfig()