docker: default device.container_path to host_path (#16811)

* docker: default device.container_path to host_path

Matches docker cli behavior.

Fixes #16754
This commit is contained in:
Michael Schurter 2023-04-06 14:44:33 -07:00 committed by GitHub
parent 4b7cd0a651
commit a8b379f962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 23 deletions

3
.changelog/16811.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
driver/docker: Default `devices.container_path` to `devices.host_path` like Docker's CLI
```

View File

@ -504,6 +504,11 @@ func (d DockerDevice) toDockerDevice() (docker.Device, error) {
return dd, fmt.Errorf("host path must be set in configuration for devices") return dd, fmt.Errorf("host path must be set in configuration for devices")
} }
// Docker's CLI defaults to HostPath in this case. See #16754
if dd.PathInContainer == "" {
dd.PathInContainer = d.HostPath
}
if dd.CgroupPermissions == "" { if dd.CgroupPermissions == "" {
dd.CgroupPermissions = "rwm" dd.CgroupPermissions = "rwm"
} }

View File

@ -216,6 +216,7 @@ config {
devices = [ devices = [
{"host_path"="/dev/null", "container_path"="/tmp/container-null", cgroup_permissions="rwm"}, {"host_path"="/dev/null", "container_path"="/tmp/container-null", cgroup_permissions="rwm"},
{"host_path"="/dev/random", "container_path"="/tmp/container-random"}, {"host_path"="/dev/random", "container_path"="/tmp/container-random"},
{"host_path"="/dev/bus/usb"},
] ]
dns_search_domains = ["sub.example.com", "sub2.example.com"] dns_search_domains = ["sub.example.com", "sub2.example.com"]
dns_options = ["debug", "attempts:10"] dns_options = ["debug", "attempts:10"]
@ -372,6 +373,11 @@ config {
ContainerPath: "/tmp/container-random", ContainerPath: "/tmp/container-random",
CgroupPermissions: "", CgroupPermissions: "",
}, },
{
HostPath: "/dev/bus/usb",
ContainerPath: "",
CgroupPermissions: "",
},
}, },
DNSSearchDomains: []string{"sub.example.com", "sub2.example.com"}, DNSSearchDomains: []string{"sub.example.com", "sub2.example.com"},
DNSOptions: []string{"debug", "attempts:10"}, DNSOptions: []string{"debug", "attempts:10"},

View File

@ -1035,7 +1035,7 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
hostConfig.ShmSize = driverConfig.ShmSize hostConfig.ShmSize = driverConfig.ShmSize
} }
// Setup devices // Setup devices from Docker-specific config
for _, device := range driverConfig.Devices { for _, device := range driverConfig.Devices {
dd, err := device.toDockerDevice() dd, err := device.toDockerDevice()
if err != nil { if err != nil {
@ -1043,6 +1043,8 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
} }
hostConfig.Devices = append(hostConfig.Devices, dd) hostConfig.Devices = append(hostConfig.Devices, dd)
} }
// Setup devices from Nomad device plugins
for _, device := range task.Devices { for _, device := range task.Devices {
hostConfig.Devices = append(hostConfig.Devices, docker.Device{ hostConfig.Devices = append(hostConfig.Devices, docker.Device{
PathOnHost: device.HostPath, PathOnHost: device.HostPath,

View File

@ -2471,23 +2471,43 @@ func TestDockerDriver_Device_Success(t *testing.T) {
t.Skip("test device mounts only on linux") t.Skip("test device mounts only on linux")
} }
hostPath := "/dev/random" cases := []struct {
containerPath := "/dev/myrandom" Name string
perms := "rwm" Input DockerDevice
Expected docker.Device
expectedDevice := docker.Device{ }{
PathOnHost: hostPath, {
PathInContainer: containerPath, Name: "AllSet",
CgroupPermissions: perms, Input: DockerDevice{
} HostPath: "/dev/random",
config := DockerDevice{ ContainerPath: "/dev/hostrandom",
HostPath: hostPath, CgroupPermissions: "rwm",
ContainerPath: containerPath, },
Expected: docker.Device{
PathOnHost: "/dev/random",
PathInContainer: "/dev/hostrandom",
CgroupPermissions: "rwm",
},
},
{
Name: "OnlyHost",
Input: DockerDevice{
HostPath: "/dev/random",
},
Expected: docker.Device{
PathOnHost: "/dev/random",
PathInContainer: "/dev/random",
CgroupPermissions: "rwm",
},
},
} }
for i := range cases {
tc := cases[i]
t.Run(tc.Name, func(t *testing.T) {
task, cfg, _ := dockerTask(t) task, cfg, _ := dockerTask(t)
cfg.Devices = []DockerDevice{config} cfg.Devices = []DockerDevice{tc.Input}
require.NoError(t, task.EncodeConcreteDriverConfig(cfg)) require.NoError(t, task.EncodeConcreteDriverConfig(cfg))
client, driver, handle, cleanup := dockerSetup(t, task, nil) client, driver, handle, cleanup := dockerSetup(t, task, nil)
@ -2498,7 +2518,9 @@ func TestDockerDriver_Device_Success(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, container.HostConfig.Devices, "Expected one device") require.NotEmpty(t, container.HostConfig.Devices, "Expected one device")
require.Equal(t, expectedDevice, container.HostConfig.Devices[0], "Incorrect device ") require.Equal(t, tc.Expected, container.HostConfig.Devices[0], "Incorrect device ")
})
}
} }
func TestDockerDriver_Entrypoint(t *testing.T) { func TestDockerDriver_Entrypoint(t *testing.T) {