diff --git a/plugins/README.md b/plugins/README.md index 3067991a..78b3f170 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -2,10 +2,11 @@ K9s plugins extend the tool to provide additional functionality via actions to further help you observe or administer your Kubernetes clusters. -| Plugin-Name | Description | Available on Views | Shortcut | Kubectl plugin, external dependencies | -|-----------------|----------------------------------|--------------------|----------|---------------------------------------------------------------------------------------| -| log_stern.yml | View resource logs using stern | pods | Ctrl-l | | -| log_jq.yml | View resource logs using jq | pods | Ctrl-j | kubectl-plugins/kubectl-jq | -| job_suspend.yml | Suspends a running cronjob | cronjobs | Ctrl-s | | -| dive.yml | Dive image layers | containers | d | [Dive](https://github.com/wagoodman/dive) | -| get-all.yml | get all resources in a namespace | all | g | [Krew](https://krew.sigs.k8s.io/), [ketall](https://github.com/corneliusweig/ketall/) | +| Plugin-Name | Description | Available on Views | Shortcut | Kubectl plugin, external dependencies | +|--------------------|----------------------------------|--------------------|----------|---------------------------------------------------------------------------------------| +| log_stern.yml | View resource logs using stern | pods | Ctrl-l | | +| log_jq.yml | View resource logs using jq | pods | Ctrl-j | kubectl-plugins/kubectl-jq | +| job_suspend.yml | Suspends a running cronjob | cronjobs | Ctrl-s | | +| dive.yml | Dive image layers | containers | d | [Dive](https://github.com/wagoodman/dive) | +| k3d_root_shell.yml | Root shell to k3d container | containers | Shift-s | [jq](https://stedolan.github.io/jq/) | +| get-all.yml | get all resources in a namespace | all | g | [Krew](https://krew.sigs.k8s.io/), [ketall](https://github.com/corneliusweig/ketall/) | diff --git a/plugins/k3d_root_shell.yml b/plugins/k3d_root_shell.yml new file mode 100644 index 00000000..295c6808 --- /dev/null +++ b/plugins/k3d_root_shell.yml @@ -0,0 +1,23 @@ +plugin: + # Opens a shell to k3d container as root + k3d-root-shell: + shortCut: Shift-S + confirm: false + description: "Root Shell" + scopes: + - containers + command: bash + background: false + args: + - -c + - | + # Check dependencies + command -v jq >/dev/null || { echo -e "jq is not installed (https://stedolan.github.io/jq/)\nPress 'q' to close" | less; exit 1; } + # Extract node name and container ID + POD_DATA="$(kubectl get pod/$POD -o json --namespace $NAMESPACE --context $CONTEXT)" + # ${...} is used to prevent variable substitution by k9s (e.g. $POD_DATA) + NODE_NAME=$(echo "${POD_DATA}" | jq -r '.spec.nodeName') + CONTAINER_ID=$(echo "${POD_DATA}" | jq -r '.status.containerStatuses[] | select(.name == "$COL-NAME") | .containerID ' | grep -oP '(?<=containerd://).*') + echo "<> Pod: $NAMESPACE/$POD | Container: $COL-NAME (${NODE_NAME}/${CONTAINER_ID})" + # Credits for this approach to https://gist.github.com/mamiu/4944e10305bc1c3af84946b33237b0e9 + docker exec -it $NODE_NAME sh -c "runc --root /run/containerd/runc/k8s.io/ exec -t -u 0 ${CONTAINER_ID} sh"