Skip to content

Instantly share code, notes, and snippets.

@ryanschwartz
Last active May 1, 2018 19:03
Show Gist options
  • Save ryanschwartz/e654f49c49154247255be1c98be1acc2 to your computer and use it in GitHub Desktop.
Save ryanschwartz/e654f49c49154247255be1c98be1acc2 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)

#===================Aliases=====================
# Docker
alias bs='eval $(boot2docker shellinit)'
alias d='docker'
alias di='docker images'
alias dps='docker ps'
alias dpa='docker ps -a'
alias dcu="docker ps -a|grep Exit| awk '{print \$1}'|xargs docker rm && docker rmi \$(docker images -f "dangling=true" -q)"
alias dr='docker run'
#alias dockermem="for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $2\" \"$NF\" \"}' && cat /sys/fs/cgroup/memory/$line*/memory.usage_in_bytes | awk '{print $1/1024/1024\"MB\"}'; done | column -t"
# Kubernetes
alias k='kubectl'
alias ks='kubectl --namespace=kube-system'
alias kgp='k get po'
alias ksgp='ks get po'
alias kg='k get'
alias ksg='ks get'
alias kgr='k get po -o=custom-columns=NAME:.metadata.name,LIMITS:.spec.containers[0].resources'
alias kl='k logs'
alias klf='k logs -f'
alias ksl='ks logs'
alias kslf='ks logs -f'
alias kd='k describe'
alias ksd='ks describe'
alias krm='k delete'
alias ksrm='ks delete'
alias ke='k exec -it'
alias ktp='k top pod'
alias ktn='k top node'
alias kcon="echo \"Current context: \`kubectl config current-context\`\";echo "==============================================================";k config view -o json | jq .contexts | jq .\[\].name | sed -e 's/\"//g'"
alias kuse='k config use-context'
alias list-kube-downloads="gsutil ls -R gs://kubernetes-release/release/v1.3.5 | sed 's|gs://kubernetes-release|https://storage.googleapis.com/kubernetes-release|; /^.*:$/d; /^$/d'"
alias podme='k run `whoami` -i --tty --image-pull-policy=Always --rm --image alpine --restart Never --command -- ash'
# Google Compute
alias gcloud='gcloud'
alias gssh='gcloud compute ssh'
alias gcil='gcloud compute instances list'
alias gdl='gcloud compute disks list'
alias gcon="echo \"Current project: \`gcloud config configurations list | grep True | grep -v grep | awk '{print \$1}'\`\";echo \"==============================================================\";echo \"Available projects:\";gcloud config configurations list | awk '{print \$1}' | grep -v NAME"
alias guse='gcloud config configurations activate'
#===================End Aliases=====================
#===================Functions=====================
# encode to base64
function en() {
echo -n $1 | base64 -b0
}
# decode base64
function de() {
echo $1 | base64 -D
}
#<=====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 rmk8
function k8() {
OLDK8ENV=$KUBEENV
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"
if [ -f $HOME/.kubeenv/$KUBEENV ]; then
KUBECONFIG="$HOME/.kubeenv/$KUBEENV"
else
read -p "Do you want to create the kube env $KUBEENV?? [y/N] " yup
if [ $yup = "yes" -o $yup = "y" ]; then
touch "$HOME/.kubeenv/$KUBEENV"
echo
echo "Don't forget to pull the configuration..."
echo "gcloud container clusters get-credentials <clustername>"
echo
else
echo
echo "Ok, bailing."
echo
KUBEENV=$OLDK8ENV
fi
fi
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 rmk8() {
if [ x"$#" != x"1" ]; then
echo "Usage: rmk8 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"
}
function make-completion-wrapper () {
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name {
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )
"$comp_function_name"
return 0
}"
eval "$function"
echo $function_name
echo "$function"
}
function _k {
((COMP_CWORD+=0))
COMP_WORDS=( kubectl ${COMP_WORDS[@]:1} )
__start_kubectl
return 0
}
#complete -o filenames -F _k k
#<======END Kubeenv======>#===================End Functions=====================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment