forked from kvaps/kubectl-node-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl-node_shell
executable file
·156 lines (144 loc) · 3.36 KB
/
kubectl-node_shell
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
#!/usr/bin/env sh
set -e
kubectl=kubectl
version=1.5.5
generator=""
node=""
nodefaultctx=0
nodefaultns=0
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"'
if [ -t 0 ]; then
tty=true
else
tty=false
fi
while [ $# -gt 0 ]; do
key="$1"
case $key in
-v | --version)
echo "kubectl-node-shell $version"
exit 0
;;
--context)
nodefaultctx=1
kubectl="$kubectl --context $2"
shift
shift
;;
--kubecontext=*)
nodefaultctx=1
kubectl="$kubectl --context=${key##*=}"
shift
;;
--kubeconfig)
kubectl="$kubectl --kubeconfig $2"
shift
shift
;;
--kubeconfig=*)
kubectl="$kubectl --kubeconfig=${key##*=}"
shift
;;
-n | --namespace)
nodefaultns=1
kubectl="$kubectl --namespace $2"
shift
shift
;;
--namespace=*)
nodefaultns=1
kubectl="$kubectl --namespace=${key##*=}"
shift
;;
--)
shift
break
;;
*)
if [ -z "$node" ]; then
node="${1#node/}"
shift
else
echo "exactly one node required"
exit 1
fi
;;
esac
done
# Set the default context and namespace to avoid situations where the user switch them during the build process
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(${kubectl} config current-context)"
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"
if [ $# -gt 0 ]; then
while [ $# -gt 0 ]; do
cmd="$cmd, \"$(echo "$1" | \
awk '{gsub(/["\\]/,"\\\\&");gsub(/\x1b/,"\\u001b");printf "%s",last;last=$0"\\n"} END{print $0}' \
)\""
shift
done
cmd="$cmd ]"
else
cmd="$cmd, \"bash\", \"-l\" ]"
fi
if [ -z "$node" ]; then
echo "Please specify node name"
exit 1
fi
image="${KUBECTL_NODE_SHELL_IMAGE:-docker.io/library/alpine}"
pod="nsenter-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)"
# Check the node
$kubectl get node "$node" >/dev/null || exit 1
container_cpu="${KUBECTL_NODE_SHELL_POD_CPU:-100m}"
container_memory="${KUBECTL_NODE_SHELL_POD_MEMORY:-256Mi}"
labels="${KUBECTL_NODE_SHELL_LABELS}"
overrides="$(
cat <<EOT
{
"spec": {
"nodeName": "$node",
"hostPID": true,
"hostNetwork": true,
"containers": [
{
"securityContext": {
"privileged": true
},
"image": "$image",
"name": "nsenter",
"stdin": true,
"stdinOnce": true,
"tty": $tty,
"command": $cmd,
"resources": {
"limits": {
"cpu": "${container_cpu}",
"memory": "${container_memory}"
},
"requests": {
"cpu": "${container_cpu}",
"memory": "${container_memory}"
}
}
}
],
"tolerations": [
{
"key": "CriticalAddonsOnly",
"operator": "Exists"
},
{
"effect": "NoExecute",
"operator": "Exists"
}
]
}
}
EOT
)"
# Support Kubectl <1.18
m=$(kubectl version --client -o yaml | awk -F'[ :"]+' '$2 == "minor" {print $3+0}')
if [ "$m" -lt 18 ]; then
generator="--generator=run-pod/v1"
fi
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM
echo "spawning \"$pod\" on \"$node\"" >&2
$kubectl run --image "$image" --restart=Never --overrides="$overrides" --labels="$labels" $([ "$tty" = true ] && echo -t) -i "$pod" $generator