-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubespy.sh
More file actions
executable file
·152 lines (130 loc) · 3.69 KB
/
kubespy.sh
File metadata and controls
executable file
·152 lines (130 loc) · 3.69 KB
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
#!/usr/bin/env bash
#
# pod debugging tool for kubernetes clusters with docker runtimes
# Copyright © 2019 Hua Zhihao <ihuazhihao@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
usage() {
local SELF
SELF="kubespy"
if [[ "$(basename "$0")" == kubectl-* ]]; then
SELF="kubectl spy"
fi
cat <<EOF
kubespy is a pod debugging tool for kubernetes clusters with docker runtimes
Usage:
$SELF POD [-c CONTAINER] [-n NAMESPACE] [--spy-image SPY_IMAGE]
Examples:
# debug the first container nginx from mypod
$SELF mypod
# debug container nginx from mypod
$SELF mypod -c nginx
# debug container nginx from mypod using busybox
$SELF mypod -c nginx --spy-image busybox
EOF
}
exit_err() {
echo >&2 "${1}"
exit 1
}
main() {
[ $# -eq 0 ] && exit_err "You must specify a pod for spying"
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
exit
;;
-c | --container)
co="$2"
shift
shift
;;
-n | --namespace)
ns="$2"
shift
shift
;;
--spy-image)
ep="$2"
shift
shift
;;
*)
po="$1"
shift
;;
esac
done
co=${co:-""}
ns=${ns:-"$(kubectl config view --minify -o 'jsonpath={..namespace}')"}
ns=${ns:-"default"}
ep=${ep:-"eu.gcr.io/cloud-native-coding/code-server-java-debug:3.4.1-3"}
spyid="spy-$(shuf -i 1000-9999 -n 1)"
kubectl -n "${ns}" delete po/"${spyid}" &>/dev/null || true
no=$(kubectl -n "${ns}" get pod "${po}" -o "jsonpath={.spec.nodeName}") || exit_err "cannot found Pod ${po}'s nodeName"
if [[ "${co}" == "" ]]; then
cid=$(kubectl -n "${ns}" get pod "${po}" -o='jsonpath={.status.containerStatuses[0].containerID}' | sed 's/docker:\/\///')
else
cid=$(kubectl -n "${ns}" get pod "${po}" -o='jsonpath={.status.containerStatuses[?(@.name=="'"${co}"'")].containerID}' | sed 's/docker:\/\///')
fi
echo "loading spy pod..."
kubectl -n "${ns}" run --generator=run-pod/v1 --overrides='
{
"spec": {
"hostNetwork": true,
"hostPID": true,
"hostIPC": true,
"nodeName": "'"${no}"'",
"containers": [
{
"name": "spy",
"image": "busybox",
"command": [ "/bin/chroot", "/host"],
"args": [
"docker",
"run",
"--network=container:'"${cid}"'",
"--pid=container:'"${cid}"'",
"--ipc=container:'"${cid}"'",
"'"${ep}"'"
],
"stdin": true,
"stdinOnce": true,
"tty": true,
"volumeMounts": [
{
"mountPath": "/host",
"name": "node"
}
]
}
],
"volumes": [
{
"name": "node",
"hostPath": {
"path": "/"
}
}
]
}
}
' --image=busybox --restart=Never "${spyid}"
kubectl -n "${ns}" port-forward pod/"${po}" 8080
kubectl -n "${ns}" delete po/"${spyid}" --force &>/dev/null || true
}
main "$@"