honor --cache-dir so that server resources and http-cache data are stored in the same location

Signed-off-by: SataQiu <1527062125@qq.com>
This commit is contained in:
SataQiu 2020-06-06 12:26:36 +08:00
parent 2dc7b68b83
commit e5d1dc953c

View File

@ -55,7 +55,10 @@ const (
flagHTTPCacheDir = "cache-dir" flagHTTPCacheDir = "cache-dir"
) )
var defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache") var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
defaultDiscoveryCacheParentDir = filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery")
)
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands // RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
// and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages // and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages
@ -224,14 +227,20 @@ 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
httpCacheDir, discoveryCacheParentDir := defaultCacheDir, defaultDiscoveryCacheParentDir
// retrieve a user-provided value for the "cache-dir" // retrieve a user-provided value for the "cache-dir"
// defaulting to ~/.kube/http-cache if no user-value is given. // override httpCacheDir and discoveryCacheDir if user-value is given.
httpCacheDir := defaultCacheDir
if f.CacheDir != nil { if f.CacheDir != nil {
httpCacheDir = *f.CacheDir httpCacheDir = *f.CacheDir
if len(httpCacheDir) > 0 {
// override discoveryCacheDir default value so that server resources and http-cache data are stored in the same location
discoveryCacheParentDir = filepath.Join(filepath.Dir(httpCacheDir), "cache", "discovery")
}
} }
discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery"), config.Host) discoveryCacheDir := computeDiscoverCacheDir(discoveryCacheParentDir, config.Host)
return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute)) return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute))
} }