Skip to content

Instantly share code, notes, and snippets.

@sqlheisenberg
Forked from rothgar/k8s.zsh
Created September 28, 2024 10:04
Show Gist options
  • Save sqlheisenberg/c2cf4551a6e11ec64bb539b9caa1fbc0 to your computer and use it in GitHub Desktop.
Save sqlheisenberg/c2cf4551a6e11ec64bb539b9caa1fbc0 to your computer and use it in GitHub Desktop.
Useful kubectl shell functions
ealias kg='k get '
ealias kgp='k get po '
ealias kgn='k get no '
ealias kgd='k get deploy '
ealias krmp='k delete po '
ealias kdp='k describe po '
ealias uek='unset KUBECONFIG'
ealias uekns='unset KUBE_NAMESPACE'
# export kubeconfig
ek() {
if [ -n "$1" ]; then
CONFIG=$(rg --max-depth 3 -l '^kind: Config$' $HOME/.kube/ 2>/dev/null \
| grep $1)
else
CONFIG=$(rg --max-depth 3 -l '^kind: Config$' $HOME/.kube/ $PWD 2>/dev/null | fzf --multi | tr '\n' ':')
fi
# echo file and remove trailing :
echo ${CONFIG%:*}
export KUBECONFIG=${CONFIG%:*}
PROFILE=$(yq '.users[0].user.exec.env[0].value' $KUBECONFIG)
REGION=$(yq '.users[0].user.exec.args' $KUBECONFIG | grep -A1 region | tail -1 | awk '{print $2}')
awsp $PROFILE $REGION
}
# delete kubeconfig files that don't connect to a kubernetes cluster
clean-k() {
export FILES=($(rg --max-depth 3 -l '^kind: Config$' $HOME/.kube/ $PWD ))
for FILE in ${FILES[@]}; do
echo "Checking $FILE"
if [ $(timeout 3 kubectl cluster-info --kubeconfig=${FILE} 2> /dev/null ) ]; then
echo "Removing $FILE"
rm -f ${FILE}
else
echo "Skipping $FILE"
fi
done
}
# main k function
fn k() {
if [ -n "$KUBE_NAMESPACE" ]; then
kubectl --namespace "$KUBE_NAMESPACE" $@
else
kubectl $@
fi
}
# helper for setting a namespace
# List namespaces, preview the pods within, and save as variable
function ekns() {
namespaces=$(kubectl get ns -o=custom-columns=:.metadata.name)
export KUBE_NAMESPACE=$(echo $namespaces | fzf --select-1 --preview "kubectl --namespace {} get pods")
echo "Set namespace to $KUBE_NAMESPACE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment