Skip to content

Commit 6c6cd2d

Browse files
committed
Setup cache dirs properly based on --cache-dir value
1 parent 896da22 commit 6c6cd2d

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ const (
5252
flagUsername = "username"
5353
flagPassword = "password"
5454
flagTimeout = "request-timeout"
55-
flagHTTPCacheDir = "cache-dir"
55+
flagCacheDir = "cache-dir"
5656
)
5757

5858
var (
59-
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache")
60-
defaultDiscoveryCacheParentDir = filepath.Join(homedir.HomeDir(), ".kube", "cache", "discovery")
59+
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "cache")
6160
)
6261

6362
// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
@@ -227,19 +226,15 @@ func (f *ConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, e
227226
// double it just so we don't end up here again for a while. This config is only used for discovery.
228227
config.Burst = 100
229228

230-
httpCacheDir, discoveryCacheParentDir := defaultCacheDir, defaultDiscoveryCacheParentDir
229+
cacheDir := defaultCacheDir
231230

232231
// retrieve a user-provided value for the "cache-dir"
233232
// override httpCacheDir and discoveryCacheDir if user-value is given.
234233
if f.CacheDir != nil {
235-
httpCacheDir = *f.CacheDir
236-
if len(httpCacheDir) > 0 {
237-
// override discoveryCacheDir default value so that server resources and http-cache data are stored in the same location
238-
discoveryCacheParentDir = filepath.Join(filepath.Dir(httpCacheDir), "cache", "discovery")
239-
}
234+
cacheDir = *f.CacheDir
240235
}
241-
242-
discoveryCacheDir := computeDiscoverCacheDir(discoveryCacheParentDir, config.Host)
236+
httpCacheDir := filepath.Join(cacheDir, "http")
237+
discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(cacheDir, "discovery"), config.Host)
243238

244239
return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(10*time.Minute))
245240
}
@@ -262,7 +257,7 @@ func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) {
262257
flags.StringVar(f.KubeConfig, "kubeconfig", *f.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
263258
}
264259
if f.CacheDir != nil {
265-
flags.StringVar(f.CacheDir, flagHTTPCacheDir, *f.CacheDir, "Default HTTP cache directory")
260+
flags.StringVar(f.CacheDir, flagCacheDir, *f.CacheDir, "Default cache directory")
266261
}
267262

268263
// add config options

staging/src/k8s.io/client-go/discovery/cached/disk/cached_discovery.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ func (d *CachedDiscoveryClient) Invalidate() {
268268
// CachedDiscoveryClient cache data. If httpCacheDir is empty, the restconfig's transport will not
269269
// be updated with a roundtripper that understands cache responses.
270270
// If discoveryCacheDir is empty, cached server resource data will be looked up in the current directory.
271-
// TODO(juanvallejo): the value of "--cache-dir" should be honored. Consolidate discoveryCacheDir with httpCacheDir
272-
// so that server resources and http-cache data are stored in the same location, provided via config flags.
273271
func NewCachedDiscoveryClientForConfig(config *restclient.Config, discoveryCacheDir, httpCacheDir string, ttl time.Duration) (*CachedDiscoveryClient, error) {
274272
if len(httpCacheDir) > 0 {
275273
// update the given restconfig with a custom roundtripper that

0 commit comments

Comments
 (0)