-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkctx.sh
54 lines (50 loc) · 1.58 KB
/
kctx.sh
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
function kctx() {
local KCONFIG="${HOME}/.kube/config"
local KCTXDIR="${HOME}/.kube/kctxcfg"
setup() {
if ! command -v yq &> /dev/null ; then
echo "yq must be installed." 1>&2
return 1
fi
rm -f "${KCTXDIR}"/*
mkdir -p "${KCTXDIR}"
local COUNT
COUNT="$(yq e '.contexts | length' "${KCONFIG}")"
local N=0
while [ "${N}" -lt "${COUNT}" ] ; do
local CTX
CTX="$(yq e ".contexts[${N}].name" "${KCONFIG}")"
local CLUSTER
CLUSTER="$(yq e ".contexts[${N}].context.cluster" "${KCONFIG}")"
local KUSER
KUSER="$(yq e ".contexts[${N}].context.user" "${KCONFIG}")"
local PROG
PROG="del(.clusters[] | select(.name != \"${CLUSTER}\"))"
PROG="${PROG} | del(.contexts[] | select(.name != \"${CTX}\"))"
PROG="${PROG} | del(.users[] | select(.name != \"${KUSER}\"))"
PROG="${PROG} | .current-context=\"${CTX}\""
yq e "${PROG}" "${KCONFIG}" > "${KCTXDIR}/${CTX}"
N=$((N + 1))
done
}
set_context() {
KUBECONFIG="${KCTXDIR}/$1"
export KUBECONFIG
unset POD_NAMESPACE
PS1="\h:\W ($1) \u\$ "
}
case "$1" in
"-h")
echo "Usage: kctx CONTEXT -- switch to CONTEXT and update PS1 prompt"
echo " kctx --setup -- read ${HOME}/.kube/config and write contexts to ${KCTXDIR}"
return 1
;;
"--setup")
setup
;;
*)
set_context "$1"
;;
esac
}