do not lose the KUBECONFIG environment variable when clobbering it
mmerickel opened this issue · 4 comments
So I've always maintained a custom KUBECONFIG=$HOME/.kube/config:$HOME/.kube/foo:$HOME/.kube/bar
. This works great for kubeswitch on the first time I run it - and then it overwrites the env var with its custom tempfile. The second time I run kubeswitch the values are lost, and only the default $HOME/.kube/config
file is shown as options of context's to switch to. I would expect it to save the KUBECONFIG value somewhere and on subsequent invocations show me all of those options.
Alternatively I can define switch-config.yaml and maintain it separately but then of course I have it maintained in two locations when I'd prefer to use the "standard" support in kubectl in some situations.
PS thanks for this tool - it scratches an itch I've had for a really long time and you've done a much nicer job implementing it than anything I was thinking to do for myself!
Thanks for your suggestion!
The only thing I can currently think of is to introduce another env variable (say KUBECONFIG_PREVIOUS
set already in switch.sh script) that remembers the last KUBECONFIG env and includes these paths in a subsequent search with switch
.
A caveat I can already think of is that switch would need to remove duplicate paths in case KUBECONFIG_PREVIOUS
and KUBECONFIG
contain the same path - possibly using a set datastructure with the path as key.
Possibly there are more implications that I have not thought about.
I'll update here when I have time to give it a shot.
I know there's mention of kubeswitch using a cache, and I figured that things like this would be its purpose when initially reading the docs... like that it would take the KUBECONFIG env var and cache it before clobbering it. I think I misunderstood the purpose of that cache because otherwise it feels like it would belong there - but yes I would expect it should be a per-terminal cache.
Some sort of secondary env var makes sense to me and probably favorable versus a second per-terminal tempfile... Could also consider writing it into the existing per-terminal tempfile and parsing it back out but feels a bit hacky just to get it down to one tempfile.
I ended up using this trick to get around this limitation
kubectx(){
CURRENT_KUBECONFIG=$(echo $KUBECONFIG | sed -e 's/^.*\.switch_tmp\/config.[0-9]*\.tmp://g')
switch $@
set -a
export KUBECONFIG=$KUBECONFIG:$CURRENT_KUBECONFIG
set +a
}
kubens(){
CURRENT_KUBECONFIG=$(echo $KUBECONFIG | sed -e 's/^.*\.switch_tmp\/config.[0-9]*\.tmp://g')
switch . > /dev/null 2>1
switch ns
set -a
export KUBECONFIG=$KUBECONFIG:$CURRENT_KUBECONFIG
set +a
}
@sherifabdlnaby 's solution works flawlessly