Skip to content

Commit

Permalink
Add support for setting pids_limit in docker plugin config.
Browse files Browse the repository at this point in the history
Signed-off-by: Shishir Mahajan <smahajan@roblox.com>
  • Loading branch information
shishir-a412ed committed Nov 29, 2021
1 parent e608286 commit 7755d57
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions drivers/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ var (
hclspec.NewAttr("pull_activity_timeout", "string", false),
hclspec.NewLiteral(`"2m"`),
),
"pids_limit": hclspec.NewAttr("pids_limit", "number", false),
// disable_log_collection indicates whether docker driver should collect logs of docker
// task containers. If true, nomad doesn't start docker_logger/logmon processes
"disable_log_collection": hclspec.NewAttr("disable_log_collection", "bool", false),
Expand Down Expand Up @@ -623,6 +624,7 @@ type DriverConfig struct {
infraImagePullTimeoutDuration time.Duration `codec:"-"`
DisableLogCollection bool `codec:"disable_log_collection"`
PullActivityTimeout string `codec:"pull_activity_timeout"`
PidsLimit int64 `codec:"pids_limit"`
pullActivityTimeoutDuration time.Duration `codec:"-"`
ExtraLabels []string `codec:"extra_labels"`
Logging LoggingConfig `codec:"logging"`
Expand Down
17 changes: 16 additions & 1 deletion drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,21 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T

memory, memoryReservation := memoryLimits(driverConfig.MemoryHardLimit, task.Resources.NomadResources.Memory)

var pidsLimit int64

// Pids limit defined in Nomad plugin config. Defaults to 0 (Unlimited).
if d.config.PidsLimit > 0 {
pidsLimit = d.config.PidsLimit
}

// Override Nomad plugin config pids limit, by user defined pids limit.
if driverConfig.PidsLimit > 0 {
if d.config.PidsLimit > 0 && driverConfig.PidsLimit > d.config.PidsLimit {
return c, fmt.Errorf("pids_limit cannot be greater than nomad plugin config pids_limit: %d", d.config.PidsLimit)
}
pidsLimit = driverConfig.PidsLimit
}

hostConfig := &docker.HostConfig{
Memory: memory, // hard limit
MemoryReservation: memoryReservation, // soft limit
Expand All @@ -840,7 +855,7 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
StorageOpt: driverConfig.StorageOpt,
VolumeDriver: driverConfig.VolumeDriver,

PidsLimit: &driverConfig.PidsLimit,
PidsLimit: &pidsLimit,

Runtime: containerRuntime,
}
Expand Down
17 changes: 17 additions & 0 deletions drivers/docker/driver_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ func TestDockerDriver_authFromHelper(t *testing.T) {
require.Equal(t, "registry.local:5000", string(content))
}

func TestDockerDriver_PluginConfig_PidsLimit(t *testing.T) {
t.Parallel()

dh := dockerDriverHarness(t, nil)
driver := dh.Impl().(*Driver)
driver.config.PidsLimit = 5

task, cfg, ports := dockerTask(t)
defer freeport.Return(ports)
require.NoError(t, task.EncodeConcreteDriverConfig(cfg))

cfg.PidsLimit = 7
_, err := driver.createContainerConfig(task, cfg, "org/repo:0.1")
require.Error(t, err)
require.Contains(t, err.Error(), `pids_limit cannot be greater than nomad plugin config pids_limit`)
}

func TestDockerDriver_PidsLimit(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
Expand Down
4 changes: 4 additions & 0 deletions drivers/docker/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
fp.Attributes["driver.docker.privileged.enabled"] = pstructs.NewBoolAttribute(true)
}

if d.config.PidsLimit > 0 {
fp.Attributes["driver.docker.pids.limit"] = pstructs.NewIntAttribute(d.config.PidsLimit, "")
}

if d.config.Volumes.Enabled {
fp.Attributes["driver.docker.volumes.enabled"] = pstructs.NewBoolAttribute(true)
}
Expand Down
6 changes: 6 additions & 0 deletions website/content/docs/drivers/docker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ plugin "docker" {
from the Docker engine during an image pull within this timeframe, Nomad will
timeout the request that initiated the pull command. (Minimum of `1m`)

- `pids_limit` - An integer value (Defaults to unlimited) that specifies the pids
limit for all the docker containers running on that nomad client node. You can
override this limit by setting `pids_limit` in your job spec, however you can only
set `pids_limit` in your job spec which is less than or equal to `pids_limit`
defined in nomad client plugin config.

- `allow_caps` - A list of allowed Linux capabilities. Defaults to

```hcl
Expand Down

0 comments on commit 7755d57

Please sign in to comment.