1
- #! /usr/bin/env sh
1
+ #! /usr/bin/env bash
2
2
set -e
3
3
4
4
kubectl=kubectl
5
5
generator=" "
6
6
node=" "
7
7
nodefaultctx=0
8
8
nodefaultns=0
9
- cmd=' [ " nsenter", " --target", "1", " --mount", " --uts", " --ipc", " --net", " --pid", "--", '
9
+ cmd=( nsenter --target 1 --mount --uts --ipc --net --pid --)
10
10
custom=false
11
- if ! [ -p /dev/stdin ] && ! [ -p /dev/stdout ]; then
11
+ if [ -t 0 ]; then
12
12
tty=true
13
13
else
14
14
tty=false
68
68
[ " $nodefaultctx " = 1 ] || kubectl=" $kubectl --context=$( ${kubectl} config current-context) "
69
69
[ " $nodefaultns " = 1 ] || kubectl=" $kubectl --namespace=$( ${kubectl} config view --minify --output ' jsonpath={.contexts..namespace}' ) "
70
70
71
- while [ $# -gt 0 ]; do
72
- cmd=" $cmd \" $1 \" , "
73
- custom=true
74
- shift
75
- done
76
- if [ " $custom " = true ]; then
77
- cmd=" $( echo " $cmd " | awk ' {sub(/..$/,"",$0); print $0}' ) ]"
71
+ if [ $# -gt 0 ]; then
72
+ cmd+=( " ${@//
73
+ / \\ n}" )
78
74
else
79
- cmd= " $cmd \" bash\" , \" -l \" ] "
75
+ cmd+=( bash -l)
80
76
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 \' \" ) "
81
83
82
84
if [ -z " $node " ]; then
83
85
echo " Please specify node name"
@@ -107,7 +109,7 @@ overrides="$(
107
109
"stdin": true,
108
110
"stdinOnce": true,
109
111
"tty": $tty ,
110
- "command": $cmd
112
+ "command": $entrypoint
111
113
}
112
114
],
113
115
"tolerations": [
@@ -131,7 +133,7 @@ if [ "$m" -lt 18 ]; then
131
133
generator=" --generator=run-pod/v1"
132
134
fi
133
135
134
- trap " EC=\$ ?; $kubectl delete pod --wait=false $pod 2>/dev/null || true; exit \$ EC" EXIT INT TERM
136
+ trap " EC=\$ ?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$ EC" EXIT INT TERM
135
137
136
- echo " spawning \" $pod \" on \" $node \" "
137
- $kubectl run --image " $image " --restart=Never --overrides=" $overrides " -ti " $pod " $generator
138
+ echo " spawning \" $pod \" on \" $node \" " >&2
139
+ $kubectl run --image " $image " --restart=Never --overrides=" $overrides " $( [ -t 0 ] && echo -t ) -i " $pod " $generator
0 commit comments