Skip to content

Instantly share code, notes, and snippets.

@sci-phi
Forked from ryanschwartz/k8 is great.md
Created February 22, 2017 22:22
Show Gist options
  • Save sci-phi/75c8eb75c9919ebabe5f6ecc8ab1ae74 to your computer and use it in GitHub Desktop.
Save sci-phi/75c8eb75c9919ebabe5f6ecc8ab1ae74 to your computer and use it in GitHub Desktop.
k8.bash

kubeenv for lazy typers...

Usage

  1. Copy the below into your .bash_profile or similar
  2. k8 <your-context>, where your-context is the shortcut name you'd like to use
  3. gcloud container clusters get-credentials <clustername>
  4. Repeat step 3 for each of the k8s contexts you want to have available
  5. To use, run k8 <your-context> to change contexts.

k8 has tab-completion for configured contexts, use removek8 <your-envname> to delete a context.

(see kubeenv readme for zsh usage)

#<=====BEGIN Kubeenv=====>
function _kubeenv()
{
local cur opts
cur="${COMP_WORDS[COMP_CWORD]}"
opts="$(ls "$HOME/.kubeenv")"
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
}
complete -o default -F _kubeenv k8 removek8
function k8() {
if [ x"$#" != x"1" ]; then
echo "Usage: k8 NAME" >&2
return 2
fi
if [ x"$KUBEENV" != x"" ]; then
nok8
fi
KUBEENV="$1"
export KUBEENV
mkdir -p "$HOME/.kubeenv"
touch "$HOME/.kubeenv/$KUBEENV"
KUBECONFIG="$HOME/.kubeenv/$KUBEENV"
export KUBECONFIG
if [ x"$KUBEENV_DISABLE_PROMPT" != x"1" ]; then
KUBEENV_PRE_PS1="$PS1"
PS1="($1) $PS1"
fi
}
function nok8() {
unset KUBECONFIG
unset KUBEENV
if [ x"$KUBEENV_DISABLE_PROMPT" != x"1" ]; then
PS1="$KUBEENV_PRE_PS1"
unset KUBEENV_PRE_PS1
fi
}
function removek8() {
if [ x"$#" != x"1" ]; then
echo "Usage: removek8 NAME" >&2
return 2
fi
if [ x"$KUBEENV" = x"$1" ]; then
echo "Can't delete active kubeenv. Run kubeenv_disable before" >&2
return 2
fi
if [ ! -e "$HOME/.kubeenv/$1" ]; then
echo "kubeenv $1 doesn't exist" >&2
return 2
fi
rm -f "$HOME/.kubeenv/$1"
}
#<======END Kubeenv======>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment