-
Notifications
You must be signed in to change notification settings - Fork 63
/
kubectl-switch
executable file
·83 lines (69 loc) · 3.1 KB
/
kubectl-switch
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# If only one argument is given when calling this script, we do a namespace switch.
# If a second argument exists when calling this script, we do a cluster switch.
# When switching clusters, alert the user if that cluster is not found.
# The trap provides the current context on exit. The context names are sorted to provide consistency.
LRED=$(echo -en '\033[01;31m')
RESTORE=$(echo -en '\033[0m')
trap 'CURRENT_CONTEXT=$(kubectl config current-context) && echo && kubectl config get-contexts | head -n1 && kubectl config get-contexts | sed 1d | sort --key=1 -b | sed "s|\*\(.*\)${CURRENT_CONTEXT}\(.*\)$|${LRED}\*\1${CURRENT_CONTEXT}\2${RESTORE}|g"' 0 1 3 15
# Usage
usage() { echo -e "# Usage\n\tPrompt for Clusters: kubectl switch cluster -l\n\tSwitch Namespace: kubectl switch <namespace>\n\tSwitch Cluster: kubectl switch cluster <cluster_name>\n" && exit 0; }
[ $# -eq 0 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] && usage
NAMESPACE="$1"
CLUSTER="$2"
### List the clusters to choose from
function list_clusters() {
IFS=$'\n'
CLUSTERS=($(kubectl config get-clusters | grep -v NAME | sort))
count=1
for i in ${!CLUSTERS[@]}; do
echo "[$count]: ${CLUSTERS[$i]}"
let count=count+1
done
echo; read -p "Enter the number of the cluster to use: " SELECTION
SELECTION=$(($SELECTION - 1))
CLUSTER="${CLUSTERS[$SELECTION]}"
switch_cluster
}
### Switch cluster context
function switch_cluster() {
# Verify it's a valid cluster name with kubectl
CLUSTER_NAME=$(kubectl config get-clusters | grep "$CLUSTER$")
# Fallback in case the user has renamed their context for the cluster.
if [ -z "${CLUSTER_NAME}" ]; then
# Allow fuzzying on context name
CLUSTER_NAME=($(kubectl config get-contexts -o name | grep $CLUSTER))
# If globbing gives more than one match, discard those and look for an exact match
if [ ${#CLUSTER_NAME[@]} -ne 1 ]; then
unset CLUSTER_NAME
CLUSTER_NAME=$(kubectl config get-contexts -o name | grep $CLUSTER$)
fi
# If all lookups have failed, exit 1.
if [ -z "${CLUSTER_NAME}" ]; then
RED=$(echo -en '\033[00;31m')
YELLOW=$(echo -en '\033[00;33m')
RESTORE=$(echo -en '\033[0m')
echo -e "\n${RED}Error on lookup. Cannot find:${YELLOW} $CLUSTER${RESTORE}\n"
exit 1
fi
fi
CLUSTER="$CLUSTER_NAME"
kubectl config use-context $CLUSTER >/dev/null 2>&1
if [ $? -ne 0 ]; then
CLUSTER=$(kubectl config get-contexts | grep " $CLUSTER " | sed 's|\*||g' | awk '{print $1}') >/dev/null 2>&1
kubectl config use-context $CLUSTER >/dev/null 2>&1
fi
[ $? -ne 0 ] && echo -e "\n####### TYPO! Please re-check the spelling for cluster: $2" && exit 1
}
### Switch namespace for current context
function switch_namespace() {
kubectl config set-context $(kubectl config current-context) --namespace="$NAMESPACE" 1>/dev/null
exit 0
}
if [[ "$@" =~ " -l" ]]; then
list_clusters
elif [[ "$@" =~ "cluster" ]]; then
switch_cluster
elif [[ ! "$@" =~ "cluster" ]]; then
switch_namespace
fi