Skip to content

Commit 1e05da7

Browse files
authored
Merge pull request #31 from kvaps/osx-compatibility
Drops sed and bash dependencies, add version
2 parents 6da560b + e46e8a4 commit 1e05da7

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

kubectl-node_shell

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

44
kubectl=kubectl
5+
version=1.5.0
56
generator=""
67
node=""
78
nodefaultctx=0
89
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
1212
tty=true
1313
else
1414
tty=false
@@ -17,6 +17,10 @@ while [ $# -gt 0 ]; do
1717
key="$1"
1818

1919
case $key in
20+
-v | --version)
21+
echo "kubectl-node-shell $version"
22+
exit 0
23+
;;
2024
--context)
2125
nodefaultctx=1
2226
kubectl="$kubectl --context $2"
@@ -69,17 +73,16 @@ done
6973
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"
7074

7175
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 ]"
7483
else
75-
cmd+=(bash -l)
84+
cmd="$cmd\"bash\", \"-l\" ]"
7685
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 \' \")"
8386

8487
if [ -z "$node" ]; then
8588
echo "Please specify node name"
@@ -109,7 +112,7 @@ overrides="$(
109112
"stdin": true,
110113
"stdinOnce": true,
111114
"tty": $tty,
112-
"command": $entrypoint
115+
"command": $cmd
113116
}
114117
],
115118
"tolerations": [
@@ -136,4 +139,4 @@ fi
136139
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM
137140

138141
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

Comments
 (0)