Merge pull request #93293 from soltysh/loading_precedence
kubeconfig: add explicit path, if specified to loading precedence
This commit is contained in:
		| @@ -288,7 +288,7 @@ func TestModifyContext(t *testing.T) { | |||||||
|  |  | ||||||
| 	// there should now be two contexts | 	// there should now be two contexts | ||||||
| 	if len(startingConfig.Contexts) != len(expectedCtx) { | 	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 { | 	for key := range startingConfig.Contexts { | ||||||
|   | |||||||
| @@ -83,10 +83,13 @@ func (o *PathOptions) GetEnvVarFiles() []string { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (o *PathOptions) GetLoadingPrecedence() []string { | func (o *PathOptions) GetLoadingPrecedence() []string { | ||||||
|  | 	if o.IsExplicitFile() { | ||||||
|  | 		return []string{o.GetExplicitFile()} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if envVarFiles := o.GetEnvVarFiles(); len(envVarFiles) > 0 { | 	if envVarFiles := o.GetEnvVarFiles(); len(envVarFiles) > 0 { | ||||||
| 		return envVarFiles | 		return envVarFiles | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return []string{o.GlobalFile} | 	return []string{o.GlobalFile} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -304,6 +304,10 @@ func (rules *ClientConfigLoadingRules) Migrate() error { | |||||||
|  |  | ||||||
| // GetLoadingPrecedence implements ConfigAccess | // GetLoadingPrecedence implements ConfigAccess | ||||||
| func (rules *ClientConfigLoadingRules) GetLoadingPrecedence() []string { | func (rules *ClientConfigLoadingRules) GetLoadingPrecedence() []string { | ||||||
|  | 	if len(rules.ExplicitPath) > 0 { | ||||||
|  | 		return []string{rules.ExplicitPath} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return rules.Precedence | 	return rules.Precedence | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot