From 445230640acd03ab643820fa497991b044ca8cdd Mon Sep 17 00:00:00 2001 From: tscuite <64051240+tscuite@users.noreply.github.com> Date: Sat, 19 Apr 2025 23:27:05 +0800 Subject: [PATCH] feat: add hostPathVolume (docker) (#3277) --- README.md | 22 +++++++++++++++++++ internal/config/shell_pod.go | 8 +++++++ internal/view/exec.go | 41 +++++++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 48c571e4..8877a811 100644 --- a/README.md +++ b/README.md @@ -470,6 +470,13 @@ You can now override the context portForward default address configuration by se memory: 100Mi # Enable TTY tty: true + hostPathVolume: + - name: docker-socket + # Mount the Docker socket into the shell pod + mountPath: /var/run/docker.sock + # The path on the host to mount + hostPath: /var/run/docker.sock + readOnly: true ``` --- @@ -518,6 +525,21 @@ k9s: portForwardAddress: localhost ``` +### Customizing the Shell Pod +You can also customize the shell pod by adding a `hostPathVolume` to your shell pod. This allows you to mount a local directory or file into the shell pod. For example, if you want to mount the Docker socket into the shell pod, you can do so as follows: +```yaml +k9s: + shellPod: + hostPathVolume: + - name: docker-socket + # Mount the Docker socket into the shell pod + mountPath: /var/run/docker.sock + # The path on the host to mount + hostPath: /var/run/docker.sock + readOnly: true +``` +This will mount the Docker socket into the shell pod at `/var/run/docker.sock` and make it read-only. You can also mount any other directory or file in a similar way. + --- ## Command Aliases diff --git a/internal/config/shell_pod.go b/internal/config/shell_pod.go index 43682bcd..9c97ab01 100644 --- a/internal/config/shell_pod.go +++ b/internal/config/shell_pod.go @@ -23,6 +23,14 @@ type ShellPod struct { ImagePullSecrets []v1.LocalObjectReference `json:"imagePullSecrets,omitempty" yaml:"imagePullSecrets,omitempty"` ImagePullPolicy v1.PullPolicy `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` TTY bool `json:"tty,omitempty" yaml:"tty,omitempty"` + HostPathVolume []hostPathVolume `json:"hostPathVolume,omitempty" yaml:"hostPathVolume,omitempty"` +} + +type hostPathVolume struct { + Name string `json:"name" yaml:"name"` + MountPath string `json:"mountPath" yaml:"mountPath"` + HostPath string `json:"hostPath" yaml:"hostPath"` + ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"` } // NewShellPod returns a new instance. diff --git a/internal/view/exec.go b/internal/view/exec.go index f3d98cd4..ee3f384f 100644 --- a/internal/view/exec.go +++ b/internal/view/exec.go @@ -477,13 +477,39 @@ func k9sShellPod(node string, cfg *config.ShellPod) *v1.Pod { Privileged: &priv, }, } + v := []v1.Volume{ + { + Name: "root-vol", + VolumeSource: v1.VolumeSource{ + HostPath: &v1.HostPathVolumeSource{ + Path: "/", + }, + }, + }, + } if len(cfg.Command) != 0 { c.Command = cfg.Command } if len(cfg.Args) > 0 { c.Args = cfg.Args } - + if len(cfg.HostPathVolume) > 0 { + for _, h := range cfg.HostPathVolume { + c.VolumeMounts = append(c.VolumeMounts, v1.VolumeMount{ + Name: h.Name, + MountPath: h.MountPath, + ReadOnly: h.ReadOnly, + }) + v = append(v, v1.Volume{ + Name: h.Name, + VolumeSource: v1.VolumeSource{ + HostPath: &v1.HostPathVolumeSource{ + Path: h.HostPath, + }, + }, + }) + } + } return &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: k9sShellPodName(), @@ -497,17 +523,8 @@ func k9sShellPod(node string, cfg *config.ShellPod) *v1.Pod { HostNetwork: true, ImagePullSecrets: cfg.ImagePullSecrets, TerminationGracePeriodSeconds: &grace, - Volumes: []v1.Volume{ - { - Name: "root-vol", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{ - Path: "/", - }, - }, - }, - }, - Containers: []v1.Container{c}, + Volumes: v, + Containers: []v1.Container{c}, Tolerations: []v1.Toleration{ { Operator: v1.TolerationOperator("Exists"),