Skip to content

Commit ddac375

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

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
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
@@ -339,7 +339,7 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
339339
return d.client.NewContainer(
340340
ctxWithTimeout,
341341
containerConfig.ContainerName,
342-
containerd.WithRuntime(d.config.ContainerdRuntime, nil),
342+
buildRuntime(d.config.ContainerdRuntime, config.Runtime),
343343
containerd.WithNewSnapshot(containerConfig.ContainerSnapshotName, containerConfig.Image),
344344
containerd.WithNewSpec(opts...),
345345
)

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

@@ -99,3 +103,30 @@ func WithMemoryLimits(soft, hard int64) oci.SpecOpts {
99103
return nil
100104
}
101105
}
106+
107+
// buildRuntime sets the container runtime e.g. runc or runsc (gVisor).
108+
func buildRuntime(pluginRuntime, jobRuntime string) containerd.NewContainerOpts {
109+
var (
110+
runcOpts runcoptions.Options
111+
runtimeOpts interface{} = &runcOpts
112+
)
113+
114+
// plugin.RuntimeRuncV2 = io.containerd.runc.v2
115+
runtime := plugin.RuntimeRuncV2
116+
117+
if jobRuntime != "" {
118+
if strings.HasPrefix(jobRuntime, "io.containerd.runc.") {
119+
runtime = jobRuntime
120+
} else {
121+
runcOpts.BinaryName = jobRuntime
122+
}
123+
} else if pluginRuntime != "" {
124+
if strings.HasPrefix(pluginRuntime, "io.containerd.runc.") {
125+
runtime = pluginRuntime
126+
} else {
127+
runcOpts.BinaryName = pluginRuntime
128+
}
129+
}
130+
131+
return containerd.WithRuntime(runtime, runtimeOpts)
132+
}

example/agent.hcl

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ data_dir = "/tmp/nomad"
44
plugin "containerd-driver" {
55
config {
66
enabled = true
7-
containerd_runtime = "io.containerd.runc.v2"
87
stats_interval = "5s"
98
}
109
}

0 commit comments

Comments
 (0)