Skip to content

Commit 5945e43

Browse files
Merge pull request #90 from notnoop/f-memory-oversubscription
Support Memory Oversubscription
2 parents a210ba6 + c5dd9b6 commit 5945e43

File tree

7 files changed

+361
-279
lines changed

7 files changed

+361
-279
lines changed

Vagrantfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ Vagrant.configure("2") do |config|
3535
rm -f go1.14.3.linux-amd64.tar.gz
3636
fi
3737
38-
# Install nomad-1.0.2
38+
# Install nomad-1.1.0
3939
if [ ! -f "/usr/bin/nomad" ]; then
40-
wget --quiet https://releases.hashicorp.com/nomad/1.0.2/nomad_1.0.2_linux_amd64.zip
41-
unzip nomad_1.0.2_linux_amd64.zip -d /usr/bin
40+
wget --quiet https://releases.hashicorp.com/nomad/1.1.0/nomad_1.1.0_linux_amd64.zip
41+
unzip nomad_1.1.0_linux_amd64.zip -d /usr/bin
4242
chmod +x /usr/bin/nomad
43-
rm -f nomad_1.0.2_linux_amd64.zip
43+
rm -f nomad_1.1.0_linux_amd64.zip
4444
fi
4545
4646
# Install containerd-1.3.4

containerd/containerd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type ContainerConfig struct {
4545
AllocDirDest string
4646
Env []string
4747
MemoryLimit int64
48+
MemoryHardLimit int64
4849
CPUShares int64
4950
}
5051

@@ -196,7 +197,7 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
196197
opts = append(opts, oci.WithEnv(containerConfig.Env))
197198

198199
// Set cgroups memory limit.
199-
opts = append(opts, oci.WithMemoryLimit(uint64(containerConfig.MemoryLimit)))
200+
opts = append(opts, WithMemoryLimits(containerConfig.MemoryLimit, containerConfig.MemoryHardLimit))
200201

201202
// Set CPU Shares.
202203
opts = append(opts, oci.WithCPUShares(uint64(containerConfig.CPUShares)))

containerd/driver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
454454

455455
// memory and cpu are coming from the resources stanza of the nomad job.
456456
// https://www.nomadproject.io/docs/job-specification/resources
457-
containerConfig.MemoryLimit = cfg.Resources.LinuxResources.MemoryLimitBytes
457+
containerConfig.MemoryLimit = cfg.Resources.NomadResources.Memory.MemoryMB * 1024 * 1024
458+
containerConfig.MemoryHardLimit = cfg.Resources.NomadResources.Memory.MemoryMaxMB * 1024 * 1024
458459
containerConfig.CPUShares = cfg.Resources.LinuxResources.CPUShares
459460

460461
container, err := d.createContainer(&containerConfig, &driverConfig)

containerd/utils.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,28 @@ func WithSysctls(sysctls map[string]string) oci.SpecOpts {
6060
return nil
6161
}
6262
}
63+
64+
// WithMemoryLimits accepts soft (`memory`) and hard (`memory_max`) limits as parameters and set the desired
65+
// limits. With `Nomad<1.1.0` releases, soft (`memory`) will act as a hard limit, and if the container process exceeds
66+
// that limit, it will be OOM'ed. With `Nomad>=1.1.0` releases, users can over-provision using `soft` and `hard`
67+
// limits. The container process will only get OOM'ed if the hard limit is exceeded.
68+
func WithMemoryLimits(soft, hard int64) oci.SpecOpts {
69+
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
70+
if s.Linux != nil {
71+
if s.Linux.Resources == nil {
72+
s.Linux.Resources = &specs.LinuxResources{}
73+
}
74+
if s.Linux.Resources.Memory == nil {
75+
s.Linux.Resources.Memory = &specs.LinuxMemory{}
76+
}
77+
78+
if hard > 0 {
79+
s.Linux.Resources.Memory.Limit = &hard
80+
s.Linux.Resources.Memory.Reservation = &soft
81+
} else {
82+
s.Linux.Resources.Memory.Limit = &soft
83+
}
84+
}
85+
return nil
86+
}
87+
}

go.mod

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,25 @@ module github.com/Roblox/nomad-driver-containerd
44
go 1.12
55

66
require (
7-
github.com/LK4D4/joincontext v0.0.0-20171026170139-1724345da6d5 // indirect
87
github.com/NVIDIA/gpu-monitoring-tools v0.0.0-20191126014920-0d8df858cca4 // indirect
9-
github.com/Sirupsen/logrus v0.0.0-00010101000000-000000000000 // indirect
10-
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
11-
github.com/appc/spec v0.8.11 // indirect
12-
github.com/checkpoint-restore/go-criu v0.0.0-20191125063657-fcdcd07065c5 // indirect
138
github.com/containerd/cgroups v0.0.0-20200609174450-80c669f4bad0
149
github.com/containerd/containerd v1.4.3
1510
github.com/containerd/go-cni v0.0.0-20191121212822-60d125212faf // indirect
1611
github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd
1712
github.com/containernetworking/plugins v0.8.3 // indirect
1813
github.com/coreos/go-iptables v0.4.3 // indirect
19-
github.com/coreos/go-semver v0.3.0 // indirect
20-
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
2114
github.com/docker/docker v17.12.0-ce-rc1.0.20200330121334-7f8b4b621b5d+incompatible
2215
github.com/docker/docker-credential-helpers v0.6.3 // indirect
23-
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
2416
github.com/docker/go-metrics v0.0.1 // indirect
2517
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
2618
github.com/gogo/googleapis v1.4.0 // indirect
27-
github.com/google/uuid v1.1.2 // indirect
28-
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 // indirect
2919
github.com/hashicorp/consul-template v0.25.1
3020
github.com/hashicorp/go-envparse v0.0.0-20190703193109-150b3a2a4611 // indirect
31-
github.com/hashicorp/go-hclog v0.12.0
32-
github.com/hashicorp/go-plugin v1.0.2-0.20191004171845-809113480b55
21+
github.com/hashicorp/go-hclog v0.14.1
3322
github.com/hashicorp/go-uuid v1.0.1
34-
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 // indirect
35-
github.com/hashicorp/nomad v1.0.2
36-
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b // indirect
37-
github.com/opencontainers/runtime-spec v1.0.3-0.20200728170252-4d89ac9fbff6
23+
github.com/hashicorp/nomad v1.1.0
24+
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d
3825
github.com/spf13/cobra v1.1.1
39-
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2 // indirect
40-
github.com/ugorji/go v1.1.7 // indirect
41-
github.com/vbatts/tar-split v0.11.1 // indirect
42-
go4.org v0.0.0-20191010144846-132d2879e1e9 // indirect
43-
google.golang.org/grpc v1.32.0 // indirect
44-
istio.io/gogo-genproto v0.0.0-20190124151557-6d926a6e6feb // indirect
4526
)
4627

4728
// use lower-case sirupsen
@@ -59,3 +40,6 @@ replace github.com/docker/distribution v2.7.1+incompatible => github.com/docker/
5940

6041
// fix the version of hashicorp/go-msgpack to 96ddbed8d05b
6142
replace github.com/hashicorp/go-msgpack => github.com/hashicorp/go-msgpack v0.0.0-20191101193846-96ddbed8d05b
43+
44+
// Workaround Nomad using an old version
45+
replace google.golang.org/api => google.golang.org/api v0.46.0

0 commit comments

Comments
 (0)