feat: add hostPathVolume (docker) (#3277)

mine
tscuite 2025-04-19 23:27:05 +08:00 committed by GitHub
parent 5fe09d4f92
commit 445230640a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 12 deletions

View File

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

View File

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

View File

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