Merge pull request #92513 from soltysh/client_lock

Add the ability to opt-out from config lock file

Kubernetes-commit: 2585d78c79054a6d85620eb5430a1498bb747967
This commit is contained in:
Kubernetes Publisher 2020-07-01 23:13:08 -07:00
commit 4088fed933
4 changed files with 23 additions and 12 deletions

2
Godeps/Godeps.json generated
View File

@ -444,7 +444,7 @@
}, },
{ {
"ImportPath": "k8s.io/apimachinery", "ImportPath": "k8s.io/apimachinery",
"Rev": "3c7bc0acc576" "Rev": "d66960f508af"
}, },
{ {
"ImportPath": "k8s.io/gengo", "ImportPath": "k8s.io/gengo",

4
go.mod
View File

@ -27,7 +27,7 @@ require (
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 golang.org/x/time v0.0.0-20191024005414-555d28b269f0
k8s.io/api v0.0.0-20200630090439-aaebd44608df k8s.io/api v0.0.0-20200630090439-aaebd44608df
k8s.io/apimachinery v0.0.0-20200701090254-3c7bc0acc576 k8s.io/apimachinery v0.0.0-20200701170247-d66960f508af
k8s.io/klog/v2 v2.1.0 k8s.io/klog/v2 v2.1.0
k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19 k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19
sigs.k8s.io/yaml v1.2.0 sigs.k8s.io/yaml v1.2.0
@ -35,5 +35,5 @@ require (
replace ( replace (
k8s.io/api => k8s.io/api v0.0.0-20200630090439-aaebd44608df k8s.io/api => k8s.io/api v0.0.0-20200630090439-aaebd44608df
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200701090254-3c7bc0acc576 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200701170247-d66960f508af
) )

2
go.sum
View File

@ -331,7 +331,7 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
k8s.io/api v0.0.0-20200630090439-aaebd44608df/go.mod h1:cQk/DBCsCL81chJRAZjSBZ4suDeNugUZE3LqnYmbQ8Q= k8s.io/api v0.0.0-20200630090439-aaebd44608df/go.mod h1:cQk/DBCsCL81chJRAZjSBZ4suDeNugUZE3LqnYmbQ8Q=
k8s.io/apimachinery v0.0.0-20200701090254-3c7bc0acc576/go.mod h1:m5QoVMHU94aieNFPfLqf7WmU8HLVLm3JasG64COkSXQ= k8s.io/apimachinery v0.0.0-20200701170247-d66960f508af/go.mod h1:m5QoVMHU94aieNFPfLqf7WmU8HLVLm3JasG64COkSXQ=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog/v2 v2.0.0 h1:Foj74zO6RbjjP4hBEKjnYtjjAhGg4jNynUdYF6fJrok= k8s.io/klog/v2 v2.0.0 h1:Foj74zO6RbjjP4hBEKjnYtjjAhGg4jNynUdYF6fJrok=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=

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()