Skip to content

Commit

Permalink
Merge pull request #78 from Roblox/entrypoint
Browse files Browse the repository at this point in the history
Add support for entrypoint.
  • Loading branch information
shishir-a412ed authored Mar 26, 2021
2 parents 7513ee0 + b390ff4 commit 0f1ac90
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
| **image** | string | yes | OCI image (docker is also OCI compatible) for your container. |
| **command** | string | no | Command to override command defined in the image. |
| **args** | []string | no | Arguments to the command. |
| **entrypoint** | []string | no | A string list overriding the image's entrypoint. |
| **cwd** | string | no | Specify the current working directory for your container process. If the directory does not exist, one will be created for you. |
| **privileged** | bool | no | Run container in privileged mode. Your container will have all linux capabilities when running in privileged mode. |
| **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`. |
Expand Down
17 changes: 13 additions & 4 deletions containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ func (d *Driver) pullImage(imageName string) (containerd.Image, error) {
}

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

// Command set by the user, to override entrypoint or cmd defined in the image.
// Entrypoint or Command set by the user, to override entrypoint or cmd defined in the image.
var args []string
if config.Command != "" {
args = append(args, config.Command)
} else if config.Entrypoint != nil && config.Entrypoint[0] != "" {
args = append(args, config.Entrypoint...)
}

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

var opts []oci.SpecOpts

opts = append(opts, oci.WithImageConfigArgs(containerConfig.Image, args))
if config.Entrypoint != nil {
// WithProcessArgs replaces the args on the generated spec.
opts = append(opts, oci.WithProcessArgs(args...))
} else {
// WithImageConfigArgs configures the spec to from the configuration of an Image
// with additional args that replaces the CMD of the image.
opts = append(opts, oci.WithImageConfigArgs(containerConfig.Image, args))
}

if !d.config.AllowPrivileged && config.Privileged {
return nil, fmt.Errorf("Running privileged jobs are not allowed. Set allow_privileged to true in plugin config to allow running privileged jobs.")
Expand Down
2 changes: 2 additions & 0 deletions containerd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var (
hclspec.NewLiteral("true"),
),
"extra_hosts": hclspec.NewAttr("extra_hosts", "list(string)", false),
"entrypoint": hclspec.NewAttr("entrypoint", "list(string)", false),
"seccomp": hclspec.NewAttr("seccomp", "bool", false),
"seccomp_profile": hclspec.NewAttr("seccomp_profile", "string", false),
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
Expand Down Expand Up @@ -161,6 +162,7 @@ type TaskConfig struct {
Privileged bool `codec:"privileged"`
HostDNS bool `codec:"host_dns"`
ExtraHosts []string `codec:"extra_hosts"`
Entrypoint []string `codec:"entrypoint"`
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
HostNetwork bool `codec:"host_network"`
Mounts []Mount `codec:"mounts"`
Expand Down
20 changes: 20 additions & 0 deletions example/entrypoint.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
job "entrypoint" {
datacenters = ["dc1"]

group "entrypoint-group" {
task "entrypoint-task" {
driver = "containerd-driver"

config {
image = "ubuntu:16.04"
entrypoint = ["/bin/echo"]
args = ["container1", "container2"]
}

resources {
cpu = 500
memory = 256
}
}
}
}
40 changes: 40 additions & 0 deletions tests/008-test-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

source $SRCDIR/utils.sh

job_name=entrypoint

test_entrypoint_nomad_job() {
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example

echo "INFO: Starting nomad $job_name job using nomad-driver-containerd."
nomad job run $job_name.nomad

# Even though $(nomad job status) reports job status as "running"
# The actual container process might not be running yet.
# We need to wait for actual container to start running before executing $(nomad alloc logs).
echo "INFO: Wait for ${job_name} container to get into RUNNING state."
is_container_active ${job_name} true

echo "INFO: Checking status of $job_name job."
job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ')
if [ "$job_status" != "running" ];then
echo "ERROR: Error in getting ${job_name} job status."
return 1
fi

output=$(nomad logs -job ${job_name})
for result in "container1" "container2" ; do
echo -e "$output" |grep "$result" &>/dev/null
if [ $? -ne 0 ];then
echo "ERROR: $result not found in the output."
return 1
fi
done

echo "INFO: purge nomad ${job_name} job."
nomad job stop -purge ${job_name}
popd
}

test_entrypoint_nomad_job
File renamed without changes.

0 comments on commit 0f1ac90

Please sign in to comment.