|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 | # shellcheck disable=SC2016
|
3 | 3 |
|
| 4 | +# Logic for authentication check is: |
| 5 | +# Check context is "cloud-platform-live" and client-id is "REPLACE_WITH_CLIENT_ID", then you aren't authenticated |
| 6 | +# Check context is "cloud-platform-live" and client-id is not "REPLACE_WITH_CLIENT_ID" and server version is returned, then you are authenticated |
| 7 | +# Check context is "cloud-platform-live" and client-id is not "REPLACE_WITH_CLIENT_ID" and server version is null, then there is a potential issue with authentication |
| 8 | + |
4 | 9 | PROMPT+='`\
|
5 |
| - kubectlCurrentContext=$(kubectl config current-context 2>/dev/null); \ |
6 |
| - kubectlCurrentNamespace=$(kubectl config view --minify --output "jsonpath={..namespace}"); \ |
7 |
| - kubectlClientId=$(kubectl config view --output json | jq -r ".users[0].user[\"auth-provider\"].config[\"client-id\"]"); \ |
8 |
| - kubectlServerVersion=$(kubectl version --output json 2>/dev/null | jq -r .serverVersion.gitVersion); \ |
9 |
| - if [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" == "REPLACE_WITH_CLIENT_ID" ]]; then \ |
10 |
| - echo -n "[ cluster: %{$fg[yellow]%}${kubectlCurrentContext} (requires authentication)%{$reset_color%} ] "; \ |
11 |
| - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${kubectlServerVersion}" != "null" ]]; then \ |
12 |
| - echo -n "[ cluster: %{$fg[green]%}${kubectlCurrentContext} (authenticated)%{$reset_color%} ] "; \ |
13 |
| - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ "${kubectlClientId}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${kubectlServerVersion}" == "null" ]]; then |
14 |
| - echo -n "[ cluster: %{$fg[red]%}${kubectlCurrentContext} (issue with authentication)%{$reset_color%} ] "; \ |
15 |
| - fi \ |
16 |
| - && if [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ ! -z "${kubectlCurrentNamespace}" ]] && [[ "${kubectlCurrentNamespace}" == *"-prod"* ]]; then \ |
17 |
| - echo -n "[ namespace: %{$fg[red]%}${kubectlCurrentNamespace}%{$reset_color%} ] "; \ |
18 |
| - elif [[ "${kubectlCurrentContext}" == "cloud-platform-live" ]] && [[ ! -z "${kubectlCurrentNamespace}" ]]; then \ |
19 |
| - echo -n "[ namespace: %{$fg[green]%}${kubectlCurrentNamespace}%{$reset_color%} ] "; \ |
20 |
| - fi \ |
| 10 | + CURRENT_CONTEXT=$(kubectl config current-context 2>/dev/null); \ |
| 11 | + CURRENT_NAMESPACE=$(kubectl config view --minify --output "jsonpath={..namespace}"); \ |
| 12 | + CURRENT_CLIENT_ID=$(kubectl config view --output json | jq -r ".users[0].user[\"auth-provider\"].config[\"client-id\"]"); \ |
| 13 | + SERVER_VERSION=$(kubectl version --output json 2>/dev/null | jq -r .serverVersion.gitVersion); \ |
| 14 | + if [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" == "REPLACE_WITH_CLIENT_ID" ]]; then \ |
| 15 | + echo -n "[ cluster: %{$fg[yellow]%}${CURRENT_CONTEXT} (requires authentication)%{$reset_color%} ] "; \ |
| 16 | + elif [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${SERVER_VERSION}" != "null" ]]; then \ |
| 17 | + echo -n "[ cluster: %{$fg[green]%}${CURRENT_CONTEXT} (authenticated)%{$reset_color%} ] "; \ |
| 18 | + elif [[ "${CURRENT_CONTEXT}" == "cloud-platform-live" ]] && [[ "${CURRENT_CLIENT_ID}" != "REPLACE_WITH_CLIENT_ID" ]] && [[ "${SERVER_VERSION}" == "null" ]]; then |
| 19 | + echo -n "[ cluster: %{$fg[red]%}${CURRENT_CONTEXT} (issue with authentication)%{$reset_color%} ] "; \ |
| 20 | + fi; \ |
| 21 | + case ${CURRENT_NAMESPACE} in \ |
| 22 | + *"development"*|*"sandbox"*) \ |
| 23 | + colour=green \ |
| 24 | + ;; \ |
| 25 | + *"test"*) \ |
| 26 | + colour=blue \ |
| 27 | + ;; \ |
| 28 | + *"preproduction"*) \ |
| 29 | + colour=yellow \ |
| 30 | + ;; \ |
| 31 | + *"production"*) \ |
| 32 | + colour=red \ |
| 33 | + ;; \ |
| 34 | + *) \ |
| 35 | + colour=white \ |
| 36 | + ;; \ |
| 37 | + esac; \ |
| 38 | + if [[ ! -z ${CURRENT_NAMESPACE} ]]; then \ |
| 39 | + echo -n "[ namespace: %{$fg[$colour]%}${CURRENT_NAMESPACE}%{$reset_color%} ] "; \ |
| 40 | + fi |
21 | 41 | `'
|
0 commit comments