-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-pod-and
executable file
·237 lines (210 loc) · 5.93 KB
/
find-pod-and
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env bash
#
# (C) SpoddyCoder, 2022
# See DOC_URL for more info on what this is and how to use
#
#
# TODO:
# include the container name in search (currently searches across namespace + pod name)
# allow user to select multiple/all matching pods and run the [command] on each
# main loop is a bit 'bash-y' - use arrays for better maintainability / readability
#
# version
VER=0.9.8
DOC_URL="https://github.com/SpoddyCoder/scube-tools"
########################
# fixed conf
#
# valid commands
CONTAINER_COMMANDS="bash sh log logs tail"
POD_COMMANDS="describe"
COMMANDS="$CONTAINER_COMMANDS $POD_COMMANDS"
########################
# functions
#
# show help
usage() {
echo "Search running pods and list matches"
echo "Select one from the list and do [command]"
echo "If a pod has multiple containers, each is shown"
echo
echo "$0 [command] [searchterm] [namespace]"
echo
echo "eg: $0 bash my-app -A"
echo
echo "[command]"
echo " bash - /bin/bash into a container"
echo " sh - /bin/sh into a container"
echo " log - show container logs (logs also works)"
echo " tail - tail container logs"
echo " describe - describe the pod"
echo " help - show usage info (--help also works)"
echo
echo "[searchterm]"
echo " somestring - search for pods matching somestring"
echo " - - match-all, return all pods in selected namespace"
echo
echo "[namespace] (optional)"
echo " no value - search the current namespace in the current context"
echo " -A - search across all namepsaces"
echo " mynamespace - search in mynamespace"
echo
echo
echo "v${VER}"
echo "${DOC_URL}"
echo
}
# $1 = error message
errMsg() {
echo -e "Error: ${1}\n"
}
########################
# start
#
echo
# display help (also respond to --help)
if [ "$#" -gt "0" ]; then
if [[ "$1" == "help" || "$1" == "--help" ]]; then
usage
exit 1
fi
fi
# check valid params
if [[ "$#" != "2" && "$#" != "3" ]]; then
usage
exit 1
fi
# command
command=$1
if [[ ! $COMMANDS =~ (^|[[:space:]])$command($|[[:space:]]) ]]; then
errMsg "invalid command"
usage
exit 1
fi
if [[ $POD_COMMANDS =~ (^|[[:space:]])$command($|[[:space:]]) ]]; then
pod_command=true
else
pod_command=false
fi
# search term
search=$2
if [[ "$search" == "-A" && "$#" == "2" ]]; then
errMsg "no search term specified"
usage
exit 1
fi
# namespace
if [ "$#" == "3" ]; then
if [ "$3" == "-A" ]; then
namespace="-A"
ns_label="across all namespaces"
else
namespace=$3
ns_label="in namespace: $namespace"
fi
else
namespace=$(kubectl config view --minify -o jsonpath='{..namespace}')
ns_label="in namespace: $namespace"
fi
# lets go...
search_label=$( [ "$pod_command" = true ] && echo "pods" || echo "containers" )
if [ "$search" == "-" ]; then
echo "Returning all $search_label $ns_label"
else
echo "Searching for $search_label matching '$search' $ns_label"
fi
# query pods
if [ "$namespace" == "-A" ]; then
podlist=$(kubectl get pod -A -o wide)
# & search by both name + namespace
results=$(echo "$podlist" | tr -s ' ' | awk '{print $1" "$2}' | grep $search)
else
podlist=$(kubectl get pod -n $namespace -o wide)
# and search by name only
results=$(echo "$podlist" | tr -s ' ' | awk '{print $1}' | grep $search)
fi
if [ "$search" != "-" ]; then
results=$(echo "$results" | grep $search)
fi
# get containers running inside each pod
container_list=""
display_list=""
cnt=0
while read pod; do
if [ "$namespace" == "-A" ]; then
podns=$(echo $pod | awk '{print $1}')
podname=$(echo $pod | awk '{print $2}')
else
podns=$namespace
podname=$(echo $pod | awk '{print $1}')
fi
if [ ! -z $podname ]; then
# check if pod has containers (should)
containers=$(kubectl get pods $podname -n $podns -o jsonpath='{.spec.containers[*].name}')
#containers="nginx varnish" # test mutliple container output
if [ "$?" != "0" ]; then
# in the event of an error, just show the pod info, pod level commands should still work
cnt=$((cnt+1))
display_list=$(echo -e "${display_list}\n${cnt}) ${podns}: $podname")
container_list=$(echo -e "${container_list}\n$cnt $podns $podname")
else
for container in $containers; do
cnt=$((cnt+1))
display_list=$(echo -e "${display_list}\n${cnt}) ${podns}: $podname -> $container")
container_list=$(echo -e "${container_list}\n$cnt $podns $podname $container")
done
fi
fi
done <<< "$results"
if [ $(echo "$results" | xargs | wc -l) == 0 ]; then
echo "None found"
exit 0
else
echo "$display_list"
fi
# get user pod/container selection from list
echo
select_label=$( [ "$pod_command" = true ] && echo "pod" || echo "container" )
read -p "Select $select_label number (or Enter to exit): " selected
if [ -z "$selected" ]; then
echo "User cancelled"
exit 0
fi
entry=$(echo "$container_list" | grep "^$selected")
if [ $(echo "$entry" | xargs | wc -l) == 0 ]; then
errMsg "invalid selection"
exit 1
fi
ns=$(echo $entry | awk '{print $2}')
pod=$(echo $entry | awk '{print $3}')
container=$(echo $entry | awk '{print $4}')
# and run the selected command on it...
echo
args="-n $ns"
if [ ! -z "$container" ]; then
args="${args} -c $container"
fi
case $command in
bash)
kubectl exec -it $pod $args -- /bin/bash
;;
sh)
kubectl exec -it $pod $args -- /bin/sh
;;
log*)
kubectl logs $pod $args
;;
tail)
kubectl logs -f $pod $args
;;
describe)
kubectl describe pod $pod -n $ns
;;
*)
errMsg "unknown command '$command'"
exit 1
;;
esac
# exit success
echo
exit 0