k9s/plugins/k3d_root_shell.yaml

24 lines
1.2 KiB
YAML

plugins:
# 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 "<<K9s-Root-Shell>> 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"