diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 1bfd348..3d8a245 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1410,20 +1410,20 @@ prompt_dir_writable() { fi } -# Kubernetes Current Context +# Kubernetes Current Context/Namespace prompt_kubecontext() { local kubectl_version="$(kubectl version --client 2>/dev/null)" if [[ -n "$kubectl_version" ]]; then - # Get the current Kubernetes config context's namespaece - local k8s_namespace=$(kubectl config get-contexts --no-headers | grep '*' | awk '{print $5}') # Get the current Kuberenetes context - local k8s_context=$(kubectl config current-context) - - if [[ -z "$k8s_namespace" ]]; then - k8s_namespace="default" + local cur_ctx=$(kubectl config view -o=jsonpath='{.current-context}') + cur_namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" + # If the namespace comes back empty set it default. + if [[ -z "${cur_namespace}" ]]; then + cur_namespace="default" fi - "$1_prompt_segment" "$0" "$2" "magenta" "white" "$k8s_context/$k8s_namespace" "KUBERNETES_ICON" + + "$1_prompt_segment" "$0" "$2" "magenta" "white" "$cur_ctx/$cur_namespace" "KUBERNETES_ICON" fi } diff --git a/test/segments/kubecontext.spec b/test/segments/kubecontext.spec index eaaa230..4f1d2c5 100755 --- a/test/segments/kubecontext.spec +++ b/test/segments/kubecontext.spec @@ -18,11 +18,19 @@ function mockKubectl() { ;; 'config') case "$2" in - 'current-context') - echo 'minikube' - ;; - 'get-contexts') - echo '* minikube minikube minikube ' + 'view') + case "$3" in + '-o=jsonpath={.current-context}') + echo 'minikube' + ;; + '-o=jsonpath={.contexts'*) + echo '' + ;; + *) + echo "Mock value missed" + exit 1 + ;; + esac ;; esac ;; @@ -36,11 +44,21 @@ function mockKubectlOtherNamespace() { ;; 'config') case "$2" in - 'current-context') - echo 'minikube' - ;; - 'get-contexts') - echo '* minikube minikube minikube kube-system' + 'view') + case "$3" in + # Get Current Context + '-o=jsonpath={.current-context}') + echo 'minikube' + ;; + # Get current namespace + '-o=jsonpath={.contexts'*) + echo 'kube-system' + ;; + *) + echo "Mock value missed" + exit 1 + ;; + esac ;; esac ;;