mirror of https://github.com/k3s-io/k3s
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
commit
eefd72557e
|
@ -68,7 +68,9 @@ func (o *PathOptions) GetEnvVarFiles() []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 {
|
||||
|
|
|
@ -139,7 +139,9 @@ func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
|
|||
|
||||
envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
|
||||
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 {
|
||||
chain = append(chain, RecommendedHomeFile)
|
||||
|
@ -615,3 +617,17 @@ func MakeRelative(path, base string) (string, error) {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -592,3 +592,30 @@ func Example_mergingEverythingNoConflicts() {
|
|||
// user:
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue