diff --git a/pkg/kubectl/cmd/testing/fake.go b/pkg/kubectl/cmd/testing/fake.go index 499df5a9d9f..402edf38947 100644 --- a/pkg/kubectl/cmd/testing/fake.go +++ b/pkg/kubectl/cmd/testing/fake.go @@ -420,7 +420,7 @@ func (f *TestFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface, err fakeClient := f.Client.(*fake.RESTClient) cacheDir := filepath.Join("", ".kube", "cache", "discovery") - cachedClient, err := discovery.NewCachedDiscoveryClientForConfig(f.ClientConfigVal, cacheDir, time.Duration(10*time.Minute)) + cachedClient, err := discovery.NewCachedDiscoveryClientForConfig(f.ClientConfigVal, cacheDir, "", time.Duration(10*time.Minute)) if err != nil { return nil, err } diff --git a/pkg/kubectl/cmd/util/config_flags.go b/pkg/kubectl/cmd/util/config_flags.go index 510d29a90a1..8f3a4ba2f3b 100644 --- a/pkg/kubectl/cmd/util/config_flags.go +++ b/pkg/kubectl/cmd/util/config_flags.go @@ -52,6 +52,8 @@ const ( flagHTTPCacheDir = "cache-dir" ) +var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache") + // TODO(juanvallejo): move to pkg/kubectl/genericclioptions once // the dependency on cmdutil is broken here. // ConfigFlags composes the set of values necessary @@ -176,8 +178,15 @@ func (f *ConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, e // double it just so we don't end up here again for a while. This config is only used for discovery. config.Burst = 100 - cacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host) - return discovery.NewCachedDiscoveryClientForConfig(config, cacheDir, time.Duration(10*time.Minute)) + // retrieve a user-provided value for the "cache-dir" + // defaulting to ~/.kube/http-cache if no user-value is given. + httpCacheDir := defaultCacheDir + if f.CacheDir != nil { + httpCacheDir = *f.CacheDir + } + + discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host) + return discovery.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute)) } // RESTMapper returns a mapper. @@ -271,6 +280,7 @@ func NewConfigFlags() *ConfigFlags { Timeout: stringptr("0"), KubeConfig: stringptr(""), + CacheDir: stringptr(defaultCacheDir), ClusterName: stringptr(""), AuthInfoName: stringptr(""), Context: stringptr(""), diff --git a/staging/src/k8s.io/client-go/discovery/cached_discovery.go b/staging/src/k8s.io/client-go/discovery/cached_discovery.go index 0cd814ab721..aca46546e62 100644 --- a/staging/src/k8s.io/client-go/discovery/cached_discovery.go +++ b/staging/src/k8s.io/client-go/discovery/cached_discovery.go @@ -239,10 +239,18 @@ func (d *CachedDiscoveryClient) Invalidate() { } // NewCachedDiscoveryClientForConfig creates a new DiscoveryClient for the given config, and wraps -// the created client in a CachedDiscoveryClient. The provided configuration is upddated with a +// the created client in a CachedDiscoveryClient. The provided configuration is updated with a // custom transport that understands cache responses. -func NewCachedDiscoveryClientForConfig(config *restclient.Config, cacheDirectory string, ttl time.Duration) (*CachedDiscoveryClient, error) { - if len(cacheDirectory) > 0 { +// We receive two distinct cache directories for now, in order to preserve old behavior +// which makes use of the --cache-dir flag value for storing cache data from the CacheRoundTripper, +// and makes use of the hardcoded destination (~/.kube/cache/discovery/...) for storing +// CachedDiscoveryClient cache data. If httpCacheDir is empty, the restconfig's transport will not +// be updated with a roundtripper that understands cache responses. +// If discoveryCacheDir is empty, cached server resource data will be looked up in the current directory. +// TODO(juanvallejo): the value of "--cache-dir" should be honored. Consolidate discoveryCacheDir with httpCacheDir +// so that server resources and http-cache data are stored in the same location, provided via config flags. +func NewCachedDiscoveryClientForConfig(config *restclient.Config, discoveryCacheDir, httpCacheDir string, ttl time.Duration) (*CachedDiscoveryClient, error) { + if len(httpCacheDir) > 0 { // update the given restconfig with a custom roundtripper that // understands how to handle cache responses. wt := config.WrapTransport @@ -250,7 +258,7 @@ func NewCachedDiscoveryClientForConfig(config *restclient.Config, cacheDirectory if wt != nil { rt = wt(rt) } - return newCacheRoundTripper(cacheDirectory, rt) + return newCacheRoundTripper(httpCacheDir, rt) } } @@ -259,7 +267,7 @@ func NewCachedDiscoveryClientForConfig(config *restclient.Config, cacheDirectory return nil, err } - return newCachedDiscoveryClient(discoveryClient, cacheDirectory, ttl), nil + return newCachedDiscoveryClient(discoveryClient, discoveryCacheDir, ttl), nil } // NewCachedDiscoveryClient creates a new DiscoveryClient. cacheDirectory is the directory where discovery docs are held. It must be unique per host:port combination to work well. diff --git a/staging/src/k8s.io/metrics/Godeps/Godeps.json b/staging/src/k8s.io/metrics/Godeps/Godeps.json index 7439204f642..a6a0272e673 100644 --- a/staging/src/k8s.io/metrics/Godeps/Godeps.json +++ b/staging/src/k8s.io/metrics/Godeps/Godeps.json @@ -82,6 +82,10 @@ "ImportPath": "github.com/modern-go/reflect2", "Rev": "05fbef0ca5da472bbf96c9322b84a53edc03c9fd" }, + { + "ImportPath": "github.com/peterbourgon/diskv", + "Rev": "5f041e8faa004a95c88a202771f4cc3e991971e6" + }, { "ImportPath": "golang.org/x/crypto/ssh/terminal", "Rev": "49796115aa4b964c318aad4f3084fdb41e9aa067" diff --git a/test/integration/apiserver/print_test.go b/test/integration/apiserver/print_test.go index 2f9bf3a682c..16c7e0ff12c 100644 --- a/test/integration/apiserver/print_test.go +++ b/test/integration/apiserver/print_test.go @@ -165,7 +165,7 @@ func TestServerSidePrint(t *testing.T) { os.Remove(cacheDir) }() - cachedClient, err := discovery.NewCachedDiscoveryClientForConfig(restConfig, cacheDir, time.Duration(10*time.Minute)) + cachedClient, err := discovery.NewCachedDiscoveryClientForConfig(restConfig, cacheDir, "", time.Duration(10*time.Minute)) if err != nil { t.Errorf("unexpected error: %v", err) }