Skip to content

Commit c79c4e4

Browse files
Add support for consul templates.
1 parent 6045772 commit c79c4e4

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

containerd/containerd.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (d *Driver) pullImage(imageName string) (containerd.Image, error) {
4141
return d.client.Pull(d.ctxContainerd, imageName, containerd.WithPullUnpack)
4242
}
4343

44-
func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath string, env []string, config *TaskConfig) (containerd.Container, error) {
44+
func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath, secretsDir, taskDir string, env []string, config *TaskConfig) (containerd.Container, error) {
4545
if config.Command == "" && len(config.Args) > 0 {
4646
return nil, fmt.Errorf("Command is empty. Cannot set --args without --command.")
4747
}
@@ -119,14 +119,22 @@ func (d *Driver) createContainer(image containerd.Image, containerName, containe
119119
return nil, fmt.Errorf("Options cannot be empty for mount type: %s. You need to atleast pass rbind and ro.", mount.Type)
120120
}
121121

122-
m := specs.Mount{}
123-
m.Type = mount.Type
124-
m.Destination = mount.Target
125-
m.Source = mount.Source
126-
m.Options = mount.Options
122+
m := buildMountpoint(mount.Type, mount.Target, mount.Source, mount.Options)
127123
mounts = append(mounts, m)
128124
}
129125

126+
// Setup "/secrets" (NOMAD_SECRETS_DIR) in the container.
127+
if secretsDir != "" {
128+
secretsMount := buildMountpoint("bind", "/secrets", secretsDir, []string{"rbind", "ro"})
129+
mounts = append(mounts, secretsMount)
130+
}
131+
132+
// Setup "/local" (NOMAD_TASK_DIR) in the container.
133+
if taskDir != "" {
134+
taskMount := buildMountpoint("bind", "/local", taskDir, []string{"rbind", "ro"})
135+
mounts = append(mounts, taskMount)
136+
}
137+
130138
if len(mounts) > 0 {
131139
opts = append(opts, oci.WithMounts(mounts))
132140
}
@@ -150,6 +158,16 @@ func (d *Driver) createContainer(image containerd.Image, containerName, containe
150158
)
151159
}
152160

161+
// buildMountpoint builds the mount point for the container.
162+
func buildMountpoint(mountType, mountTarget, mountSource string, mountOptions []string) specs.Mount {
163+
m := specs.Mount{}
164+
m.Type = mountType
165+
m.Destination = mountTarget
166+
m.Source = mountSource
167+
m.Options = mountOptions
168+
return m
169+
}
170+
153171
func (d *Driver) loadContainer(id string) (containerd.Container, error) {
154172
return d.client.LoadContainer(d.ctxContainerd, id)
155173
}

containerd/driver.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,17 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
357357

358358
// Setup environment variables.
359359
var env []string
360+
var secretsDir, taskDir string
360361
for key, val := range cfg.Env {
361362
if skipOverride(key) {
362363
continue
363364
}
365+
if key == "NOMAD_SECRETS_DIR" {
366+
secretsDir = val
367+
}
368+
if key == "NOMAD_TASK_DIR" {
369+
taskDir = val
370+
}
364371
env = append(env, fmt.Sprintf("%s=%s", key, val))
365372
}
366373

@@ -370,7 +377,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
370377
netnsPath = cfg.NetworkIsolation.Path
371378
}
372379

373-
container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, env, &driverConfig)
380+
container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, secretsDir, taskDir, env, &driverConfig)
374381
if err != nil {
375382
return nil, nil, fmt.Errorf("Error in creating container: %v", err)
376383
}

0 commit comments

Comments
 (0)