-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.kbashrc
220 lines (206 loc) · 5.81 KB
/
.kbashrc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# .bashrc
debugf () {
if [ ! -z $DEBUG ]; then
echo $@
fi
}
printoption() {
kubectl get pods -n $NAMESPACE -o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name | nl -w2 -bp[a-z] -s'. '
echo "Choose a pod from list pod [pod number] [container number]"
echo ""
}
pods () {
kubectl get pods -n $NAMESPACE | nl -w2 -bp[a-z] -s'. '
}
pod () {
if [ -z $PODNAME ]; then
if [ "$#" -eq 0 ]; then
while true; do
local npods=`kubectl get pods -n $NAMESPACE | wc -l`
pods
echo -n "Select a pod [0 to quit] > "
read selection
if [ $selection -eq 0 ]; then
return
elif [ $selection -gt 0 ] && [ $selection -lt $npods ]; then
pod $selection
return
fi
echo "Invalid selection $selection"
done
else
podname=$(kubectl get pods -n $NAMESPACE | grep -v NAME | sed -n "$1 p" | cut -d" " -f1)
export PODNAME=$podname
PS1="\e[0;32m$NAMESPACE>\e[m\e[0;31m$PODNAME>\e[m "
container $2
fi
else
kubectl get pods -n $NAMESPACE -o wide $PODNAME
fi
}
container () {
if [ -z $PODNAME ]; then
printoption
return
fi
clist=$(kubectl get pods -n $NAMESPACE $PODNAME -o=custom-columns=:.spec.containers[*].name | grep .)
if [ "$#" -eq 0 ]; then
nc=`echo $clist | tr ',' '\n' | wc -l`
if [ $nc -gt 1 ]; then
while true; do
echo $clist | tr ',' '\n' | nl -w2 -bp[a-z] -s'. '
echo -n "Select a container [0 to quit] > "
read selection
if [ $selection -eq 0 ]; then
return
elif [ $selection -gt 0 ] && [ $selection -le $nc ]; then
container $selection
return
fi
echo "Invalid selection $selection"
done
fi
else
podtype=$(echo $clist | tr ',' '\n' | sed -n "$1 p")
export CONTAINER=$podtype
PS1="\e[0;32m$NAMESPACE>\e[m\e[0;31m$PODNAME>\e[m\e[m\e[0;36m$CONTAINER> \e[m"
fi
}
quit () {
if [ -z $PODNAME ]; then
tmux kill-session -t $SESSION
export SESSION=""
elif [ -z $CONTAINER ]; then
export PODNAME=""
PS1="\e[0;32m$NAMESPACE > \e[m"
else
export CONTAINER=""
PS1="\e[0;32m$NAMESPACE>\e[m\e[0;31m$PODNAME>\e[m "
fi
}
bash () {
if [ -z $CONTAINER ]; then
kubectl exec -it $PODNAME -n $NAMESPACE -- /bin/bash
else
kubectl exec -it $PODNAME -n $NAMESPACE -c $CONTAINER -- /bin/bash
fi
}
cmd () {
if [ -z $PODNAME ]; then
printoption
return
fi
echo $@
if [ -z $CONTAINER ]; then
kubectl exec -it $PODNAME -n $NAMESPACE -- "$@"
else
kubectl exec -it $PODNAME -n $NAMESPACE -c $CONTAINER -- "$@"
fi
}
cpfrom () {
if [ -z $PODNAME ]; then
printoption
return
fi
if [ -z $CONTAINER ]; then
kubectl cp $NAMESPACE/$PODNAME:$1 $2
else
kubectl cp $NAMESPACE/$PODNAME:$1 $2 -c $CONTAINER
fi
}
cpto () {
if [ -z $PODNAME ]; then
printoption
return
fi
if [ -z $CONTAINER ]; then
kubectl cp $1 $NAMESPACE/$PODNAME:$2
else
kubectl cp $1 $NAMESPACE/$PODNAME:$2 -c $CONTAINER
fi
}
service () {
kubectl get service -n $NAMESPACE -o wide
}
log () {
if [ -z $PODNAME ]; then
printoption
return
fi
if [ -z $CONTAINER ]; then
kubectl logs $1 $PODNAME -n $NAMESPACE
else
kubectl logs $1 $PODNAME -n $NAMESPACE -c $CONTAINER
fi
}
logall () {
if [ -z $PODNAME ]; then
printoption
return
fi
appname=`kubectl get pod $PODNAME -o template --template='{{.metadata.labels}}' -n $NAMESPACE | cut -d: -f2 | cut -d" " -f1`
if [ -z $CONTAINER ]; then
kubectl logs --selector app=$appname -n $NAMESPACE $1
else
kubectl logs --selector app=$appname -n $NAMESPACE -c $CONTAINER $1
fi
}
describe () {
kubectl describe pod -n $NAMESPACE $PODNAME
}
split () {
tmux split-window $1\; \
send-key "source ~/.kbashrc $NAMESPACE" C-m \; \
select-layout even-vertical \;
}
switch () {
selection=0
if [ "$#" -gt 0 ];then
export NAMESPACE=$1
source ~/.kbashrc
return
fi
kubectl get namespaces | awk '{print $1}' | nl -w2 -bp[a-z] -s'. '
max=`kubectl get namespaces | wc -l`
while true; do
echo -n "Select a namespace [0 to quit] > "
read selection
if [ $selection -eq 0 ]; then
return
elif [ $selection -gt 0 ] && [ $selection -lt $max ]; then
namespace=$(kubectl get namespaces | grep -v NAME | sed -n "$selection p" | cut -d" " -f1)
source ~/.kbashrc $namespace
return
fi
echo "Invalid selection $selection"
done
}
help () {
echo ""
echo " pods Display pods"
echo " pod [pod][container] Select a pod/container"
echo " service Service"
echo " container [container] Choose a container from the list"
echo " describe Describe the pod"
echo " bash Enter a shell"
echo " cmd <command> Run a command in a container"
echo " cpto <local> <remote> Copy from local to pod"
echo " cpfrom <remote> <local> Copy from pod to local"
echo " log [-f] Container log"
echo " split Split panes [C-A y to send command to all panes]"
echo " switch [namespace] Switch namespace"
echo ""
}
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=x
if [ "$#" -gt 0 ];then
export NAMESPACE=$1
export PS1="\e[0;32m$NAMESPACE>\e[m"
else
export SESSION=`date +%s`
tmux -f ~/.tmux.conf new-session -s $SESSION -d \; \
send-key "source ~/.kbashrc $NAMESPACE" C-m \; \
attach-session -d \; \
rename-window $NAMESPACE \;
fi
# User specific aliases and functions