Skip to content

Drops sed and bash dependencies, add version #31

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 2 commits into from
Jul 8, 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
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