function _kctxns() { local conf="${KUBECONFIG:-"$HOME/.kube/config"}" local tmp cache file="$1" func="$2"; shift 2 mapfile -t cache < "$file" if [ "$conf" -nt "$file" ]; then tmp="$(_"$func"_get)" if [ "$tmp" != "${cache[0]}" ]; then cache[1]="${cache[0]}" fi cache[0]="$tmp" echo -e "${cache[0]}\n${cache[1]}" > "$file" fi if [ -z "$1" ]; then echo "${cache[0]}" return fi if [ "$1" == - ]; then set -- "${cache[1]}" "${cache[0]}" else if [ "$1" == "${cache[0]}" ]; then return fi set -- "$1" "${cache[0]}" fi _"$func"_set "$1" || return echo -e "$1\n$2" > "$file" } function _kns_get() { kubectl config view --minify -ojsonpath='{..namespace}' } function _kns_set() { 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 echo "Switched to namespace \"$1\"." } function kns() { local dir="$XDG_CACHE_HOME/kns" mkdir -p "$dir" > /dev/null _kctxns "$dir/$(kctx)" kns "$@" } function _kctx_get() { kubectl config current-context ; } function _kctx_set() { kubectl config use-context "$1" ; } # shellcheck disable=SC2120 function kctx() { _kctxns "$XDG_CACHE_HOME/kctx" kctx "$@" ; } 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 n s a for a in '' "${@/=/= }"; do s="${a%%=*}" n="${a#*=}" compalias "kd${s}=kubectl describe$n" \ "kg${s}=kubectl get$n" \ "kg${s}alloname=kubectl get --all-namespaces -oname$n" \ "kg${s}allowide=kubectl get --all-namespaces -owide$n" \ "kg${s}alloyaml=kubectl get --all-namespaces -oyaml$n" \ "kg${s}oname=kubectl get -oname$n" \ "kg${s}oname=kubectl get -oname$n" \ "kg${s}owide=kubectl get -owide$n" \ "kg${s}owide=kubectl get -owide$n" \ "kg${s}oyaml=kubectl get -oyaml$n" \ "kg${s}oyaml=kubectl get -oyaml$n" \ "krm${s}=kubectl delete$n" 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