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
```

Kubernetes-commit: eefd72557ef7ce008e2138ca7d91f6a8d8634f73
This commit is contained in:
Kubernetes Publisher
2018-03-19 19:35:26 -07:00
4 changed files with 76 additions and 31 deletions

58
Godeps/Godeps.json generated
View File

@@ -248,119 +248,119 @@
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/apps/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/apps/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/apps/v1beta2",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/authentication/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/authentication/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/authorization/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/authorization/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/autoscaling/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/autoscaling/v2beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/batch/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/batch/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/batch/v2alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/certificates/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/core/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/events/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/extensions/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/imagepolicy/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/networking/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/policy/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/rbac/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/rbac/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/rbac/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/scheduling/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/settings/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/storage/v1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/storage/v1alpha1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/api/storage/v1beta1",
"Rev": "927313aa2f67b6f51d3ea954027cb009efb31941"
"Rev": "fd252c3a3e1debf912ff5b80221a31a6a3c24493"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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)
}
}
}