Skip to content

Commit 6db85a4

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

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

README.md

Lines changed: 11 additions & 2 deletions
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 |
@@ -106,6 +114,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
106114
| **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. |
107115
| **sysctl** | map[string]string | no | A key-value map of sysctl configurations to set to the containers on start. |
108116
| **readonly_rootfs** | bool | no | Container root filesystem will be read-only. |
117+
| **runtime** | string | no | A string representing a configured runtime to pass to containerd. This is equivalent to the `--runtime` argument in the docker CLI. |
109118
| **host_network** | bool | no | Enable host network. This is equivalent to `--net=host` in docker. |
110119
| **extra_hosts** | []string | no | A list of hosts, given as host:IP, to be added to /etc/hosts. |
111120
| **cap_add** | []string | no | Add individual capabilities. |

containerd/containerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
300300
return d.client.NewContainer(
301301
ctxWithTimeout,
302302
containerConfig.ContainerName,
303-
containerd.WithRuntime(d.config.ContainerdRuntime, nil),
303+
buildRuntime(d.config.ContainerdRuntime, config.Runtime),
304304
containerd.WithNewSnapshot(containerConfig.ContainerSnapshotName, containerConfig.Image),
305305
containerd.WithNewSpec(opts...),
306306
)

containerd/driver.go

Lines changed: 3 additions & 1 deletion
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),
@@ -115,6 +115,7 @@ var (
115115
"seccomp_profile": hclspec.NewAttr("seccomp_profile", "string", false),
116116
"sysctl": hclspec.NewAttr("sysctl", "list(map(string))", false),
117117
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
118+
"runtime": hclspec.NewAttr("runtime", "string", false),
118119
"host_network": hclspec.NewAttr("host_network", "bool", false),
119120
"auth": hclspec.NewBlock("auth", false, hclspec.NewObject(map[string]*hclspec.Spec{
120121
"username": hclspec.NewAttr("username", "string", false),
@@ -185,6 +186,7 @@ type TaskConfig struct {
185186
ImagePullTimeout string `codec:"image_pull_timeout"`
186187
ExtraHosts []string `codec:"extra_hosts"`
187188
Entrypoint []string `codec:"entrypoint"`
189+
Runtime string `codec:"runtime"`
188190
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
189191
HostNetwork bool `codec:"host_network"`
190192
Auth RegistryAuth `codec:"auth"`

containerd/utils.go

Lines changed: 31 additions & 0 deletions
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

@@ -85,3 +89,30 @@ func WithMemoryLimits(soft, hard int64) oci.SpecOpts {
8589
return nil
8690
}
8791
}
92+
93+
// buildRuntime sets the container runtime e.g. runc or runsc (gVisor).
94+
func buildRuntime(pluginRuntime, jobRuntime string) containerd.NewContainerOpts {
95+
var (
96+
runcOpts runcoptions.Options
97+
runtimeOpts interface{} = &runcOpts
98+
)
99+
100+
// plugin.RuntimeRuncV2 = io.containerd.runc.v2
101+
runtime := plugin.RuntimeRuncV2
102+
103+
if jobRuntime != "" {
104+
if strings.HasPrefix(jobRuntime, "io.containerd.runc.") {
105+
runtime = jobRuntime
106+
} else {
107+
runcOpts.BinaryName = jobRuntime
108+
}
109+
} else if pluginRuntime != "" {
110+
if strings.HasPrefix(pluginRuntime, "io.containerd.runc.") {
111+
runtime = pluginRuntime
112+
} else {
113+
runcOpts.BinaryName = pluginRuntime
114+
}
115+
}
116+
117+
return containerd.WithRuntime(runtime, runtimeOpts)
118+
}

example/agent.hcl

Lines changed: 0 additions & 1 deletion
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/010-test-allow-privileged.sh

Lines changed: 1 addition & 1 deletion
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)