/
k8s.sh
139 lines (118 loc) · 3.32 KB
/
k8s.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# colors
GREEN='\033[0;32m';
YELLOW='\033[0;33m';
NC='\033[0m' # No Color
## kubernetes
alias k=kubectl
# get current kubectl context with index numbers. If a context name is also provided as an argument, switch to that context
function get-ctx() {
curr_ctx=`kubectl config current-context`
idx=1;
for c in $(kubectx); do
if [[ "$c" = "$curr_ctx" ]]; then
echo "${YELLOW}$idx${NC} ${YELLOW}$c${NC}";
else
echo "${YELLOW}$idx${NC} $c";
fi
((idx=idx+1));
done;
}
function set-ctx() {
idx=1;
for c in $(kubectx); do
if [[ "$idx" = "$1" ]]; then
kubectx $c;
break;
else
((idx=idx+1));
fi
done;
}
# get namespaces of the cluster in current context with index numbers. If a namespace name is also provided as an argument, switch to that namespace
function get-ns() {
curr_ns=`kubectl config view --minify -o jsonpath='{..namespace}'`
idx=1;
for n in $(kubens); do
if [[ "$n" = "$curr_ns" ]]; then
echo "${YELLOW}$idx${NC} ${YELLOW}$n${NC}";
else
echo "${YELLOW}$idx${NC} $n";
fi
((idx=idx+1));
done;
}
function set-ns() {
idx=1;
for n in $(kubens); do
if [[ "$idx" = "$1" ]]; then
kubens $n;
break;
else
((idx=idx+1));
fi
done;
}
# Shortcut to the context-switch function
function kx() {
if [[ -z "$1" ]]; then
get-ctx;
else
set-ctx $1;
fi
}
# Shortcut to the ns-switching function
function kn() {
if [[ -z "$1" ]]; then
get-ns;
else
set-ns $1;
fi
}
# Shortcut to remove cluster from context
function kxr() {
idx=1;
for c in $(kubectx); do
if [[ "$idx" = "$1" ]]; then
yq eval -i "del(.clusters.[] | select(.name == \"$c\"))" ~/.kube/config
yq eval -i "del(.contexts.[] | select(.name == \"$c\"))" ~/.kube/config
yq eval -i "del(.users.[] | select(.name == \"$c\"))" ~/.kube/config
echo "Removed ${YELLOW}$c${NC} from kube-context";
break;
else
((idx=idx+1));
fi
done;
}
# get pod identifier
# usage : pod <namespace> <service-name>
function pod() {
cands=`kubectl -n $1 get pods| grep -v 'No resources found' | grep Running | awk '{print $1}'`
(echo "$cands" | grep ^$2-$1) || (echo "$cands" | grep ^$2) || (echo "$cands" | grep ^$1) || (echo "$cands" | grep $1)
}
# get log for the pod given
# usage : log <namespace> <service-name>
function log() {
pod=`pod $1 $2`
kubectl -n $1 logs -c $2 $pod -f
}
# log forwarding
# usage : lf <namespace> <service-name>
function lf() {
pod=`pod $1 $2`
if [[ -z "$pod" ]]; then
echo "no pod found in namespace $1 name $2. Searched for prefix $2-$1" >&2
else
kubectl -n $1 logs --tail=1 -c $2 -f $pod
fi
}
# port forwarding
# usage : pf <namespace> <service-name> 8080:8080
function pf() {
pod=`pod $1 $2`
if [[ -z "$pod" ]]; then
echo "no pod found in namespace $1 name $2" >&2
else
echo "forwarding port from $pod container $2 $3" >&2
kubectl -n $1 port-forward $pod $3
fi
}