forked from kvaps/kubectl-node-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl-node_shell
executable file
·76 lines (68 loc) · 1.4 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
#!/bin/sh
context=""
kubeconfig=""
generator=""
node=""
namespace=""
while [ $# -gt 0 ]; do
key="$1"
case $key in
--context)
context="--context $2"
shift
shift
;;
--kubeconfig)
kubeconfig="--kubeconfig $2"
shift
shift
;;
-n | --namespace)
namespace="--namespace $2"
shift
shift
;;
*)
node="$1"
shift
;;
esac
done
if [ -z "$node" ]; then
echo "Please specify node name"
exit 1
fi
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" $context $kubeconfig >/dev/null || exit 1
overrides="$(
cat <<EOT
{
"spec": {
"nodeName": "$node",
"hostPID": true,
"containers": [
{
"securityContext": {
"privileged": true
},
"image": "$image",
"name": "nsenter",
"stdin": true,
"stdinOnce": true,
"tty": true,
"command": [ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", "bash", "-l" ]
}
]
}
}
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
echo "spawning \"$pod\" on \"$node\""
kubectl run --rm --image "$image" --overrides="$overrides" -ti "$pod" $generator $context $kubeconfig $namespace