Skip to content

Commit 122a92d

Browse files
committed
drop bash and sed dependencies
1 parent 6da560b commit 122a92d

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

kubectl-node_shell

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env sh
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 --)
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
1211
tty=true
1312
else
1413
tty=false
@@ -69,17 +68,16 @@ done
6968
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"
7069

7170
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 ]"
7478
else
75-
cmd+=(bash -l)
79+
cmd="$cmd\"bash\", \"-l\" ]"
7680
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 \' \")"
8381

8482
if [ -z "$node" ]; then
8583
echo "Please specify node name"
@@ -109,7 +107,7 @@ overrides="$(
109107
"stdin": true,
110108
"stdinOnce": true,
111109
"tty": $tty,
112-
"command": $entrypoint
110+
"command": $cmd
113111
}
114112
],
115113
"tolerations": [
@@ -127,6 +125,8 @@ overrides="$(
127125
EOT
128126
)"
129127

128+
echo "$overrides"
129+
130130
# Support Kubectl <1.18
131131
m=$(kubectl version --client -o yaml | awk -F'[ :"]+' '$2 == "minor" {print $3+0}')
132132
if [ "$m" -lt 18 ]; then
@@ -136,4 +136,4 @@ fi
136136
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM
137137

138138
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

Comments
 (0)