-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkc.sh
28 lines (22 loc) · 1.03 KB
/
kc.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
#!/usr/bin/env bash
# KC Quick kubernetes context switch
# Run without arguments to quickly select the working context
# Run with -n to select the namespace from the current context
TEMPFILE=$(mktemp)
if [ "X${1}X" == "X-nX" ]
then
NAMESPACES=()
while IFS='' read -r line; do NAMESPACES+=("$line" "$line"); done < <(kubectl get ns --no-headers=true -o name | sed -re 's/namespace\/(.*)/\1/gi')
if dialog --keep-tite --no-tags --default-item "$(kubectl config current-context)" --menu "Please select a k8s namespace" 0 0 5 "${NAMESPACES[@]}" 2>"${TEMPFILE}"
then
kubectl config set-context --current --namespace="$(cat "${TEMPFILE}")"
fi
else
CONTEXTS=()
while IFS='' read -r line; do CONTEXTS+=("$line" "$line"); done < <(kubectl config get-contexts --no-headers=true -o name)
if dialog --keep-tite --no-tags --default-item "$(kubectl config current-context)" --menu "Please select a k8s context" 0 0 5 "${CONTEXTS[@]}" 2>"${TEMPFILE}"
then
kubectl config use-context "$(cat "${TEMPFILE}")"
fi
fi
rm "${TEMPFILE}"