Skip to content

Commit f3c4525

Browse files
Fix issue #116 - allow relative paths in mounts. (#123)
* Fix issue #116 - allow relative paths in mounts. * Add test. Signed-off-by: Shishir Mahajan <[email protected]>
1 parent 16127eb commit f3c4525

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

containerd/containerd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
251251
return nil, fmt.Errorf("Options cannot be empty for mount type: %s. You need to atleast pass rbind and ro.", mount.Type)
252252
}
253253

254+
// Allow paths relative to $NOMAD_TASK_DIR.
255+
// More details: https://github.com/Roblox/nomad-driver-containerd/issues/116#issuecomment-983171458
256+
if mount.Type == "bind" && strings.HasPrefix(mount.Source, "local") {
257+
mount.Source = containerConfig.TaskDirSrc + mount.Source[5:]
258+
}
259+
254260
m := buildMountpoint(mount.Type, mount.Target, mount.Source, mount.Options)
255261
mounts = append(mounts, m)
256262
}

example/mosquitto.nomad

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
job "mosquitto" {
2+
datacenters = ["dc1"]
3+
4+
group "msq-group" {
5+
task "msq-task" {
6+
driver = "containerd-driver"
7+
8+
config {
9+
image = "ubuntu:16.04"
10+
command = "sleep"
11+
args = ["600s"]
12+
mounts = [
13+
{
14+
type = "bind"
15+
target = "/mosquitto/config/mosquitto.conf"
16+
source = "local/mosquitto.conf"
17+
options = ["rbind", "rw"]
18+
}
19+
]
20+
}
21+
22+
template {
23+
destination = "local/mosquitto.conf"
24+
data = <<EOF
25+
bind_address 0.0.0.0
26+
allow_anonymous true
27+
persistence true
28+
persistence_location /mosquitto/data/
29+
log_dest stdout
30+
EOF
31+
}
32+
33+
resources {
34+
cpu = 500
35+
memory = 256
36+
}
37+
}
38+
}
39+
}

tests/010-test-template-stanza.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
source $SRCDIR/utils.sh
4+
5+
job_name=mosquitto
6+
filename="/mosquitto/config/mosquitto.conf"
7+
8+
# test template stanza
9+
test_template_stanza_nomad_job() {
10+
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example
11+
12+
echo "INFO: Starting nomad $job_name job using nomad-driver-containerd."
13+
nomad job run -detach $job_name.nomad
14+
15+
# Even though $(nomad job status) reports job status as "running"
16+
# The actual container process might not be running yet.
17+
# We need to wait for actual container to start running before trying exec.
18+
echo "INFO: Wait for ${job_name} container to get into RUNNING state, before trying exec."
19+
is_container_active ${job_name} true
20+
21+
echo "INFO: Checking status of $job_name job."
22+
job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ')
23+
if [ "$job_status" != "running" ];then
24+
echo "ERROR: Error in getting ${job_name} job status."
25+
exit 1
26+
fi
27+
28+
# Check if bind mount exists.
29+
echo "INFO: Checking if bind mount was successful and $filename exists."
30+
nomad alloc exec -job $job_name cat $filename >/dev/null 2>&1
31+
rc=$?
32+
if [ $rc -ne 0 ]; then
33+
echo "ERROR: bind mount was unsuccessful. $filename does not exist."
34+
exit 1
35+
fi
36+
37+
echo "INFO: Stopping nomad ${job_name} job."
38+
nomad job stop -detach ${job_name}
39+
job_status=$(nomad job status -short ${job_name}|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ')
40+
if [ $job_status != "dead(stopped)" ];then
41+
echo "ERROR: Error in stopping ${job_name} job."
42+
exit 1
43+
fi
44+
45+
echo "INFO: purge nomad ${job_name} job."
46+
nomad job stop -detach -purge ${job_name}
47+
popd
48+
}
49+
50+
test_template_stanza_nomad_job
File renamed without changes.

0 commit comments

Comments
 (0)