Skip to content

Commit 8e8f5c5

Browse files
sundbryolljanat
authored andcommitted
TaskConfig: Add option for file_limit to set RLIMIT_NOFILE
Depending on the workload, the default resource limit (ulimit) for the max number of file descriptors per process may need to be raised, such as for database servers. Add a `file_limit` option to the task config which will allow the limit to be set per task. Signed-off-by: Ryan Sundberg <[email protected]>
1 parent 9f1daa6 commit 8e8f5c5

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ To interact with `images` and `containers` directly, you can use [`nerdctl`](htt
115115
| **privileged** | bool | no | Run container in privileged mode. Your container will have all linux capabilities when running in privileged mode. |
116116
| **pids_limit** | int64 | no | An integer value that specifies the pid limit for the container. Defaults to unlimited. |
117117
| **pid_mode** | string | no | `host` or not set (default). Set to `host` to share the PID namespace with the host. |
118+
| **file_limit** | int64 | no | An integer value that specifies the file descriptor ulimit for the container. Defaults to 1024 by containerd. |
118119
| **hostname** | string | no | The hostname to assign to the container. When launching more than one of a task (using `count`) with this option set, every container the task starts will have the same hostname. |
119120
| **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`. |
120121
| **seccomp** | bool | no | Enable default seccomp profile. List of [`allowed syscalls`](https://github.com/containerd/containerd/blob/master/contrib/seccomp/seccomp_default.go#L51-L395). |

containerd-driver.exe

35.3 MB
Binary file not shown.

containerd/containerd.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
etchosts "github.com/Roblox/nomad-driver-containerd/etchosts"
2727
"github.com/containerd/containerd"
28+
"github.com/containerd/containerd/containers"
2829
"github.com/containerd/containerd/cio"
2930
"github.com/containerd/containerd/contrib/seccomp"
3031
"github.com/containerd/containerd/oci"
@@ -93,6 +94,27 @@ func withResolver(creds CredentialsOpt) containerd.RemoteOpt {
9394
return containerd.WithResolver(resolver)
9495
}
9596

97+
func withFileLimit(maxOpenFiles uint64) oci.SpecOpts {
98+
return func(_ context.Context, _ oci.Client, _ *containers.Container, spec *oci.Spec) error {
99+
newRlimits := []specs.POSIXRlimit{{
100+
Type: "RLIMIT_NOFILE",
101+
Hard: maxOpenFiles,
102+
Soft: maxOpenFiles,
103+
}}
104+
105+
// Copy existing rlimits excluding previous RLIMIT_NOFILE
106+
for _, rlimit := range spec.Process.Rlimits {
107+
if rlimit.Type != "RLIMIT_NOFILE" {
108+
newRlimits = append(newRlimits, rlimit)
109+
}
110+
}
111+
112+
spec.Process.Rlimits = newRlimits
113+
114+
return nil
115+
}
116+
}
117+
96118
func (d *Driver) pullImage(imageName, imagePullTimeout string, auth *RegistryAuth) (containerd.Image, error) {
97119
pullTimeout, err := time.ParseDuration(imagePullTimeout)
98120
if err != nil {
@@ -167,6 +189,11 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
167189
}
168190
}
169191

192+
// Set the resource limit for open file descriptors
193+
if config.FileLimit > 0 {
194+
opts = append(opts, withFileLimit(uint64(config.FileLimit)))
195+
}
196+
170197
// Size of /dev/shm
171198
if len(config.ShmSize) > 0 {
172199
shmBytes, err := units.RAMInBytes(config.ShmSize)

containerd/driver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ var (
108108
"privileged": hclspec.NewAttr("privileged", "bool", false),
109109
"pids_limit": hclspec.NewAttr("pids_limit", "number", false),
110110
"pid_mode": hclspec.NewAttr("pid_mode", "string", false),
111+
"file_limit": hclspec.NewAttr("file_limit", "number", false),
111112
"hostname": hclspec.NewAttr("hostname", "string", false),
112113
"host_dns": hclspec.NewDefault(
113114
hclspec.NewAttr("host_dns", "bool", false),
@@ -194,6 +195,7 @@ type TaskConfig struct {
194195
Privileged bool `codec:"privileged"`
195196
PidsLimit int64 `codec:"pids_limit"`
196197
PidMode string `codec:"pid_mode"`
198+
FileLimit int64 `codec:"file_limit"`
197199
Hostname string `codec:"hostname"`
198200
HostDNS bool `codec:"host_dns"`
199201
ImagePullTimeout string `codec:"image_pull_timeout"`

0 commit comments

Comments
 (0)