expose more P9K_KUBECONTEXT variables for gke and eks contexts

This commit is contained in:
romkatv 2019-07-30 09:02:46 +02:00
parent f337616738
commit 32421d296c

View file

@ -2883,8 +2883,8 @@ prompt_dir_writable() {
################################################################ ################################################################
# Kubernetes Current Context/Namespace # Kubernetes Current Context/Namespace
prompt_kubecontext() { prompt_kubecontext() {
unset P9K_KUBECONTEXT_{NAME,CLUSTER,NAMESPACE}
(( $+commands[kubectl] )) || return (( $+commands[kubectl] )) || return
local cfg local cfg
local -a key local -a key
for cfg in ${(s.:.)${KUBECONFIG:-$HOME/.kube/config}}; do for cfg in ${(s.:.)${KUBECONFIG:-$HOME/.kube/config}}; do
@ -2894,16 +2894,16 @@ prompt_kubecontext() {
done done
if ! _p9k_cache_get $0 "${key[@]}"; then if ! _p9k_cache_get $0 "${key[@]}"; then
local context cluster namespace local name namespace cluster cloud_name cloud_account cloud_zone cloud_cluster text state
() { () {
local cfg && cfg=(${(f)"$(kubectl config view -o=yaml 2>/dev/null)"}) || return local cfg && cfg=(${(f)"$(kubectl config view -o=yaml 2>/dev/null)"}) || return
local ctx=(${(@M)cfg:#current-context: [^\"\'\|\>]*}) local ctx=(${(@M)cfg:#current-context: [^\"\'\|\>]*})
(( $#ctx == 1 )) || return (( $#ctx == 1 )) || return
context=${ctx[1]#current-context: } name=${ctx[1]#current-context: }
local -i pos=${cfg[(i)contexts:]} local -i pos=${cfg[(i)contexts:]}
(( pos <= $#cfg )) || return (( pos <= $#cfg )) || return
shift $pos cfg shift $pos cfg
pos=${cfg[(i) name: $context]} pos=${cfg[(i) name: $name]}
(( pos <= $#cfg )) || return (( pos <= $#cfg )) || return
(( --pos )) (( --pos ))
for ((; pos > 0; --pos)); do for ((; pos > 0; --pos)); do
@ -2917,46 +2917,53 @@ prompt_kubecontext() {
fi fi
done done
} }
local text=$context if [[ -n $name ]]; then
local ns=${namespace:-default} # gke_my-account_us-east1-a_cluster-01
if [[ $context != $ns && ($ns != default || $_POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE == 1) ]]; then if [[ $cluster == (#b)gke_(?*)_(asia|australia|europe|northamerica|southamerica|us)-([a-z]##<->-[a-z])_(?*) ]]; then
text+="/$ns" cloud_name=gke
fi cloud_account=$match[1]
local shorten cloud_zone=$match[2]-$match[3]
for shorten in $_POWERLEVEL9K_KUBECONTEXT_SHORTEN; do cloud_cluster=$match[4]
case $shorten in if (( $+_POWERLEVEL9K_KUBECONTEXT_SHORTEN[(I)gke] )); then
gke) text=$cloud_cluster
# gke_projectname_availability-zone_cluster-01 => cluster-01 fi
if [[ $cluster == (#b)gke_[^_]#_[^_]#_(?*) ]]; then # arn:aws:eks:us-east-1:123456789012:cluster/cluster-01
text=$match[1] elif [[ $cluster == (#b)arn:aws:eks:([[:alnum:]-]##):([[:digit:]]##):cluster/(?*) ]]; then
break cloud_name=eks
fi cloud_zone=$match[1]
;; cloud_account=$match[2]
eks) cloud_cluster=$match[3]
# arn:aws:eks:us-east-1:123456789012:cluster/eks-infra if (( $+_POWERLEVEL9K_KUBECONTEXT_SHORTEN[(I)eks] )); then
if [[ $cluster == (#b)arn:aws:eks:[[:alnum:]-]##:[[:digit:]]##:cluster/(?*) ]]; then text=$cloud_cluster
text=$match[1] fi
break fi
fi if [[ -z $text ]]; then
esac text=$name
done local ns=${namespace:-default}
local suf if [[ $_POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE == 1 || $ns != (default|$name) ]]; then
if [[ -n $text ]]; then text+="/$ns"
fi
fi
local pat class local pat class
for pat class in "${_POWERLEVEL9K_KUBECONTEXT_CLASSES[@]}"; do for pat class in "${_POWERLEVEL9K_KUBECONTEXT_CLASSES[@]}"; do
if [[ $text == ${~pat} ]]; then if [[ $text == ${~pat} ]]; then
[[ -n $class ]] && suf=_${(U)class} [[ -n $class ]] && state=_${(U)class}
break break
fi fi
done done
fi fi
_p9k_cache_set "$context" "$cluster" "$namespace" "$text" "$suf" _p9k_cache_set "$name" "$namespace" "$cluster" "$cloud_name" "$cloud_account" "$cloud_zone" "$cloud_cluster" "$text" "$state"
fi fi
[[ -n $_p9k_cache_val[1] ]] || return
typeset -g P9K_KUBECONTEXT_NAME=$_p9k_cache_val[1] typeset -g P9K_KUBECONTEXT_NAME=$_p9k_cache_val[1]
typeset -g P9K_KUBECONTEXT_CLUSTER=$_p9k_cache_val[2] typeset -g P9K_KUBECONTEXT_NAMESPACE=$_p9k_cache_val[2]
typeset -g P9K_KUBECONTEXT_NAMESPACE=$_p9k_cache_val[3] typeset -g P9K_KUBECONTEXT_CLUSTER=$_p9k_cache_val[3]
_p9k_prompt_segment $0$_p9k_cache_val[5] magenta white KUBERNETES_ICON 0 '' "${_p9k_cache_val[4]//\%/%%}" typeset -g P9K_KUBECONTEXT_CLOUD_NAME=$_p9k_cache_val[4]
typeset -g P9K_KUBECONTEXT_CLOUD_ACCOUNT=$_p9k_cache_val[5]
typeset -g P9K_KUBECONTEXT_CLOUD_ZONE=$_p9k_cache_val[6]
typeset -g P9K_KUBECONTEXT_CLOUD_CLUSTER=$_p9k_cache_val[7]
[[ -n $_p9k_cache_val[8] ]] || return
_p9k_prompt_segment $0$_p9k_cache_val[9] magenta white KUBERNETES_ICON 0 '' "${_p9k_cache_val[8]//\%/%%}"
} }
################################################################ ################################################################