Remove defaultCacheDir and use func instead

This commit is contained in:
Arda Güçlü 2022-05-30 09:31:35 +03:00
parent 937c7677a2
commit 37c6a220dc

View File

@ -57,10 +57,6 @@ const (
flagCacheDir = "cache-dir"
)
var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "cache")
)
// 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
// as per the golang type overlapping.
@ -275,16 +271,13 @@ func (f *ConfigFlags) toDiscoveryClient() (discovery.CachedDiscoveryInterface, e
config.Burst = f.discoveryBurst
config.QPS = f.discoveryQPS
cacheDir := defaultCacheDir
if kcd := os.Getenv("KUBECACHEDIR"); kcd != "" {
cacheDir = kcd
}
cacheDir := getDefaultCacheDir()
// retrieve a user-provided value for the "cache-dir"
// override httpCacheDir and discoveryCacheDir if user-value is given.
// user-provided value has higher precedence than default
// and KUBECACHEDIR environment variable.
if f.CacheDir != nil && *f.CacheDir != "" && *f.CacheDir != defaultCacheDir {
if f.CacheDir != nil && *f.CacheDir != "" && *f.CacheDir != getDefaultCacheDir() {
cacheDir = *f.CacheDir
}
@ -294,6 +287,17 @@ func (f *ConfigFlags) toDiscoveryClient() (discovery.CachedDiscoveryInterface, e
return diskcached.NewCachedDiscoveryClientForConfig(config, discoveryCacheDir, httpCacheDir, time.Duration(6*time.Hour))
}
// getDefaultCacheDir returns default caching directory path.
// it first looks at KUBECACHEDIR env var if it is set, otherwise
// it returns standard kube cache dir.
func getDefaultCacheDir() string {
if kcd := os.Getenv("KUBECACHEDIR"); kcd != "" {
return kcd
}
return filepath.Join(homedir.HomeDir(), ".kube", "cache")
}
// ToRESTMapper returns a mapper.
func (f *ConfigFlags) ToRESTMapper() (meta.RESTMapper, error) {
if f.usePersistentConfig {
@ -426,7 +430,7 @@ func NewConfigFlags(usePersistentConfig bool) *ConfigFlags {
Timeout: utilpointer.String("0"),
KubeConfig: utilpointer.String(""),
CacheDir: utilpointer.String(defaultCacheDir),
CacheDir: utilpointer.String(getDefaultCacheDir()),
ClusterName: utilpointer.String(""),
AuthInfoName: utilpointer.String(""),
Context: utilpointer.String(""),