1- #! /usr/bin/env bash
1+ #! /usr/bin/env sh
22set -e
33
44kubectl=kubectl
5+ version=1.5.0
56generator=" "
67node=" "
78nodefaultctx=0
89nodefaultns=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
1313else
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 "
6973[ " $nodefaultns " = 1 ] || kubectl=" $kubectl --namespace=$( ${kubectl} config view --minify --output ' jsonpath={.contexts..namespace}' ) "
7074
7175if [ $# -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 ]"
7483else
75- cmd+=( bash -l)
84+ cmd= " $cmd \" bash\" , \" -l \" ] "
7685fi
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
8487if [ -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": [
136139trap " EC=\$ ?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$ EC" EXIT INT TERM
137140
138141echo " 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