kubernetes_context.sh
1 #!/usr/bin/env bash 2 # setting the locale, some users have issues with different locales, this forces the correct one 3 export LC_ALL=en_US.UTF-8 4 5 hide_arn_from_cluster=$1 6 extract_account=$2 7 hide_user=$3 8 just_current_context=$4 9 label=$5 10 11 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 12 source $current_dir/utils.sh 13 14 current_context=$(kubectl config view --minify --output 'jsonpath={.current-context}'; echo) 15 current_user=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.name=="'$current_context'")].context.user}'; echo) 16 current_cluster=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.name=="'$current_context'")].context.cluster}'; echo) 17 current_namespace=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.name=="'$current_context'")].context.namespace}'; echo) 18 19 current_account_id="" 20 if [[ "$current_cluster" =~ ^arn:aws:eks:[a-z0-9\-]*:[0-9]*:cluster/[a-z0-9\-]*$ ]]; then 21 if [ "$extract_account" = "true" ]; then 22 current_account_id=$(echo "$current_cluster" | cut -d':' -f5) 23 fi 24 if [ "$hide_arn_from_cluster" = "true" ]; then 25 current_cluster=${current_cluster##*/} 26 fi 27 fi 28 29 if [ "$hide_user" = "true" ]; then 30 current_user="" 31 fi 32 33 main() 34 { 35 # storing the refresh rate in the variable RATE, default is 5 36 RATE=$(get_tmux_option "@dracula-refresh-rate" 5) 37 OUTPUT_STRING="" 38 39 if [ "$just_current_context" = "true" ] 40 then 41 echo "$current_context" 42 else 43 getFullMessage 44 fi 45 46 sleep $RATE 47 } 48 49 getFullMessage() 50 { 51 if [ ! -z "$current_account_id" ] 52 then 53 OUTPUT_STRING="${current_account_id}/" 54 fi 55 56 if [ ! -z "$current_user" ] 57 then 58 OUTPUT_STRING="${current_user}@" 59 fi 60 61 if [ ! -z "$current_cluster" ] 62 then 63 OUTPUT_STRING="${OUTPUT_STRING}${current_cluster}" 64 fi 65 66 if [ ! -z "$current_namespace" ] 67 then 68 OUTPUT_STRING="${OUTPUT_STRING}:${current_namespace}" 69 fi 70 71 if [ "$OUTPUT_STRING" = "" ] 72 then 73 OUTPUT_STRING="kubeconfig not valid" 74 fi 75 76 if [ "$label" = "" ] 77 then 78 echo "${OUTPUT_STRING}" 79 else 80 echo "${label} ${OUTPUT_STRING}" 81 fi 82 } 83 84 # run the main driver 85 main