1
- #! /usr/bin/env bash
1
+ #! /usr/bin/env sh
2
2
set -e
3
3
4
4
kubectl=kubectl
5
+ version=1.5.0
5
6
generator=" "
6
7
node=" "
7
8
nodefaultctx=0
8
9
nodefaultns=0
9
- cmd=(nsenter --target 1 --mount --uts --ipc --net --pid --)
10
- custom=false
11
- if [ -t 0 ]; then
10
+ cmd=' [ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"'
11
+ if ! [ -p /dev/stdin ] && ! [ -p /dev/stdout ]; then
12
12
tty=true
13
13
else
14
14
tty=false
@@ -17,6 +17,10 @@ while [ $# -gt 0 ]; do
17
17
key=" $1 "
18
18
19
19
case $key in
20
+ -v | --version)
21
+ echo " kubectl-node-shell $version "
22
+ exit 0
23
+ ;;
20
24
--context)
21
25
nodefaultctx=1
22
26
kubectl=" $kubectl --context $2 "
69
73
[ " $nodefaultns " = 1 ] || kubectl=" $kubectl --namespace=$( ${kubectl} config view --minify --output ' jsonpath={.contexts..namespace}' ) "
70
74
71
75
if [ $# -gt 0 ]; then
72
- cmd+=( " ${@//
73
- / \\ n}" )
76
+ while [ $# -gt 0 ]; do
77
+ cmd=" $cmd , \" $( echo " $1 " | \
78
+ awk ' {gsub(/\\/,"\\\\");gsub(/"/,"\\\"");gsub(/$/,"\\n");printf last}{last=$0} END{gsub(/\\n/,"",last);printf last}' \
79
+ ) \" "
80
+ shift
81
+ done
82
+ cmd=" $cmd ]"
74
83
else
75
- cmd+=( bash -l)
84
+ cmd= " $cmd \" bash\" , \" -l \" ] "
76
85
fi
77
- # translate embedded single-quotes to double-quotes, so the following line will work
78
- cmd=( " ${cmd[@]// \' / \" } " )
79
-
80
- # jsonify(as an array) the argument list (mainly from the command line)
81
- entrypoint=" $( echo " ['${cmd[@]/%/ \' , \' } ']" | sed -e " s/' /'/g" \
82
- -e " s/, '']\$ /]/" -Ee " s/([\"\\ ])/\\\\ \1/g" -e ' s/\\\\n/\\n/g' | tr \' \" ) "
83
86
84
87
if [ -z " $node " ]; then
85
88
echo " Please specify node name"
@@ -109,7 +112,7 @@ overrides="$(
109
112
"stdin": true,
110
113
"stdinOnce": true,
111
114
"tty": $tty ,
112
- "command": $entrypoint
115
+ "command": $cmd
113
116
}
114
117
],
115
118
"tolerations": [
136
139
trap " EC=\$ ?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$ EC" EXIT INT TERM
137
140
138
141
echo " spawning \" $pod \" on \" $node \" " >&2
139
- $kubectl run --image " $image " --restart=Never --overrides=" $overrides " $( [ -t 0 ] && echo -t) -i " $pod " $generator
142
+ $kubectl run --image " $image " --restart=Never --overrides=" $overrides " $( [ " $tty " = true ] && echo -t) -i " $pod " $generator
0 commit comments