Merge pull request #67093 from juanvallejo/jvallejo/improve-config-context-update

Automatic merge from submit-queue (batch tested with PRs 67161, 67093, 67077). 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>.

improve kubeconfig file modification time

In cases where there are few destination filenames for a given
amount of contexts, but a large amount of contexts, this patch
prevents reading and writing to the same file (or small number
of files) over and over again needlessly.

**Release note**:
```release-note
Decrease the amount of time it takes to modify kubeconfig files with large amounts of contexts
```

cc @deads2k

Kubernetes-commit: 3587aa4206ddf502540d388466985bb51fc4a0e6
This commit is contained in:
Kubernetes Publisher 2018-08-09 19:31:01 -07:00
commit 961ecfb9a2
3 changed files with 145 additions and 89 deletions

164
Godeps/Godeps.json generated
View File

@ -272,331 +272,331 @@
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/admissionregistration/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/apps/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/apps/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/apps/v1beta2",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/authentication/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/authentication/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/authorization/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/authorization/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/autoscaling/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/autoscaling/v2beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/batch/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/batch/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/batch/v2alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/certificates/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/coordination/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/core/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/events/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/extensions/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/imagepolicy/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/networking/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/policy/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/rbac/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/rbac/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/rbac/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/scheduling/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/scheduling/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/settings/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/storage/v1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/storage/v1alpha1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/api/storage/v1beta1",
"Rev": "70491ec73e10be2989242c913738f049a37e72c7"
"Rev": "91bfdbcf0c2cab32ec1236cee4c300793abea68a"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/apitesting",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/apitesting/fuzzer",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/apitesting/roundtrip",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/errors",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/meta",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/api/resource",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/fuzzer",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/internalversion",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1beta1",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/conversion",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/conversion/queryparams",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/fields",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/labels",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/schema",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/json",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/recognizer",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/streaming",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/versioning",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/selection",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/types",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/cache",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/clock",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/diff",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/errors",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/framer",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream/spdy",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/json",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/mergepatch",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/naming",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/net",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/remotecommand",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/sets",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/strategicpatch",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/validation",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/validation/field",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/wait",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/util/yaml",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/version",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/pkg/watch",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
"Rev": "cd53e6e3b3a5f792de51f8d95fb17f46f0627abf"
"Rev": "720795f37ef04a6d63ba53b0b43233b3f851c149"
},
{
"ImportPath": "k8s.io/kube-openapi/pkg/util/proto",

View File

@ -126,6 +126,46 @@ func TestMergeContext(t *testing.T) {
matchStringArg(namespace, actual, t)
}
func TestModifyContext(t *testing.T) {
expectedCtx := map[string]bool{
"updated": true,
"clean": true,
}
pathOptions := NewDefaultPathOptions()
config := createValidTestConfig()
// define new context and assign it - our path options config
config.Contexts["updated"] = &clientcmdapi.Context{
Cluster: "updated",
AuthInfo: "updated",
}
config.CurrentContext = "updated"
if err := ModifyConfig(pathOptions, *config, true); err != nil {
t.Errorf("Unexpected error: %v", err)
}
startingConfig, err := pathOptions.GetStartingConfig()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// make sure the current context was updated
matchStringArg("updated", startingConfig.CurrentContext, 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))
}
for key := range startingConfig.Contexts {
if !expectedCtx[key] {
t.Fatalf("expected context %q to exist", key)
}
}
}
func TestCertificateData(t *testing.T) {
caData := []byte("ca-data")
certData := []byte("cert-data")

View File

@ -220,6 +220,9 @@ func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, rela
}
}
// seenConfigs stores a map of config source filenames to computed config objects
seenConfigs := map[string]*clientcmdapi.Config{}
for key, context := range newConfig.Contexts {
startingContext, exists := startingConfig.Contexts[key]
if !reflect.DeepEqual(context, startingContext) || !exists {
@ -228,15 +231,28 @@ func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, rela
destinationFile = configAccess.GetDefaultFilename()
}
configToWrite, err := getConfigFromFile(destinationFile)
if err != nil {
return err
// we only obtain a fresh config object from its source file
// if we have not seen it already - this prevents us from
// reading and writing to the same number of files repeatedly
// when multiple / all contexts share the same destination file.
configToWrite, seen := seenConfigs[destinationFile]
if !seen {
var err error
configToWrite, err = getConfigFromFile(destinationFile)
if err != nil {
return err
}
seenConfigs[destinationFile] = configToWrite
}
configToWrite.Contexts[key] = context
if err := WriteToFile(*configToWrite, destinationFile); err != nil {
return err
}
configToWrite.Contexts[key] = context
}
}
// actually persist config object changes
for destinationFile, configToWrite := range seenConfigs {
if err := WriteToFile(*configToWrite, destinationFile); err != nil {
return err
}
}