Merge pull request #53880 from wackxu/kubeconfig

Automatic merge from submit-queue (batch tested with PRs 51423, 53880). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

prevent the same path load multiple times

**What this PR does / why we need it**:

prevent the same path load multiple times,if there are several same path, we only load one time

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #53723

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/8/head
Kubernetes Submit Queue 2018-03-19 19:35:26 -07:00 committed by GitHub
commit eefd72557e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View File

@ -68,7 +68,9 @@ func (o *PathOptions) GetEnvVarFiles() []string {
return []string{} return []string{}
} }
return filepath.SplitList(envVarValue) fileList := filepath.SplitList(envVarValue)
// prevent the same path load multiple times
return deduplicate(fileList)
} }
func (o *PathOptions) GetLoadingPrecedence() []string { func (o *PathOptions) GetLoadingPrecedence() []string {

View File

@ -139,7 +139,9 @@ func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
envVarFiles := os.Getenv(RecommendedConfigPathEnvVar) envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
if len(envVarFiles) != 0 { if len(envVarFiles) != 0 {
chain = append(chain, filepath.SplitList(envVarFiles)...) fileList := filepath.SplitList(envVarFiles)
// prevent the same path load multiple times
chain = append(chain, deduplicate(fileList)...)
} else { } else {
chain = append(chain, RecommendedHomeFile) chain = append(chain, RecommendedHomeFile)
@ -615,3 +617,17 @@ func MakeRelative(path, base string) (string, error) {
} }
return path, nil return path, nil
} }
// deduplicate removes any duplicated values and returns a new slice, keeping the order unchanged
func deduplicate(s []string) []string {
encountered := map[string]bool{}
ret := make([]string, 0)
for i := range s {
if encountered[s[i]] {
continue
}
encountered[s[i]] = true
ret = append(ret, s[i])
}
return ret
}

View File

@ -592,3 +592,30 @@ func Example_mergingEverythingNoConflicts() {
// user: // user:
// token: red-token // token: red-token
} }
func TestDeduplicate(t *testing.T) {
testCases := []struct {
src []string
expect []string
}{
{
src: []string{"a", "b", "c", "d", "e", "f"},
expect: []string{"a", "b", "c", "d", "e", "f"},
},
{
src: []string{"a", "b", "c", "b", "e", "f"},
expect: []string{"a", "b", "c", "e", "f"},
},
{
src: []string{"a", "a", "b", "b", "c", "b"},
expect: []string{"a", "b", "c"},
},
}
for _, testCase := range testCases {
get := deduplicate(testCase.src)
if !reflect.DeepEqual(get, testCase.expect) {
t.Errorf("expect: %v, get: %v", testCase.expect, get)
}
}
}