diff --git a/README.md b/README.md index 085b3e5..855afc1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # kubectl node-shell *(formerly known as **kubectl-enter**)* -Start a root shell in the node's host OS running. +Start a root shell in the node's host OS running. Uses an alpine pod with nsenter for Linux nodes and a [HostProcess pod](https://kubernetes.io/docs/tasks/configure-pod-container/create-hostprocess-pod/) with PowerShell for Windows nodes. ![demo](https://gist.githubusercontent.com/kvaps/2e3d77975a844654ec297893e21a0829/raw/c778a8405ff8c686e4e807a97e9721b423e7208f/kubectl-node-shell.gif) diff --git a/kubectl-node_shell b/kubectl-node_shell index ac43a54..3739adf 100755 --- a/kubectl-node_shell +++ b/kubectl-node_shell @@ -2,12 +2,15 @@ set -e kubectl=kubectl -version=1.5.5 +version=1.7.0 generator="" node="" nodefaultctx=0 nodefaultns=0 -cmd='[ "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid", "--"' +container_cpu="${KUBECTL_NODE_SHELL_POD_CPU:-100m}" +container_memory="${KUBECTL_NODE_SHELL_POD_MEMORY:-256Mi}" +labels="${KUBECTL_NODE_SHELL_LABELS}" + if [ -t 0 ]; then tty=true else @@ -68,11 +71,42 @@ while [ $# -gt 0 ]; do esac done +if [ -z "$node" ]; then + echo "Please specify node name" + exit 1 +fi + # Set the default context and namespace to avoid situations where the user switch them during the build process [ "$nodefaultctx" = 1 ] || kubectl="$kubectl --context=$(${kubectl} config current-context)" [ "$nodefaultns" = 1 ] || kubectl="$kubectl --namespace=$(${kubectl} config view --minify --output 'jsonpath={.contexts..namespace}')" +# Check the node and retrieve the node OS label +os="$($kubectl get node $node -o jsonpath="{.metadata.labels.kubernetes\.io/os}" || exit 1)" + +# Set pod configuration per operating system +if [ "$os" = "windows" ]; then + image="${KUBECTL_NODE_SHELL_IMAGE_WINDOWS:-mcr.microsoft.com/powershell}" + name="pwsh" + pod="${name}-$(env LC_ALL=C tr -dc a-z0-9 /dev/null || exit 1 - -container_cpu="${KUBECTL_NODE_SHELL_POD_CPU:-100m}" -container_memory="${KUBECTL_NODE_SHELL_POD_MEMORY:-256Mi}" -labels="${KUBECTL_NODE_SHELL_LABELS}" - overrides="$( - cat <