Skip to content

Instantly share code, notes, and snippets.

@aita
Created June 10, 2021 04:40
Show Gist options
  • Save aita/67409f0bf15770addbeef874850506f8 to your computer and use it in GitHub Desktop.
Save aita/67409f0bf15770addbeef874850506f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
kubenv () {
if [[ -z "$1" ]]; then
cat <<EOF >/dev/stderr
Usage: kubenv CONTEXT_NAME
EOF
return 1
fi
local -r ctx="$1"
local -r ctxfile=$(mktemp)
cat <<EOF >$ctxfile
apiVersion: v1
kind: Config
current-context: ${ctx}
EOF
if [[ ${KUBECONFIG_SAVED+x} ]]; then
export KUBECONFIG_SAVED=$KUBECONFIG
fi
KUBECONFIG=$KUBECONFIG_SAVED
if [[ -z "$KUBECONFIG" ]]; then
KUBECONFIG="${XDG_CACHE_HOME:-$HOME/.kube}/config"
fi
KUBECONFIG="$ctxfile:$KUBECONFIG"
export KUBECONFIG
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment