-
Notifications
You must be signed in to change notification settings - Fork 39.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce KUBECACHEDIR environment variable to override default discovery cache dir #109479
Introduce KUBECACHEDIR environment variable to override default discovery cache dir #109479
Conversation
…very cache dir This PR introduces new environment variable, namely `KUBECACHEDIR`. `KUBECACHEDIR` is used to override default discovery cache directory for all commands(whose default value is $HOME/.kube/cache). `--cache-dir` flag per command has higher precedence than `KUBECACHEDIR` and default directory path.
@ardaguclu: This issue is currently awaiting triage. If a SIG or subproject determines this is a relevant issue, they will accept it by applying the The Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/test pull-kubernetes-unit |
@@ -420,7 +426,7 @@ func NewConfigFlags(usePersistentConfig bool) *ConfigFlags { | |||
Timeout: utilpointer.String("0"), | |||
KubeConfig: utilpointer.String(""), | |||
|
|||
CacheDir: utilpointer.String(defaultCacheDir), | |||
CacheDir: utilpointer.String(""), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This potentially affects someone who is using NewConfigFlags()
. Previously, it would return a ConfigFlags
with CacheDir
already populated with the default cache directory. Now it will return an empty CacheDir
. This could be a breaking change for someone if they have built a client that uses this to know where the cache directory will be located.
There really isn't any way for a caller to know what the cache directory is going to be otherwise.
I wonder if it would be better to just change how defaultCacheDir is determined and leave everything else as-is?
So instead of this:
var (
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "cache")
)
Can you set defaultCacheDir conditionally if the environment variable is set, and using the current default value otherwise? Maybe using an init()
func or something to do that.
Then toDiscoveryClient()
and NewConfigFlags()
don't need to change at all and everything should work exactly the same as it has been, just with the environment variable option.
Thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is really a good point @brianpursley and indeed it breaks the backward compatibility.
But for your suggestion, after setting KUBECACHEDIR
in init function, there might be a risk to not using latest KUBECACHEDIR
in case of updates?
Maybe we can leave defaultCacheDir
as is and we can just use cache-dir
if it is not equal to defaultCacheDir.
I mean something like that;
cacheDir := defaultCacheDir
if kcd := os.Getenv("KUBECACHEDIR"); kcd != "" {
cacheDir = kcd
}
// 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 {
cacheDir = *f.CacheDir
}
Only problem in here is user can not use defaultCacheDir by passing --cache-dir
if KUBECACHEDIR
is set but I think, this is too edge case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is OK to change, it just needs to be set in all the same places it is currently.
What about something like this?...
Remove the defaultCacheDir
variable and add a function that can return the default cache dir, something like this:
func getDefaultCacheDir() string {
if kcd := os.Getenv("KUBECACHEDIR"); kcd != "" {
return kcd
}
return filepath.Join(homedir.HomeDir(), ".kube", "cache")
}
Then you can replace defaultCacheDir
with getDefaultCacheDir()
which will follow the rules of looking at the KUBECACHEDIR environment variable first, then constructing it from the homedir if the environment variable is not set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brianpursley thanks your the suggestion. I agree, this is better solution and changed this PR that way.
/test pull-kubernetes-unit |
1 similar comment
/test pull-kubernetes-unit |
/priority backlog |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ardaguclu, brianpursley The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind feature
What this PR does / why we need it:
This PR introduces new environment variable, namely
KUBECACHEDIR
.KUBECACHEDIR
is used to override default discovery cache directory for all commands(whose default value
is $HOME/.kube/cache).
--cache-dir
flag per command has higher precedence thanKUBECACHEDIR
and defaultdirectory path.
Which issue(s) this PR fixes:
Fixes kubernetes/kubectl#1184
Does this PR introduce a user-facing change?