diff --git a/kubectx b/kubectx index c80ebf8..4348b2b 100755 --- a/kubectx +++ b/kubectx @@ -103,28 +103,39 @@ swap_context() { } user_of_context() { + # TODO(ahmetb) no longer used, consider deleting kubectl config view \ -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.user}" } cluster_of_context() { + # TODO(ahmetb) no longer used, consider deleting kubectl config view \ -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}" } +context_exists() { + grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name) +} + rename_context() { local old_name="${1}" local new_name="${2}" + # TODO(ahmetb) old_user and old_cluster are no longer used, clean up local old_user old_cluster old_user="$(user_of_context "${old_name}")" old_cluster="$(cluster_of_context "${old_name}")" - if [[ -z "$old_user" || -z "$old_cluster" ]]; then echo "error: Cannot retrieve context ${old_name}." >&2 exit 1 fi + if context_exists "${new_name}"; then + echo "Context \"${new_name}\" exists, deleting." >&2 + kubectl config delete-context "${new_name}" 1>/dev/null 2>&1 + fi + kubectl config rename-context "${old_name}" "${new_name}" }