Skip to content

Commit

Permalink
config: map host devices into container
Browse files Browse the repository at this point in the history
Fixes #41
  • Loading branch information
towe75 committed Jul 11, 2021
1 parent 67fa7c3 commit da23d9f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## UNRELEASED

FEATURES:
* config: Map host devices into container. [[GH-41](https://github.com/hashicorp/nomad-driver-podman/pull/41)]

BUG FIXES:
* log: Use error key context to log errors rather than Go err style. [[GH-126](https://github.com/hashicorp/nomad-driver-podman/pull/126)]

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ config {
}
```

* **devices** - (Optional) A list of `host-device[:container-device][:permissions]` definitions.
Each entry adds a host device to the container. Optional permissions can be used to specify device permissions, it is combination of r for read, w for write, and m for mknod(2). See podman documentation for more details.

```
config {
devices = [
"/dev/net/tun"
]
}
```

* **hostname** - (Optional) The hostname to assign to the container. When launching more than one of a task (using count) with this option set, every container the task starts will have the same hostname.

* **Forwarding and Exposing Ports** - (Optional) See [Docker Driver Configuration](https://www.nomadproject.io/docs/drivers/docker.html#forwarding-and-exposing-ports) for details.
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
"command": hclspec.NewAttr("command", "string", false),
"cap_add": hclspec.NewAttr("cap_add", "list(string)", false),
"cap_drop": hclspec.NewAttr("cap_drop", "list(string)", false),
"devices": hclspec.NewAttr("devices", "list(string)", false),
"entrypoint": hclspec.NewAttr("entrypoint", "string", false),
"working_dir": hclspec.NewAttr("working_dir", "string", false),
"hostname": hclspec.NewAttr("hostname", "string", false),
Expand Down Expand Up @@ -103,6 +104,7 @@ type TaskConfig struct {
CapAdd []string `codec:"cap_add"`
CapDrop []string `codec:"cap_drop"`
Command string `codec:"command"`
Devices []string `codec:"devices"`
Entrypoint string `codec:"entrypoint"`
WorkingDir string `codec:"working_dir"`
Hostname string `codec:"hostname"`
Expand Down
5 changes: 5 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
return nil, nil, err
}
createOpts.ContainerStorageConfig.Mounts = allMounts
allDevices := []spec.LinuxDevice{}
for _, device := range driverConfig.Devices {
allDevices = append(allDevices, spec.LinuxDevice{Path: device})
}
createOpts.ContainerStorageConfig.Devices = allDevices

// Resources config options
createOpts.ContainerResourceConfig.ResourceLimits = &spec.LinuxResources{
Expand Down
48 changes: 48 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,54 @@ func TestPodmanDriver_User(t *testing.T) {

}

func TestPodmanDriver_Device(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
}

taskCfg := newTaskConfig("", []string{
// print our username to stdout
"sh",
"-c",
"sleep 1; ls -l /dev/net/tun",
})

task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "device",
AllocID: uuid.Generate(),
Resources: createBasicResources(),
}
taskCfg.Devices = []string{"/dev/net/tun"}
require.NoError(t, task.EncodeConcreteDriverConfig(&taskCfg))

d := podmanDriverHarness(t, nil)
cleanup := d.MkAllocDir(task, true)
defer cleanup()

_, _, err := d.StartTask(task)
require.NoError(t, err)

defer d.DestroyTask(task.ID, true)

// Attempt to wait
waitCh, err := d.WaitTask(context.Background(), task.ID)
require.NoError(t, err)

select {
case res := <-waitCh:
// should have a exitcode=0 result
require.True(t, res.Successful())
case <-time.After(time.Duration(tu.TestMultiplier()*2) * time.Second):
t.Fatalf("Container did not exit in time")
}

// see if stdout was populated with the "whoami" output
tasklog := readLogfile(t, task)
require.Contains(t, tasklog, "dev/net/tun")

}

// test memory/swap options
func TestPodmanDriver_Swap(t *testing.T) {
if !tu.IsCI() {
Expand Down

0 comments on commit da23d9f

Please sign in to comment.