restore old cached client behavior

This commit is contained in:
juanvallejo 2018-05-11 15:50:13 -04:00
parent 4059355743
commit 57f308ae60
5 changed files with 31 additions and 9 deletions

View File

@ -420,7 +420,7 @@ func (f *TestFactory) DiscoveryClient() (discovery.CachedDiscoveryInterface, err
fakeClient := f.Client.(*fake.RESTClient) fakeClient := f.Client.(*fake.RESTClient)
cacheDir := filepath.Join("", ".kube", "cache", "discovery") 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -52,6 +52,8 @@ const (
flagHTTPCacheDir = "cache-dir" flagHTTPCacheDir = "cache-dir"
) )
var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
// TODO(juanvallejo): move to pkg/kubectl/genericclioptions once // TODO(juanvallejo): move to pkg/kubectl/genericclioptions once
// the dependency on cmdutil is broken here. // the dependency on cmdutil is broken here.
// ConfigFlags composes the set of values necessary // 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. // double it just so we don't end up here again for a while. This config is only used for discovery.
config.Burst = 100 config.Burst = 100
cacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host) // retrieve a user-provided value for the "cache-dir"
return discovery.NewCachedDiscoveryClientForConfig(config, cacheDir, time.Duration(10*time.Minute)) // 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. // RESTMapper returns a mapper.
@ -271,6 +280,7 @@ func NewConfigFlags() *ConfigFlags {
Timeout: stringptr("0"), Timeout: stringptr("0"),
KubeConfig: stringptr(""), KubeConfig: stringptr(""),
CacheDir: stringptr(defaultCacheDir),
ClusterName: stringptr(""), ClusterName: stringptr(""),
AuthInfoName: stringptr(""), AuthInfoName: stringptr(""),
Context: stringptr(""), Context: stringptr(""),

View File

@ -239,10 +239,18 @@ func (d *CachedDiscoveryClient) Invalidate() {
} }
// NewCachedDiscoveryClientForConfig creates a new DiscoveryClient for the given config, and wraps // 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. // custom transport that understands cache responses.
func NewCachedDiscoveryClientForConfig(config *restclient.Config, cacheDirectory string, ttl time.Duration) (*CachedDiscoveryClient, error) { // We receive two distinct cache directories for now, in order to preserve old behavior
if len(cacheDirectory) > 0 { // 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 // update the given restconfig with a custom roundtripper that
// understands how to handle cache responses. // understands how to handle cache responses.
wt := config.WrapTransport wt := config.WrapTransport
@ -250,7 +258,7 @@ func NewCachedDiscoveryClientForConfig(config *restclient.Config, cacheDirectory
if wt != nil { if wt != nil {
rt = wt(rt) 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 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. // 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.

View File

@ -82,6 +82,10 @@
"ImportPath": "github.com/modern-go/reflect2", "ImportPath": "github.com/modern-go/reflect2",
"Rev": "05fbef0ca5da472bbf96c9322b84a53edc03c9fd" "Rev": "05fbef0ca5da472bbf96c9322b84a53edc03c9fd"
}, },
{
"ImportPath": "github.com/peterbourgon/diskv",
"Rev": "5f041e8faa004a95c88a202771f4cc3e991971e6"
},
{ {
"ImportPath": "golang.org/x/crypto/ssh/terminal", "ImportPath": "golang.org/x/crypto/ssh/terminal",
"Rev": "49796115aa4b964c318aad4f3084fdb41e9aa067" "Rev": "49796115aa4b964c318aad4f3084fdb41e9aa067"

View File

@ -165,7 +165,7 @@ func TestServerSidePrint(t *testing.T) {
os.Remove(cacheDir) 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 { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }