Skip to content
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

Add flags for netns attach #68

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ kubectl node-shell <node> --image <image>
# Use X-mode (mount /host, and do not enter host namespace)
kubectl node-shell -x <node>

# Skip specific namespace types to enter, choose any of ipc, mount, pid, net, uts
kubectl node-shell <node> --no-ipc

# Execute custom command
kubectl node-shell <node> -- echo 123

Expand Down
46 changes: 45 additions & 1 deletion kubectl-node_shell
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ x_mode=0
labels="${KUBECTL_NODE_SHELL_LABELS}"
pod_running_timeout="${KUBECTL_NODE_SHELL_POD_RUNNING_TIMEOUT:-1m}"
custom_image=""
use_ipc=true
use_mount=true
use_pid=true
use_net=true
use_uts=true

if [ -t 0 ]; then
tty=true
Expand Down Expand Up @@ -71,6 +76,26 @@ while [ $# -gt 0 ]; do
shift
shift
;;
--no-ipc)
use_ipc=false
shift
;;
--no-mount)
use_mount=false
shift
;;
--no-pid)
use_pid=false
shift
;;
--no-net)
use_net=false
shift
;;
--no-uts)
use_uts=false
shift
;;
--)
shift
break
Expand Down Expand Up @@ -116,7 +141,24 @@ else # If the OS isn't windows, assume linux
image="${custom_image:-${KUBECTL_NODE_SHELL_IMAGE:-$default_image}}"
name="nsenter"
pod="${name}-$(env LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 6)"
cmd_start='"nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid"'
cmd_start='"nsenter", "--target", "1"'
# , "--mount", "--uts", "--ipc", "--net", "--pid"
if [ "$use_mount" = true ]; then
cmd_start="${cmd_start}, \"--mount\""
fi
if [ "$use_uts" = true ]; then
cmd_start="${cmd_start}, \"--uts\""
fi
if [ "$use_ipc" = true ]; then
cmd_start="${cmd_start}, \"--ipc\""
fi
if [ "$use_net" = true ]; then
cmd_start="${cmd_start}, \"--net\""
fi
if [ "$use_pid" = true ]; then
cmd_start="${cmd_start}, \"--pid\""
fi

cmd_arg_prefix=', "--"'
cmd_default=', "bash", "-l"'
security_context='{"privileged":true}'
Expand Down Expand Up @@ -191,4 +233,6 @@ fi
trap "EC=\$?; $kubectl delete pod --wait=false $pod >&2 || true; exit \$EC" EXIT INT TERM

echo "spawning \"$pod\" on \"$node\"" >&2

echo $overrides
$kubectl run --image "$image" --restart=Never --overrides="$overrides" --labels="$labels" --pod-running-timeout="$pod_running_timeout" $([ "$tty" = true ] && echo -t) -i "$pod" $generator