Skip to content

Commit

Permalink
Merge pull request #31 from kvaps/osx-compatibility
Browse files Browse the repository at this point in the history
Drops sed and bash dependencies, add version
  • Loading branch information
kvaps authored Jul 8, 2021
2 parents 6da560b + e46e8a4 commit 1e05da7
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions kubectl-node_shell
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
set -e

kubectl=kubectl
version=1.5.0
generator=""
node=""
nodefaultctx=0
nodefaultns=0
cmd=(nsenter --target 1 --mount --uts --ipc --net --pid --)
custom=false
if [ -t 0 ]; then
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"'
if ! [ -p /dev/stdin ] && ! [ -p /dev/stdout ]; then
tty=true
else
tty=false
Expand All @@ -17,6 +17,10 @@ while [ $# -gt 0 ]; do
key="$1"

case $key in
-v | --version)
echo "kubectl-node-shell $version"
exit 0
;;
--context)
nodefaultctx=1
kubectl="$kubectl --context $2"
Expand Down Expand Up @@ -69,17 +73,16 @@ done
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"

if [ $# -gt 0 ]; then
cmd+=( "${@//
/\\n}" )
while [ $# -gt 0 ]; do
cmd="$cmd, \"$(echo "$1" | \
awk '{gsub(/\\/,"\\\\");gsub(/"/,"\\\"");gsub(/$/,"\\n");printf last}{last=$0} END{gsub(/\\n/,"",last);printf last}' \
)\""
shift
done
cmd="$cmd ]"
else
cmd+=(bash -l)
cmd="$cmd\"bash\", \"-l\" ]"
fi
# translate embedded single-quotes to double-quotes, so the following line will work
cmd=( "${cmd[@]//\'/\"}" )

# jsonify(as an array) the argument list (mainly from the command line)
entrypoint="$(echo "['${cmd[@]/%/\', \'}']" | sed -e "s/' /'/g" \
-e "s/, '']\$/]/" -Ee "s/([\"\\])/\\\\\1/g" -e 's/\\\\n/\\n/g' | tr \' \")"

if [ -z "$node" ]; then
echo "Please specify node name"
Expand Down Expand Up @@ -109,7 +112,7 @@ overrides="$(
"stdin": true,
"stdinOnce": true,
"tty": $tty,
"command": $entrypoint
"command": $cmd
}
],
"tolerations": [
Expand All @@ -136,4 +139,4 @@ fi
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM

echo "spawning \"$pod\" on \"$node\"" >&2
$kubectl run --image "$image" --restart=Never --overrides="$overrides" $([ -t 0 ] && echo -t) -i "$pod" $generator
$kubectl run --image "$image" --restart=Never --overrides="$overrides" $([ "$tty" = true ] && echo -t) -i "$pod" $generator

0 comments on commit 1e05da7

Please sign in to comment.