-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.kube_helper
34 lines (31 loc) · 1.35 KB
/
.kube_helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
. ${HOME}/.colors
CONTEXT_OPT=${CONTEXT_OPT:-"--context"}
NAMESPACE_OPT=${NAMESPACE_OPT:-"--namespace"}
if [ -n "${KCTX}" ]
then
CONTEXT="${CONTEXT_OPT}=${KCTX}"
config=$(kubectl config view | yq -o y ".contexts[] | select(.name==\"${KCTX}\")")
cluster=$(echo "${config}" | yq -r '.context.cluster')
[ -t 1 ] && \
/bin/echo -e "${Dark_Gray}Setting context${RESET}: ${Light_Green}${KCTX}${RESET} in ${Light_Blue}${cluster}${RESET} cluster"
else
config=$(kubectl config view --minify)
cluster=$(echo "${config}" | yq -r '.contexts| .[]| .context.cluster')
context_name=$(echo "${config}" | yq -r '.contexts| .[]| .name')
KCTX=${context_name}
[ -t 1 ] && \
/bin/echo -e "${Dark_Gray}Default context${RESET}: ${Green}${context_name}${RESET} in ${Blue}${cluster}${RESET} cluster"
fi
if [ -n "${KNS}" ]
then
NAMESPACE="${NAMESPACE_OPT}=${KNS}"
[ -t 1 ] && \
/bin/echo -e "${Dark_Gray}Setting namespace${RESET}: ${Yellow}${KNS}${RESET}"
else
[ -z "${config}" -a -n "${context_name}" ] && config=$(kubectl config view --minify)
[ -z "${context_name}" ] && config=$(kubectl config view | yq -r "{\"contexts\": [.contexts[] | select(.name==\"${KCTX}\")]}")
namespace=$(echo "${config}" | yq -r '.contexts| .[]| .context.namespace')
[ -t 1 ] && \
/bin/echo -e "${Dark_Gray}Default namespace${RESET}: ${Brown}${namespace}${RESET}"
fi