From 231f17096851fe1ba9f6c54a39ca74907a83abf2 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Tue, 28 Mar 2017 18:03:26 -0700 Subject: [PATCH] Add support to create aliases Signed-off-by: Ahmet Alp Balkan --- kubectx | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/kubectx b/kubectx index 06572b2..1f81616 100755 --- a/kubectx +++ b/kubectx @@ -22,10 +22,11 @@ KUBECTX="${HOME}/.kube/kubectx" function usage { cat <<"EOF" USAGE: - kubectx : list the contexts - kubectx : switch to context - kubectx - : switch to the previous context - kubectx -h|--help : show this message + kubectx : list the contexts + kubectx : switch to context + kubectx - : switch to the previous context + kubectx = : create alias for context + kubectx -h|--help : show this message EOF } @@ -34,7 +35,8 @@ function current_context { } function get_contexts { - kubectl config get-contexts | awk '{print $2}' | tail -n +2 + kubectl config view \ + -o=jsonpath='{range .contexts[*].name}{@}{"\n"}{end}' } function list_contexts { @@ -91,6 +93,36 @@ function swap_context { set_context "$ctx" } +function user_of_context { + kubectl config view \ + -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.user}" +} + +function cluster_of_context { + kubectl config view \ + -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}" +} + +function alias_context { + local old_name="$1" + local new_name="$2" + + local old_user="$(user_of_context $old_name)" + local old_cluster="$(cluster_of_context $old_name)" + + if [[ -z "$old_user" || -z "$old_cluster" ]]; then + echo "Cannot retrieve context ${old_name}". + exit 1 + fi + + kubectl config set-context "${new_name}" \ + --cluster="${old_cluster}" \ + --user="${old_user}" \ + + echo "Aliased ${old_name} as ${new_name}." +} + + if [[ "$#" -eq 0 ]]; then list_contexts elif [[ "$#" -eq 1 ]]; then @@ -98,6 +130,8 @@ elif [[ "$#" -eq 1 ]]; then usage elif [[ "$1" = "-" ]]; then swap_context + elif [[ "$1" =~ (.+)=(.+) ]]; then + alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" else set_context "$1" fi