From 8d56f64297a7bbe0b147003d02d095eb66e66ebe Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Wed, 23 Sep 2020 13:39:11 -0700 Subject: [PATCH] Bind mount ${NOMAD_ALLOC_DIR} into the container. --- containerd/containerd.go | 8 +++++++- containerd/driver.go | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/containerd/containerd.go b/containerd/containerd.go index 4530721..d87cd30 100644 --- a/containerd/containerd.go +++ b/containerd/containerd.go @@ -41,7 +41,7 @@ func (d *Driver) pullImage(imageName string) (containerd.Image, error) { return d.client.Pull(d.ctxContainerd, imageName, containerd.WithPullUnpack) } -func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath, secretsDir, taskDir string, env []string, config *TaskConfig) (containerd.Container, error) { +func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath, secretsDir, taskDir, allocDir string, env []string, config *TaskConfig) (containerd.Container, error) { if config.Command == "" && len(config.Args) > 0 { return nil, fmt.Errorf("Command is empty. Cannot set --args without --command.") } @@ -135,6 +135,12 @@ func (d *Driver) createContainer(image containerd.Image, containerName, containe mounts = append(mounts, taskMount) } + // Setup "/alloc" (NOMAD_ALLOC_DIR) in the container. + if allocDir != "" { + allocMount := buildMountpoint("bind", "/alloc", allocDir, []string{"rbind", "ro"}) + mounts = append(mounts, allocMount) + } + if len(mounts) > 0 { opts = append(opts, oci.WithMounts(mounts)) } diff --git a/containerd/driver.go b/containerd/driver.go index 92ae860..26d76f2 100644 --- a/containerd/driver.go +++ b/containerd/driver.go @@ -357,7 +357,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive // Setup environment variables. var env []string - var secretsDir, taskDir string + var secretsDir, taskDir, allocDir string for key, val := range cfg.Env { if skipOverride(key) { continue @@ -368,6 +368,9 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive if key == "NOMAD_TASK_DIR" { taskDir = val } + if key == "NOMAD_ALLOC_DIR" { + allocDir = val + } env = append(env, fmt.Sprintf("%s=%s", key, val)) } @@ -377,7 +380,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive netnsPath = cfg.NetworkIsolation.Path } - container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, secretsDir, taskDir, env, &driverConfig) + container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, secretsDir, taskDir, allocDir, env, &driverConfig) if err != nil { return nil, nil, fmt.Errorf("Error in creating container: %v", err) }