Skip to content

Instantly share code, notes, and snippets.

@tcotav
Created March 31, 2017 16:08
Show Gist options
  • Save tcotav/6bdc2faae8373d183137c72ab4e5d126 to your computer and use it in GitHub Desktop.
Save tcotav/6bdc2faae8373d183137c72ab4e5d126 to your computer and use it in GitHub Desktop.
dot-bashrc set kubernetes kubeconfig on login
#!/bin/bash
# add this to users .bashrc file -- remove the #!/bin/bash
#
# script that prompts user on login to select a kubernetes cluster as their current
#
# - we set an alias for kubectl which is kubectl --kubeconfig=<some config>
# - we create a different kubeconfig file in .kube for each context
#
declare -a KUBE_CONFIG_FILES
# these are filenames in ~/.kube -- make 'em descriptive -- change to match your files in ~/.kube
KUBE_CONFIG_FILES[1]="dev-cluster-kubeconfig"
KUBE_CONFIG_FILES[2]="int-cluster-kubeconfig"
KUBE_CONFIG_FILES[3]="prod-cluster-kubeconfig"
# I put this in here to FORCE it
# kubectl without choosing will FAIL
rm ~/.kube/config
# lock 'em in until they choose or ctrl-C out (breaking kubectl for them)
while :
do
echo "Please Choose Kubernetes Cluster"
for i in "${!KUBE_CONFIG_FILES[@]}"; do
echo "${i}) ${KUBE_CONFIG_FILES[$i]}"
done
echo
echo "Please select [1 - ${#KUBE_CONFIG_FILES[@]}]"
echo
read clusterchoice
if [ "$clusterchoice" -ge 1 -a "$clusterchoice" -le ${#KUBE_CONFIG_FILES[@]} ];then
break
fi
done
alias kubectl="/usr/local/bin/kubectl --kubeconfig=${HOME}/.kube/${KUBE_CONFIG_FILES[${clusterchoice}]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment