mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 13:50:01 +00:00 
			
		
		
		
	Setup cache dirs properly based on --cache-dir value
This commit is contained in:
		| @@ -52,12 +52,11 @@ const ( | |||||||
| 	flagUsername         = "username" | 	flagUsername         = "username" | ||||||
| 	flagPassword         = "password" | 	flagPassword         = "password" | ||||||
| 	flagTimeout          = "request-timeout" | 	flagTimeout          = "request-timeout" | ||||||
| 	flagHTTPCacheDir     = "cache-dir" | 	flagCacheDir         = "cache-dir" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| 	defaultCacheDir                = filepath.Join(homedir.HomeDir(), ".kube", "http-cache") | 	defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "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 | ||||||
| @@ -227,19 +226,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 | ||||||
|  |  | ||||||
| 	httpCacheDir, discoveryCacheParentDir := defaultCacheDir, defaultDiscoveryCacheParentDir | 	cacheDir := defaultCacheDir | ||||||
|  |  | ||||||
| 	// retrieve a user-provided value for the "cache-dir" | 	// retrieve a user-provided value for the "cache-dir" | ||||||
| 	// override httpCacheDir and discoveryCacheDir if user-value is given. | 	// override httpCacheDir and discoveryCacheDir if user-value is given. | ||||||
| 	if f.CacheDir != nil { | 	if f.CacheDir != nil { | ||||||
| 		httpCacheDir = *f.CacheDir | 		cacheDir = *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") |  | ||||||
| 	} | 	} | ||||||
| 	} | 	httpCacheDir := filepath.Join(cacheDir, "http") | ||||||
|  | 	discoveryCacheDir := computeDiscoverCacheDir(filepath.Join(cacheDir, "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)) | ||||||
| } | } | ||||||
| @@ -262,7 +257,7 @@ func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet) { | |||||||
| 		flags.StringVar(f.KubeConfig, "kubeconfig", *f.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") | 		flags.StringVar(f.KubeConfig, "kubeconfig", *f.KubeConfig, "Path to the kubeconfig file to use for CLI requests.") | ||||||
| 	} | 	} | ||||||
| 	if f.CacheDir != nil { | 	if f.CacheDir != nil { | ||||||
| 		flags.StringVar(f.CacheDir, flagHTTPCacheDir, *f.CacheDir, "Default HTTP cache directory") | 		flags.StringVar(f.CacheDir, flagCacheDir, *f.CacheDir, "Default cache directory") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// add config options | 	// add config options | ||||||
|   | |||||||
| @@ -268,8 +268,6 @@ func (d *CachedDiscoveryClient) Invalidate() { | |||||||
| // CachedDiscoveryClient cache data. If httpCacheDir is empty, the restconfig's transport will not | // CachedDiscoveryClient cache data. If httpCacheDir is empty, the restconfig's transport will not | ||||||
| // be updated with a roundtripper that understands cache responses. | // 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. | // 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) { | func NewCachedDiscoveryClientForConfig(config *restclient.Config, discoveryCacheDir, httpCacheDir string, ttl time.Duration) (*CachedDiscoveryClient, error) { | ||||||
| 	if len(httpCacheDir) > 0 { | 	if len(httpCacheDir) > 0 { | ||||||
| 		// update the given restconfig with a custom roundtripper that | 		// update the given restconfig with a custom roundtripper that | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user