Skip to content

Commit 6da560b

Browse files
joesuf4Joe Schaefer
andauthored
bug: cmdline args need jsonification; tty support needs adjustment (#27)
- switch shell to bash so we can use arrays and bulk element transforms - use [ -t 0 ] to baseline whether we need terminal support for the invocation - send human-interest commentary to stderr instead of stdout Co-authored-by: Joe Schaefer <[email protected]>
1 parent 7e4a10b commit 6da560b

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

kubectl-node_shell

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
22
set -e
33

44
kubectl=kubectl
55
generator=""
66
node=""
77
nodefaultctx=0
88
nodefaultns=0
9-
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", '
9+
cmd=(nsenter --target 1 --mount --uts --ipc --net --pid --)
1010
custom=false
11-
if ! [ -p /dev/stdin ] && ! [ -p /dev/stdout ]; then
11+
if [ -t 0 ]; then
1212
tty=true
1313
else
1414
tty=false
@@ -68,16 +68,18 @@ done
6868
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(${kubectl} config current-context)"
6969
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"
7070

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}" )
7874
else
79-
cmd="$cmd\"bash\", \"-l\" ]"
75+
cmd+=(bash -l)
8076
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 \' \")"
8183

8284
if [ -z "$node" ]; then
8385
echo "Please specify node name"
@@ -107,7 +109,7 @@ overrides="$(
107109
"stdin": true,
108110
"stdinOnce": true,
109111
"tty": $tty,
110-
"command": $cmd
112+
"command": $entrypoint
111113
}
112114
],
113115
"tolerations": [
@@ -131,7 +133,7 @@ if [ "$m" -lt 18 ]; then
131133
generator="--generator=run-pod/v1"
132134
fi
133135

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
135137

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

Comments
 (0)