Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Enhancement] Adding the ability to strip cluster name from AWS ARN if using EKS #1237

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion segments/kubecontext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ P9K_KUBECONTEXT_BACKGROUND='blue'
### Customize Icon

The main Icon can be changed by setting `P9K_KUBECONTEXT_ICON="my_icon"`. To change the
icon color only, set `P9K_KUBECONTEXT_ICON_COLOR="red"`.
icon color only, set `P9K_KUBECONTEXT_ICON_COLOR="red"`.

### Strip Cluster and Namespace from AWS ARN (If using EKS)

You may extract the clutser name and namespace from the AWS ARN that is supplied when you use an EKS cluster. This is useful when you really just want to see the cluster name and do not want the full ARN.
```
P9K_KUBECONTEXT_STRIPEKS=true
```
5 changes: 5 additions & 0 deletions segments/kubecontext/kubecontext.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ prompt_kubecontext() {
cur_namespace="default"
fi

# Extract cluster name and namespace from AWS ARN, if enabled.
if [[ "$P9K_KUBECONTEXT_STRIPEKS" == true ]]; then
cur_ctx=${${cur_ctx##*\:}/cluster\//}
fi

local k8s_final_text=""

if [[ "$cur_ctx" == "$cur_namespace" ]]; then
Expand Down
40 changes: 40 additions & 0 deletions segments/kubecontext/kubecontext.spec
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,44 @@ function testKubeContextPrintsNothingIfKubectlNotAvailable() {
unalias kubectl
}

function mockKubectlEKS() {
case "$1" in
'version')
echo 'non-empty text'
;;
'config')
case "$2" in
'view')
case "$3" in
'-o=jsonpath={.current-context}')
echo 'arn:aws:eks:us-east-1:123456789012:cluster/eksname'
;;
'-o=jsonpath={.contexts'*)
echo ''
;;
*)
echo "Mock value missed"
exit 1
;;
esac
;;
esac
;;
esac
}

function testStripEKS() {
alias kubectl=mockKubectlEKS

P9K_KUBECONTEXT_STRIPEKS=true

local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(kubecontext)

assertEquals "%K{004} %F{015}⎈ %F{015}eksname/default %k%F{004}%f " "$(__p9k_build_left_prompt)"

unset P9K_LEFT_PROMPT_ELEMENTS
unalias kubectl
}

source shunit2/shunit2