Skip to content

bug: cmdline args need jsonification; tty support needs adjustment #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions kubectl-node_shell
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
set -e

kubectl=kubectl
generator=""
node=""
nodefaultctx=0
nodefaultns=0
cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--", '
cmd=(nsenter --target 1 --mount --uts --ipc --net --pid --)
custom=false
if ! [ -p /dev/stdin ] && ! [ -p /dev/stdout ]; then
if [ -t 0 ]; then
tty=true
else
tty=false
Expand Down Expand Up @@ -68,16 +68,18 @@ done
[ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(${kubectl} config current-context)"
[ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')"

while [ $# -gt 0 ]; do
cmd="$cmd\"$1\", "
custom=true
shift
done
if [ "$custom" = true ]; then
cmd="$(echo "$cmd" | awk '{sub(/..$/,"",$0); print $0}') ]"
if [ $# -gt 0 ]; then
cmd+=( "${@//
/\\n}" )
else
cmd="$cmd\"bash\", \"-l\" ]"
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 @@ -107,7 +109,7 @@ overrides="$(
"stdin": true,
"stdinOnce": true,
"tty": $tty,
"command": $cmd
"command": $entrypoint
}
],
"tolerations": [
Expand All @@ -131,7 +133,7 @@ if [ "$m" -lt 18 ]; then
generator="--generator=run-pod/v1"
fi

trap "EC=\$?; $kubectl delete pod --wait=false $pod 2>/dev/null || true; exit \$EC" EXIT INT TERM
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM

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