Skip to content

Commit 0f1ac90

Browse files
Merge pull request #78 from Roblox/entrypoint
Add support for entrypoint.
2 parents 7513ee0 + b390ff4 commit 0f1ac90

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
9595
| **image** | string | yes | OCI image (docker is also OCI compatible) for your container. |
9696
| **command** | string | no | Command to override command defined in the image. |
9797
| **args** | []string | no | Arguments to the command. |
98+
| **entrypoint** | []string | no | A string list overriding the image's entrypoint. |
9899
| **cwd** | string | no | Specify the current working directory for your container process. If the directory does not exist, one will be created for you. |
99100
| **privileged** | bool | no | Run container in privileged mode. Your container will have all linux capabilities when running in privileged mode. |
100101
| **host_dns** | bool | no | Default (`true`). By default, a container launched using `containerd-driver` will use host `/etc/resolv.conf`. This is similar to [`docker behavior`](https://docs.docker.com/config/containers/container-networking/#dns-services). However, if you don't want to use host DNS, you can turn off this flag by setting `host_dns=false`. |

containerd/containerd.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ func (d *Driver) pullImage(imageName string) (containerd.Image, error) {
7676
}
7777

7878
func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskConfig) (containerd.Container, error) {
79-
if config.Command == "" && len(config.Args) > 0 {
80-
return nil, fmt.Errorf("Command is empty. Cannot set --args without --command.")
79+
if config.Command != "" && config.Entrypoint != nil {
80+
return nil, fmt.Errorf("Both command and entrypoint are set. Only one of them needs to be set.")
8181
}
8282

83-
// Command set by the user, to override entrypoint or cmd defined in the image.
83+
// Entrypoint or Command set by the user, to override entrypoint or cmd defined in the image.
8484
var args []string
8585
if config.Command != "" {
8686
args = append(args, config.Command)
87+
} else if config.Entrypoint != nil && config.Entrypoint[0] != "" {
88+
args = append(args, config.Entrypoint...)
8789
}
8890

8991
// Arguments to the command set by the user.
@@ -93,7 +95,14 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
9395

9496
var opts []oci.SpecOpts
9597

96-
opts = append(opts, oci.WithImageConfigArgs(containerConfig.Image, args))
98+
if config.Entrypoint != nil {
99+
// WithProcessArgs replaces the args on the generated spec.
100+
opts = append(opts, oci.WithProcessArgs(args...))
101+
} else {
102+
// WithImageConfigArgs configures the spec to from the configuration of an Image
103+
// with additional args that replaces the CMD of the image.
104+
opts = append(opts, oci.WithImageConfigArgs(containerConfig.Image, args))
105+
}
97106

98107
if !d.config.AllowPrivileged && config.Privileged {
99108
return nil, fmt.Errorf("Running privileged jobs are not allowed. Set allow_privileged to true in plugin config to allow running privileged jobs.")

containerd/driver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ var (
103103
hclspec.NewLiteral("true"),
104104
),
105105
"extra_hosts": hclspec.NewAttr("extra_hosts", "list(string)", false),
106+
"entrypoint": hclspec.NewAttr("entrypoint", "list(string)", false),
106107
"seccomp": hclspec.NewAttr("seccomp", "bool", false),
107108
"seccomp_profile": hclspec.NewAttr("seccomp_profile", "string", false),
108109
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
@@ -161,6 +162,7 @@ type TaskConfig struct {
161162
Privileged bool `codec:"privileged"`
162163
HostDNS bool `codec:"host_dns"`
163164
ExtraHosts []string `codec:"extra_hosts"`
165+
Entrypoint []string `codec:"entrypoint"`
164166
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
165167
HostNetwork bool `codec:"host_network"`
166168
Mounts []Mount `codec:"mounts"`

example/entrypoint.nomad

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
job "entrypoint" {
2+
datacenters = ["dc1"]
3+
4+
group "entrypoint-group" {
5+
task "entrypoint-task" {
6+
driver = "containerd-driver"
7+
8+
config {
9+
image = "ubuntu:16.04"
10+
entrypoint = ["/bin/echo"]
11+
args = ["container1", "container2"]
12+
}
13+
14+
resources {
15+
cpu = 500
16+
memory = 256
17+
}
18+
}
19+
}
20+
}

tests/008-test-entrypoint.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
source $SRCDIR/utils.sh
4+
5+
job_name=entrypoint
6+
7+
test_entrypoint_nomad_job() {
8+
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example
9+
10+
echo "INFO: Starting nomad $job_name job using nomad-driver-containerd."
11+
nomad job run $job_name.nomad
12+
13+
# Even though $(nomad job status) reports job status as "running"
14+
# The actual container process might not be running yet.
15+
# We need to wait for actual container to start running before executing $(nomad alloc logs).
16+
echo "INFO: Wait for ${job_name} container to get into RUNNING state."
17+
is_container_active ${job_name} true
18+
19+
echo "INFO: Checking status of $job_name job."
20+
job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ')
21+
if [ "$job_status" != "running" ];then
22+
echo "ERROR: Error in getting ${job_name} job status."
23+
return 1
24+
fi
25+
26+
output=$(nomad logs -job ${job_name})
27+
for result in "container1" "container2" ; do
28+
echo -e "$output" |grep "$result" &>/dev/null
29+
if [ $? -ne 0 ];then
30+
echo "ERROR: $result not found in the output."
31+
return 1
32+
fi
33+
done
34+
35+
echo "INFO: purge nomad ${job_name} job."
36+
nomad job stop -purge ${job_name}
37+
popd
38+
}
39+
40+
test_entrypoint_nomad_job
File renamed without changes.

0 commit comments

Comments
 (0)