From 8b4af9488ab054c11452d1bcb2932f7474b52693 Mon Sep 17 00:00:00 2001 From: jmcshane Date: Fri, 22 Nov 2024 12:01:40 -0500 Subject: [PATCH 1/2] Add flags for netns attach --- README.md | 3 +++ kubectl-node_shell | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ca7a8e..a5ca948 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ kubectl node-shell --image # Use X-mode (mount /host, and do not enter host namespace) kubectl node-shell -x +# Skip specific namespace types to enter, choose any of ipc, mount, pid, net, uts +kubectl node-shell --no-ipc + # Execute custom command kubectl node-shell -- echo 123 diff --git a/kubectl-node_shell b/kubectl-node_shell index 1ce2983..1bc65df 100755 --- a/kubectl-node_shell +++ b/kubectl-node_shell @@ -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 @@ -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 @@ -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 &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 From 8b7002928b9d8cbe1eb8f0f758dd0f6307ca1f4b Mon Sep 17 00:00:00 2001 From: James McShane Date: Mon, 25 Nov 2024 12:32:28 -0500 Subject: [PATCH 2/2] Update kubectl-node_shell --- kubectl-node_shell | 2 -- 1 file changed, 2 deletions(-) diff --git a/kubectl-node_shell b/kubectl-node_shell index 1bc65df..5c24304 100755 --- a/kubectl-node_shell +++ b/kubectl-node_shell @@ -233,6 +233,4 @@ 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