Skip to content

Instantly share code, notes, and snippets.

@NovoG93
Last active May 17, 2024 13:17
Show Gist options
  • Save NovoG93/123df246a24c13c3bba8fbacd5dff172 to your computer and use it in GitHub Desktop.
Save NovoG93/123df246a24c13c3bba8fbacd5dff172 to your computer and use it in GitHub Desktop.
k8s.bashrc
if [ -f /usr/bin/kubectl ]; then
source <(kubectl completion bash)
fi
if [ -f /usr/bin/kubeadm ]; then
source <(kubeadm completion bash)
fi
if [ -f /usr/bin/helm ]; then
source <(helm completion bash)
fi
get-ns() {
# Get current context
NS=$(cat ~/.kube/config 2>/dev/null| grep -o 'namespace: [^/]*' | cut -d' ' -f2)
if [ -n "$NS" ]; then
echo "[${NS}]"
fi
}
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
NORMAL="\[\033[00m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\e[1;33m\]"
GREEN="\[\e[1;32m\]"
PS1="${debian_chroot:+($debian_chroot)}${GREEN}\u@\h${NORMAL}${YELLOW}$(get-ns)${NORMAL}:${BLUE}\w${NORMAL}$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u@\h$(get-ns):\w$ "
fi
k-change-ns() {
kubectl config set-context --current --namespace="$1"
}
# Autocompletion for k-change-ns
_k_change_ns() {
local cur=${COMP_WORDS[COMP_CWORD]}
local namespaces=$(kubectl get namespaces --no-headers -o custom-columns=:metadata.name)
COMPREPLY=($(compgen -W "${namespaces}" -- ${cur}))
}
complete -F _k_change_ns k-change-ns
alias k='kubectl'
complete -F __start_kubectl k
export oy="-oyaml"
export doy="--dry-run=client $oy"
export fg="--force --grace-period=0"
export v="vim"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment