🔧 use rename-context in place of set-context

This commit is contained in:
Bhargav Nookala 2017-11-02 16:48:58 -07:00
parent ed2afa7572
commit 9ed6690978

14
kubectx
View File

@ -36,9 +36,9 @@ usage() {
cat <<"EOF"
USAGE:
kubectx : list the contexts
kubectx <NAME> : switch to context
kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : create alias for context
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx -h,--help : show this message
EOF
exit 1
@ -107,7 +107,7 @@ cluster_of_context() {
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}"
}
alias_context() {
rename_context() {
local old_name="${1}"
local new_name="${2}"
@ -119,11 +119,7 @@ alias_context() {
exit 1
fi
kubectl config set-context "${new_name}" \
--cluster="${old_cluster}" \
--user="${old_user}" \
echo "Aliased \"${old_name}\" as \"${new_name}\"." >&2
kubectl config rename-context "${old_name}" "${new_name}"
}
main() {
@ -141,7 +137,7 @@ main() {
echo "error: unrecognized flag \"${1}\"" >&2
usage
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_context "${1}"
fi