Last active
July 28, 2021 14:56
-
-
Save beli-sk/87f21ac16b41a7c89001de0c3b30b1ac to your computer and use it in GitHub Desktop.
Bash: set default namespace for kubectl command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### kubens - set default namespace for kubectl command | |
function kubens { | |
if [[ -n "$1" ]] ; then | |
alias kubectl="kubectl -n $1" | |
export HELM_NAMESPACE="$1" | |
PS1=$( echo "$PS1" | sed -r 's/(.*)\(ns:[^)]*\)(.*)/\1\2/' ) | |
PS1="(ns:$1)${PS1}" | |
else | |
unalias kubectl | |
unset HELM_NAMESPACE | |
PS1=$( echo "$PS1" | sed -r 's/(.*)\(ns:[^)]*\)(.*)/\1\2/' ) | |
fi | |
} | |
function _kubens_completions() { | |
[[ "${#COMP_WORDS[@]}" != "2" ]] && return | |
COMPREPLY=($(compgen -W "`kubectl get ns -o jsonpath='{.items[*].metadata.name}'`" -- "${COMP_WORDS[1]}")) | |
} | |
complete -F _kubens_completions kubens | |
### end kubens |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment