Skip to content

Instantly share code, notes, and snippets.

@caruccio
Created May 10, 2024 14:27
Show Gist options
  • Save caruccio/5c25ef92d7ad0f2e8d2e1b39c0271110 to your computer and use it in GitHub Desktop.
Save caruccio/5c25ef92d7ad0f2e8d2e1b39c0271110 to your computer and use it in GitHub Desktop.
Use local kubeconfig on dir change
#!/bin/bash
COLOR_LIGHT_GREEN="$(tput setaf 10)"
COLOR_RESET="$(tput sgr0)"
function kubecfg()
{
if [ $# -eq 0 ]; then
echo $KUBECONFIG
return
fi
if [ "$1" == -u ]; then
if [ -v KUBECONFIG ]; then
echo Unsetting KUBECONFIG=$KUBECONFIG
unset KUBECONFIG
fi;
return
fi
export KUBECONFIG="$(realpath $1)"
if ! [ -e "$1" ]; then
echo Creating kubeconfig: $1
touch $1
else
chmod 700 $KUBECONFIG
fi
echo -e "${COLOR_LIGHT_GREEN}Using kubeconfig: ${KUBECONFIG}${COLOR_RESET}"
}
function cd()
{
command cd "$@"
[ -e ./.kubeconfig ] && kubecfg ./.kubeconfig && return
[ -e ./kubeconfig ] && kubecfg ./kubeconfig && return
[ -e ./.kube/config ] && kubecfg ./.kube/config && return
kubecfg $HOME/.kube/config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment