Skip to content

Commit 6766a8c

Browse files
Add support for --runtime.
Signed-off-by: Shishir Mahajan <[email protected]>
1 parent 1c443d6 commit 6766a8c

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
@@ -79,18 +79,26 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
7979

8080
To interact with `images` and `containers` directly, you can use [`nerdctl`](https://github.com/containerd/nerdctl) which is a docker compatible CLI for `containerd`. `nerdctl` is already installed in the vagrant VM at `/usr/local/bin`.
8181

82-
## Supported options
82+
## Supported Options
8383

8484
**Driver Config**
8585

8686
| Option | Type | Required | Default | Description |
8787
| :---: | :---: | :---: | :---: | :--- |
8888
| **enabled** | bool | no | true | Enable/Disable task driver. |
89-
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
89+
| **containerd_runtime** | string | no | `io.containerd.runc.v2` | Runtime for containerd. |
9090
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
9191
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
9292
| **auth** | block | no | N/A | Provide authentication for a private registry. See [Authentication](#authentication-private-registry) for more details. |
9393

94+
## Supported Runtimes
95+
96+
Valid options for `containerd_runtime` (**Driver Config**).
97+
98+
- `io.containerd.runc.v1`: `runc` runtime that supports a single container.
99+
- `io.containerd.runc.v2` (Default): `runc` runtime that supports multiple containers per shim.
100+
- `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/)
101+
94102
**Task Config**
95103

96104
| Option | Type | Required | Description |
@@ -111,6 +119,7 @@ To interact with `images` and `containers` directly, you can use [`nerdctl`](htt
111119
| **shm_size** | string | no | Size of /dev/shm e.g. "128M" if you want 128 MB of /dev/shm. |
112120
| **sysctl** | map[string]string | no | A key-value map of sysctl configurations to set to the containers on start. |
113121
| **readonly_rootfs** | bool | no | Container root filesystem will be read-only. |
122+
| **runtime** | string | no | A string representing a configured runtime to pass to containerd. This is equivalent to the `--runtime` argument in the docker CLI. |
114123
| **host_network** | bool | no | Enable host network. This is equivalent to `--net=host` in docker. |
115124
| **extra_hosts** | []string | no | A list of hosts, given as host:IP, to be added to /etc/hosts. |
116125
| **cap_add** | []string | no | Add individual capabilities. |

containerd/containerd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
327327
return d.client.NewContainer(
328328
ctxWithTimeout,
329329
containerConfig.ContainerName,
330-
containerd.WithRuntime(d.config.ContainerdRuntime, nil),
330+
buildRuntime(d.config.ContainerdRuntime, config.Runtime),
331331
containerd.WithNewSnapshot(containerConfig.ContainerSnapshotName, containerConfig.Image),
332332
containerd.WithNewSpec(opts...),
333333
)

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),
@@ -121,6 +121,7 @@ var (
121121
"shm_size": hclspec.NewAttr("shm_size", "string", false),
122122
"sysctl": hclspec.NewAttr("sysctl", "list(map(string))", false),
123123
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
124+
"runtime": hclspec.NewAttr("runtime", "string", false),
124125
"host_network": hclspec.NewAttr("host_network", "bool", false),
125126
"auth": hclspec.NewBlock("auth", false, hclspec.NewObject(map[string]*hclspec.Spec{
126127
"username": hclspec.NewAttr("username", "string", true),
@@ -194,6 +195,7 @@ type TaskConfig struct {
194195
ImagePullTimeout string `codec:"image_pull_timeout"`
195196
ExtraHosts []string `codec:"extra_hosts"`
196197
Entrypoint []string `codec:"entrypoint"`
198+
Runtime string `codec:"runtime"`
197199
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
198200
HostNetwork bool `codec:"host_network"`
199201
Auth RegistryAuth `codec:"auth"`

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

@@ -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

-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/010-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)