Skip to content

Instantly share code, notes, and snippets.

@leosunmo
Created February 14, 2018 01:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leosunmo/e680910d1a4783f2ee8453101a9e33a8 to your computer and use it in GitHub Desktop.
Save leosunmo/e680910d1a4783f2ee8453101a9e33a8 to your computer and use it in GitHub Desktop.
Kubernetes kubectl bash function using heptio-authenticator-aws + aws-vault to authenticate
# aws-vault function
function av() {
if [[ $# -ge 2 ]]; then
case $1 in
ap-admin)
aws-vault exec --session-ttl=4h ap-admin -- ${@:2}
;;
us-admin)
aws-vault exec --session-ttl=4h us-admin -- ${@:2}
;;
*)
# We exit if you run "av somecommand" to avoid ending up in an AWS authenticated subshell
echo "Unknown profile $1. Exiting."
;;
esac
else
# We exit here as well for the same reason.
echo "No command detected. Exiting."
fi
}
# kubectl funtions
# one function per cluster/region
# we use the first argument as namespace
# kubectl function for our dev environment
function kdev() {
case $1 in
system)
kubectl --context=kdev-engineer --token "$(av ap-admin heptio-authenticator-aws token -i cluster.ap-southeast-2.example.org)" --namespace=kube-system ${@:2}
;;
red)
kubectl --context=kdev-engineer --token "$(av ap-admin heptio-authenticator-aws token -i cluster.ap-southeast-2.example.org)" --namespace=red ${@:2}
;;
blue)
kubectl --context=kdev-engineer --token "$(av ap-admin heptio-authenticator-aws token -i cluster.ap-southeast-2.example.org)" --namespace=blue ${@:2}
;;
*)
kubectl --context=kdev-engineer --token "$(av ap-admin heptio-authenticator-aws token -i cluster.ap-southeast-2.example.org)" $@
esac
}
# kubectl funtion for our US prod environment
function kus() {
case $1 in
system)
kubectl --context=kus-engineer --token "$(av us-admin heptio-authenticator-aws token -i cluster.us-west-2.example.org)" --namespace=kube-system ${@:2}
;;
red)
kubectl --context=kus-engineer --token "$(av us-admin heptio-authenticator-aws token -i cluster.us-west-2.example.org)" --namespace=red ${@:2}
;;
blue)
kubectl --context=kus-engineer --token "$(av us-admin heptio-authenticator-aws token -i cluster.us-west-2.example.org)" --namespace=blue ${@:2}
;;
*)
kubectl --context=kus-engineer --token "$(av us-admin heptio-authenticator-aws token -i cluster.us-west-2.example.org)" $@
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment