-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsns
60 lines (53 loc) · 1.42 KB
/
sns
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
#!/bin/bash
# GMcB - a quick helper script to change Kubernetes namespace only
# Version 0.2
createmenu ()
{
#echo "Items in the array: $#"
#echo "$@"
COLUMNS=1
select option; do # in "$@" is the default
if [ "$REPLY" -eq "$#" ];
then
echo "Exiting..."
break;
elif [ 1 -le "$REPLY" ] && [ "$REPLY" -le $(($#-1)) ];
then
return 0
break;
else
echo "Incorrect Input: Select a number 1-$#"
fi
done
}
get_ns ()
{
ns_array=($(kubectl get ns | grep -v NAME | awk '{print $1}'))
}
switch_namespace ()
{
get_ns
#echo -e "\nYou are currently in cluster" $(kubectl config current-context)
echo -e "Please select your namespace:\n"
createmenu "${ns_array[@]}"
echo -e "\nSwitching to namespace $option"
kubectl config set-context $(kubectl config current-context) --namespace=$option
kcur
}
kcur () {
echo -e "\nYou are currently here:\n"
echo -e "Current Cluster:\t" $(kubectl config current-context)
echo -e "Current Namespace:\t" $(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$(kubectl config current-context)\")].context.namespace}\n")
}
if [ $# -eq 0 ]; then
switch_namespace
else
get_ns
if [[ ! " ${ns_array[@]} " =~ " $1 " ]]; then
echo -e "\nUnable to switch namespace '$1' - namespace not found\n"
switch_namespace
else
kubectl config set-context $(kubectl config current-context) --namespace=$1
kcur
fi
fi