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 all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ The segments that are currently available are:
* [`java_version`](segments/java_version/README.md) - Show the current Java version.
* **Haskell Segments:**
* [`stack_project`](segments/stack_project/README.md) - Show if you are in a Haskell Stack project directory.
* **Terraform Segments:**
* [`terraform`](segments/terraform/README.md) - Display the selected terraform workspace.

**Cloud Segments:**
* **AWS Segments:**
Expand Down
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
```
6 changes: 6 additions & 0 deletions segments/kubecontext/kubecontext.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ prompt_kubecontext() {
cur_namespace="default"
fi

# Extract cluster name and namespace from AWS ARN, if enabled.
# Example eks cluster string: arn:aws:eks:us-east-1:123456789012:cluster/eksname
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
27 changes: 27 additions & 0 deletions segments/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Terraform Workspace

Displays the currently selected workspace within your state file. Works by inspecting your .terraform/environment file.

![](segment.png)

## Installation

To use this segment, you need to activate it by adding `terraform` to your
`P9K_LEFT_PROMPT_ELEMENTS` or `P9K_RIGHT_PROMPT_ELEMENTS` array, depending
where you want to show this segment.

## Configuration

### Color Customization

You can change the foreground and background color of this segment by setting.
```
P9K_TERRAFORM_FOREGROUND='red'
P9K_TERRAFORM_BACKGROUND='blue'
```

### Customize Icon

The main Icon can be changed by setting `P9K_TERRAFORM_ICON="my_icon"`. To change the
icon color only, set `P9K_TERRAFORM_ICON_COLOR="red"`. Using a globe for now until a
terraform specific icon is included in font packages.
36 changes: 36 additions & 0 deletions segments/terraform/terraform.p9k
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
################################################################
# @title powerlevel9k Segment - Terraform Workspace
# @source [powerlevel9k](https://github.com/bhilburn/powerlevel9k)
##

(){
# Set the right locale to protect special characters
local LC_ALL="" LC_CTYPE="en_US.UTF-8"
################################################################
# Register segment
# Parameters:
# segment_name context background foreground Generic Flat/Awesome-Patched Awesome-FontConfig Awesome-Mapped-FontConfig NerdFont
# ⎈ ⎈ ⎈ ⎈ ⎈
p9k::register_segment "TERRAFORM" "" "purple" "black" "TF: " $'\uF484' $'\uF484' $'\uF484' $'\uF484'
}

################################################################
# @description
# Display the select Terraform Workspace.
##
# @args
# $1 string Alignment - left | right
# $2 integer Segment index
# $3 boolean Whether the segment should be joined
##
prompt_terraform() {
local tf_workspace=""

if [[ -e .terraform/environment ]]; then
tf_workspace=$(< ./.terraform/environment)
tf_workspace=${tf_workspace%%[[:space:]]}
fi

p9k::prepare_segment "$0" "" $1 "$2" $3 "$tf_workspace"
}
51 changes: 51 additions & 0 deletions segments/terraform/terraform.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env zsh
#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8

# Required for shunit2 to run correctly
setopt shwordsplit
SHUNIT_PARENT=$0

function setUp() {
export TERM="xterm-256color"
local -a P9K_RIGHT_PROMPT_ELEMENTS
P9K_RIGHT_PROMPT_ELEMENTS=()
# Load Powerlevel9k
source powerlevel9k.zsh-theme
source segments/terraform/terraform.p9k
}

function testTerraformEnvironment() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(terraform)

# Setup mock data, there is a .terraform/environment file showing the selected
# environment/workspace so we will key off of that as its faster than running
# a 'terraform workspace list'
if [ -d '.terraform' ]; then
echo "Bailing out, theres an active .terraform directory here!"
else
touch placeholder.tf # This is needed because we only run prompt when your inside a directory with .tf files in it
mkdir .terraform
touch ./.terraform/environment
echo "workspace-name" > ./.terraform/environment

assertEquals "%K{093} %F{000}TF: %F{000}workspace-name %k%F{093}%f " "$(__p9k_build_left_prompt)"

rm placeholder.tf
rm -rf .terraform
fi

unset P9K_LEFT_PROMPT_ELEMENTS
}

function testTerraformNoEnvironment() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(terraform)

# No .terraform or workspace detected so show none.
assertEquals "%k%FNONE%f " "$(__p9k_build_left_prompt)"

unset P9K_LEFT_PROMPT_ELEMENTS
}

source shunit2/shunit2