Skip to content

Commit 6ada188

Browse files
Add support for --runtime.
Signed-off-by: Shishir Mahajan <[email protected]>
1 parent 6600111 commit 6ada188

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,25 @@ will launch the job.<br/>
7777

7878
More detailed instructions are in the [`example README.md`](https://github.com/Roblox/nomad-driver-containerd/tree/master/example)
7979

80-
## Supported options
80+
## Supported Options
8181

8282
**Driver Config**
8383

8484
| Option | Type | Required | Default | Description |
8585
| :---: | :---: | :---: | :---: | :--- |
8686
| **enabled** | bool | no | true | Enable/Disable task driver. |
87-
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
87+
| **containerd_runtime** | string | no | `io.containerd.runc.v2` | Runtime for containerd. |
8888
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
8989
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
9090

91+
## Supported Runtimes
92+
93+
Valid options for `containerd_runtime` (**Driver Config**).
94+
95+
- `io.containerd.runc.v1`: `runc` runtime that supports a single container.
96+
- `io.containerd.runc.v2` (Default): `runc` runtime that supports multiple containers per shim.
97+
- `io.containerd.runsc.v1`: `gVisor` is an OCI compliant container runtime which provides better security than `runc`. They achieve this by implementing a user space kernel written in go, which implements a substantial portion of the Linux system call interface. For more details, please check their [`official documentation`](https://gvisor.dev/docs/)
98+
9199
**Task Config**
92100

93101
| Option | Type | Required | Description |
@@ -105,6 +113,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
105113
| **seccomp_profile** | string | no | Path to custom seccomp profile. `seccomp` must be set to `true` in order to use `seccomp_profile`. The default `docker` seccomp profile found [`here`](https://github.com/moby/moby/blob/master/profiles/seccomp/default.json) can be used as a reference, and modified to create a custom seccomp profile. |
106114
| **sysctl** | map[string]string | no | A key-value map of sysctl configurations to set to the containers on start. |
107115
| **readonly_rootfs** | bool | no | Container root filesystem will be read-only. |
116+
| **runtime** | string | no | A string representing a configured runtime to pass to containerd. This is equivalent to the `--runtime` argument in the docker CLI. |
108117
| **host_network** | bool | no | Enable host network. This is equivalent to `--net=host` in docker. |
109118
| **extra_hosts** | []string | no | A list of hosts, given as host:IP, to be added to /etc/hosts. |
110119
| **cap_add** | []string | no | Add individual capabilities. |

containerd/containerd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
267267
return d.client.NewContainer(
268268
ctxWithTimeout,
269269
containerConfig.ContainerName,
270-
containerd.WithRuntime(d.config.ContainerdRuntime, nil),
270+
buildRuntime(d.config.ContainerdRuntime, config.Runtime),
271271
containerd.WithNewSnapshot(containerConfig.ContainerSnapshotName, containerConfig.Image),
272272
containerd.WithNewSpec(opts...),
273273
)

containerd/driver.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var (
7878
hclspec.NewAttr("enabled", "bool", false),
7979
hclspec.NewLiteral("true"),
8080
),
81-
"containerd_runtime": hclspec.NewAttr("containerd_runtime", "string", true),
81+
"containerd_runtime": hclspec.NewAttr("containerd_runtime", "string", false),
8282
"stats_interval": hclspec.NewAttr("stats_interval", "string", false),
8383
"allow_privileged": hclspec.NewDefault(
8484
hclspec.NewAttr("allow_privileged", "bool", false),
@@ -114,6 +114,7 @@ var (
114114
"seccomp_profile": hclspec.NewAttr("seccomp_profile", "string", false),
115115
"sysctl": hclspec.NewAttr("sysctl", "list(map(string))", false),
116116
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
117+
"runtime": hclspec.NewAttr("runtime", "string", false),
117118
"host_network": hclspec.NewAttr("host_network", "bool", false),
118119
"mounts": hclspec.NewBlockList("mounts", hclspec.NewObject(map[string]*hclspec.Spec{
119120
"type": hclspec.NewDefault(
@@ -173,6 +174,7 @@ type TaskConfig struct {
173174
ImagePullTimeout string `codec:"image_pull_timeout"`
174175
ExtraHosts []string `codec:"extra_hosts"`
175176
Entrypoint []string `codec:"entrypoint"`
177+
Runtime string `codec:"runtime"`
176178
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
177179
HostNetwork bool `codec:"host_network"`
178180
Mounts []Mount `codec:"mounts"`

containerd/utils.go

+31
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ package containerd
2020
import (
2121
"context"
2222
"os"
23+
"strings"
2324
"syscall"
2425

26+
"github.com/containerd/containerd"
2527
"github.com/containerd/containerd/containers"
2628
"github.com/containerd/containerd/oci"
29+
"github.com/containerd/containerd/plugin"
30+
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
2731
specs "github.com/opencontainers/runtime-spec/specs-go"
2832
)
2933

@@ -60,3 +64,30 @@ func WithSysctls(sysctls map[string]string) oci.SpecOpts {
6064
return nil
6165
}
6266
}
67+
68+
// buildRuntime sets the container runtime e.g. runc or runsc (gVisor).
69+
func buildRuntime(pluginRuntime, jobRuntime string) containerd.NewContainerOpts {
70+
var (
71+
runcOpts runcoptions.Options
72+
runtimeOpts interface{} = &runcOpts
73+
)
74+
75+
// plugin.RuntimeRuncV2 = io.containerd.runc.v2
76+
runtime := plugin.RuntimeRuncV2
77+
78+
if jobRuntime != "" {
79+
if strings.HasPrefix(jobRuntime, "io.containerd.runc.") {
80+
runtime = jobRuntime
81+
} else {
82+
runcOpts.BinaryName = jobRuntime
83+
}
84+
} else if pluginRuntime != "" {
85+
if strings.HasPrefix(pluginRuntime, "io.containerd.runc.") {
86+
runtime = pluginRuntime
87+
} else {
88+
runcOpts.BinaryName = pluginRuntime
89+
}
90+
}
91+
92+
return containerd.WithRuntime(runtime, runtimeOpts)
93+
}

example/agent.hcl

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ log_level = "INFO"
33
plugin "containerd-driver" {
44
config {
55
enabled = true
6-
containerd_runtime = "io.containerd.runc.v2"
76
stats_interval = "5s"
87
}
98
}

tests/009-test-allow-privileged.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test_allow_privileged() {
99

1010
cp agent.hcl agent.hcl.bkp
1111

12-
sed -i '8 i \ allow_privileged = false' agent.hcl
12+
sed -i '7 i \ allow_privileged = false' agent.hcl
1313
sudo systemctl restart nomad
1414
is_systemd_service_active "nomad.service" true
1515

0 commit comments

Comments
 (0)