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