-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube-nodeshell.sh
executable file
·158 lines (144 loc) · 3.31 KB
/
kube-nodeshell.sh
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
#!/bin/bash
set -e
project_path=$(
cd $(dirname $0)
pwd
)
project_name=$(basename $0)
kubectl=kubectl
node=""
install=0
uninstall=0
temporary=0
function help() {
echo "Usage:"
echo "${project_name} {node} [options]"
echo " -h, --help: Print help"
echo " -n, --namespace: The namespace scope for this CLI request"
echo " -i, --install: If true, install the nodeshell daemonset"
echo " -u, --uninstall: If true, uninstall the nodeshell daemonset"
echo " -t: If true, create a temporary nodeshell pod and exec"
echo " --context: Context of kubeconfig"
echo " --kubeconfig: Path to kubeconfig file"
exit 0
}
function install_ds() {
ds_file="https://raw.githubusercontent.com/qingwave/kube-nodeshell/main/kube-nodeshell.yaml"
$kubectl apply -f $ds_file
}
function uninstall_ds() {
$kubectl delete ds kube-nodeshell
}
function exec_temporary_pod() {
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", "bash"]'
overrides="$(
cat <<EOT
{
"spec": {
"enableServiceLinks": false,
"nodeName": "$node",
"hostPID": true,
"hostNetwork": true,
"containers": [
{
"securityContext": {
"privileged": true
},
"image": "alpine",
"name": "nsenter",
"stdin": true,
"stdinOnce": true,
"tty": true,
"command": $cmd
}
],
"tolerations": [
{
"operator": "Exists"
}
]
}
}
EOT
)"
pod="kube-nodeshell-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)"
$kubectl run --image=alpine --restart=Never --rm --overrides="$overrides" -it $pod
}
function exec_daemonset_pod() {
pod=$($kubectl get po --field-selector spec.nodeName=${node} -l app=kube-nodeshell -ojsonpath='{.items[?(@.status.phase=="Running")].metadata.name}')
if [ -z $pod ]; then
echo "failed to get pod in node/$node, please check daemonset kube-nodeshell status"
exit
fi
$kubectl exec -it $pod -- bash
}
while [ $# -gt 0 ]; do
key="$1"
case $key in
-h | --help)
help
;;
--kubeconfig)
kubectl="$kubectl --kubeconfig $2"
shift
shift
;;
--context)
kubectl="$kubectl --context $2"
shift
shift
;;
-n | --namespace)
kubectl="$kubectl --namespace $2"
shift
shift
;;
-i | --install)
install=1
shift
;;
-u | --uninstall)
uninstall=1
shift
;;
-t)
temporary=1
shift
;;
*)
if [ -z "$node" ]; then
node="$1"
shift
else
echo "invalid parameter $key"
exit 1
fi
;;
esac
done
if [ ${uninstall} -eq 1 ]; then
uninstall_ds
echo "success uninstall kube-nodeshell"
exit 0
fi
if [ ${install} -eq 1 ]; then
install_ds
echo "success install kube-nodeshell"
exit 0
fi
if [[ -z "$node" ]]; then
echo "node is missing"
help
fi
kubectl get no $node > /dev/null
if [ "${temporary}" -eq 0 ]; then
ds=$($kubectl get ds -l app=kube-nodeshell -ojsonpath='{.items[*].metadata.name}')
if [ -z $ds ]; then
temporary=1
fi
fi
if [ "${temporary}" -eq 1 ]; then
exec_temporary_pod
else
exec_daemonset_pod
fi