Plugin for opening a root shell to k3d containter (#1709)

* Plugin for opening a root shell to k3d containter

* Add namespace and context to kubectl call

Co-authored-by: Igor Iatsenko <igor.iatsenko@here.com>
mine
iserpent 2022-09-18 16:08:38 +02:00 committed by GitHub
parent cab718387c
commit ea18aec512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -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. 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 | | Plugin-Name | Description | Available on Views | Shortcut | Kubectl plugin, external dependencies |
|-----------------|----------------------------------|--------------------|----------|---------------------------------------------------------------------------------------| |--------------------|----------------------------------|--------------------|----------|---------------------------------------------------------------------------------------|
| log_stern.yml | View resource logs using stern | pods | Ctrl-l | | | 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 | | 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 | | | job_suspend.yml | Suspends a running cronjob | cronjobs | Ctrl-s | |
| dive.yml | Dive image layers | containers | d | [Dive](https://github.com/wagoodman/dive) | | 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/) | | 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/) |

View File

@ -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 "<<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"