Skip to content

Instantly share code, notes, and snippets.

@beli-sk
Last active February 25, 2019 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beli-sk/ae0f05e5f1a62c163f6b0d393b4958f6 to your computer and use it in GitHub Desktop.
Save beli-sk/ae0f05e5f1a62c163f6b0d393b4958f6 to your computer and use it in GitHub Desktop.
Kubernetes: kubectl temporary environment per session
#!/bin/bash
# to be sourced by interactive shell or added to .bashrc
#
# kubectl temporary environment per session
# - any `kubectl config` changes (like use-context) go to temporary file
#
KUBETMPDIR=`mktemp -d`
[[ "$1" == "--merged" || "$1" == "-m" ]] && touch "${KUBETMPDIR}/kubeconfig" || cp "${HOME}/.kube/config" "${KUBETMPDIR}/kubeconfig"
eval "function kubetmp_cleanup {
rm -f ${KUBETMPDIR}/kubeconfig
rmdir ${KUBETMPDIR}
}"
export KUBECONFIG="${KUBETMPDIR}/kubeconfig:${HOME}/.kube/config"
unset KUBETMPDIR
trap kubetmp_cleanup EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment