dotfiles/.config/bash/11-k8s.bash

102 lines
3.9 KiB
Bash

function kns() {
local cache="$XDG_CACHE_HOME/${FUNCNAME[0]}"
local conf="${KUBECONFIG:-"$HOME/.kube/config"}"
if [ "$1" == - ]; then
echo "not implemented" >&2
return 1
elif [ -n "$1" ]; then
if ! kubectl get namespace -oname | grep -q "^namespace/$1\$"; then
echo "error: no namespace exists with the name: \"$1\"" >&2
return 1
fi
kubectl config set-context --current --namespace "$1" > /dev/null
else
if [ "$conf" -nt "$cache" ]; then
set -- "$(kubectl config view --minify -ojsonpath='{..namespace}')"
else
cat "$cache"
return
fi
fi
tee "$cache" <<< "$1"
}
function kctx() {
local cache="$XDG_CACHE_HOME/${FUNCNAME[0]}"
local conf="${KUBECONFIG:-"$HOME/.kube/config"}"
if [ "$1" == - ]; then
echo "not implemented" >&2
return 1
elif [ -n "$1" ]; then
kubectl config use-context "$1" > /dev/null || return
else
if [ "$conf" -nt "$cache" ]; then
set -- "$(kubectl config current-context)"
else
cat "$cache"
return
fi
fi
tee "$cache" <<< "$1"
}
alias kga='kubectl get "$(kubectl api-resources --verbs=list --namespaced -oname | grep -v event | paste -sd,)"' \
kgaoname='kubectl get "$(kubectl api-resources --verbs=list --namespaced -oname | grep -v event | paste -sd,)" -oname' \
kgaowide='kubectl get "$(kubectl api-resources --verbs=list --namespaced -oname | grep -v event | paste -sd,)" -owide' \
kgaoyaml='kubectl get "$(kubectl api-resources --verbs=list --namespaced -oname | grep -v event | paste -sd,)" -oyaml'
. /usr/share/bash-completion/completions/kubectl
compalias k=kubectl \
ka='kubectl apply --recursive -f' \
kd='kubectl describe' \
kex='kubectl exec -i -t' \
kg='kubectl get' \
kgno='kubectl get node' \
klo='kubectl logs -f' \
kpf='kubectl port-forward' \
krm='kubectl delete' \
function _k8salias() {
local name short
for a in "$@"; do
name="${a#*=}" short="${a%%=*}"
compalias "kd${short}=kubectl describe $name" \
"kd${short}alloname=kubectl describe $name --all-namespaces -oname" \
"kd${short}allowide=kubectl describe $name --all-namespaces -owide" \
"kd${short}alloyaml=kubectl describe $name --all-namespaces -oyaml" \
"kd${short}oname=kubectl describe $name -oname" \
"kd${short}owide=kubectl describe $name -owide" \
"kd${short}oyaml=kubectl describe $name -oyaml" \
"kg${short}=kubectl get $name" \
"kg${short}alloname=kubectl get $name --all-namespaces -oname" \
"kg${short}allowide=kubectl get $name --all-namespaces -owide" \
"kg${short}alloyaml=kubectl get $name --all-namespaces -oyaml" \
"kg${short}oname=kubectl get $name -oname" \
"kg${short}owide=kubectl get $name -owide" \
"kg${short}oyaml=kubectl get $name -oyaml" \
"krm${short}=kubectl delete $name" \
"krm${short}alloname=kubectl delete $name --all-namespaces -oname" \
"krm${short}allowide=kubectl delete $name --all-namespaces -owide" \
"krm${short}alloyaml=kubectl delete $name --all-namespaces -oyaml" \
"krm${short}oname=kubectl delete $name -oname" \
"krm${short}owide=kubectl delete $name -owide" \
"krm${short}oyaml=kubectl delete $name -oyaml"
done
}
_k8salias cj=cronjob \
cm=configmap \
dep=deployment \
ing=ingress \
j=job \
ns=namespace \
po=pod \
pvc=persistentvolumeclaim \
pv=persistentvolume \
sec=secret \
sts=statefulset \
svc=service
unset -f _k8salias