Merge pull request #93293 from soltysh/loading_precedence

kubeconfig: add explicit path, if specified to loading precedence

Kubernetes-commit: 583d01a9bfc0c403bc8e1043f7046f1c8b5d7881
This commit is contained in:
Kubernetes Publisher 2020-11-04 13:24:53 -08:00
commit bcecfeab18
4 changed files with 52 additions and 2 deletions

View File

@ -288,7 +288,7 @@ func TestModifyContext(t *testing.T) {
// there should now be two contexts
if len(startingConfig.Contexts) != len(expectedCtx) {
t.Fatalf("unexpected nuber of contexts, expecting %v, but found %v", len(expectedCtx), len(startingConfig.Contexts))
t.Fatalf("unexpected number of contexts, expecting %v, but found %v", len(expectedCtx), len(startingConfig.Contexts))
}
for key := range startingConfig.Contexts {

View File

@ -83,10 +83,13 @@ func (o *PathOptions) GetEnvVarFiles() []string {
}
func (o *PathOptions) GetLoadingPrecedence() []string {
if o.IsExplicitFile() {
return []string{o.GetExplicitFile()}
}
if envVarFiles := o.GetEnvVarFiles(); len(envVarFiles) > 0 {
return envVarFiles
}
return []string{o.GlobalFile}
}

View File

@ -304,6 +304,10 @@ func (rules *ClientConfigLoadingRules) Migrate() error {
// GetLoadingPrecedence implements ConfigAccess
func (rules *ClientConfigLoadingRules) GetLoadingPrecedence() []string {
if len(rules.ExplicitPath) > 0 {
return []string{rules.ExplicitPath}
}
return rules.Precedence
}

View File

@ -865,3 +865,46 @@ func TestDeduplicate(t *testing.T) {
}
}
}
func TestLoadingGetLoadingPrecedence(t *testing.T) {
testCases := map[string]struct {
rules *ClientConfigLoadingRules
env string
precedence []string
}{
"default": {
precedence: []string{filepath.Join(os.Getenv("HOME"), ".kube/config")},
},
"explicit": {
rules: &ClientConfigLoadingRules{
ExplicitPath: "/explicit/kubeconfig",
},
precedence: []string{"/explicit/kubeconfig"},
},
"envvar-single": {
env: "/env/kubeconfig",
precedence: []string{"/env/kubeconfig"},
},
"envvar-multiple": {
env: "/env/kubeconfig:/other/kubeconfig",
precedence: []string{"/env/kubeconfig", "/other/kubeconfig"},
},
}
kubeconfig := os.Getenv("KUBECONFIG")
defer os.Setenv("KUBECONFIG", kubeconfig)
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
os.Setenv("KUBECONFIG", test.env)
rules := test.rules
if rules == nil {
rules = NewDefaultClientConfigLoadingRules()
}
actual := rules.GetLoadingPrecedence()
if !reflect.DeepEqual(actual, test.precedence) {
t.Errorf("expect %v, got %v", test.precedence, actual)
}
})
}
}