Skip to content

Commit

Permalink
container_create: Set a minimum memory limit
Browse files Browse the repository at this point in the history
We set a minimum limit of 4MB. Lower values could result
in container failing to start.

Signed-off-by: Mrunal Patel <[email protected]>
  • Loading branch information
mrunalp committed Aug 7, 2018
1 parent 45f20dd commit dd46e3b
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 13 deletions.
12 changes: 11 additions & 1 deletion server/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const (
scopePrefix = "crio"
defaultCgroupfsParent = "/crio"
defaultSystemdParent = "system.slice"

// minMemoryLimit is the minimum memory that must be set for a container.
// A lower value would result in the container failing to start.
minMemoryLimit = 4194304
)

type orderedMounts []rspec.Mount
Expand Down Expand Up @@ -988,7 +992,13 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string,
specgen.SetLinuxResourcesCPUPeriod(uint64(resources.GetCpuPeriod()))
specgen.SetLinuxResourcesCPUQuota(resources.GetCpuQuota())
specgen.SetLinuxResourcesCPUShares(uint64(resources.GetCpuShares()))
specgen.SetLinuxResourcesMemoryLimit(resources.GetMemoryLimitInBytes())

memoryLimit := resources.GetMemoryLimitInBytes()
if memoryLimit != 0 && memoryLimit < minMemoryLimit {
return nil, fmt.Errorf("set memory limit %v too low; should be at least %v", memoryLimit, minMemoryLimit)
}

specgen.SetLinuxResourcesMemoryLimit(memoryLimit)
specgen.SetProcessOOMScoreAdj(int(resources.GetOomScoreAdj()))
specgen.SetLinuxResourcesCPUCpus(resources.GetCpusetCpus())
specgen.SetLinuxResourcesCPUMems(resources.GetCpusetMems())
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_by_imageid.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_hostport.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_logging.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_resolvconf.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_resolvconf_ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_seccomp.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_config_sleep.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_redis_default_mounts.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_redis_device.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"namespace_options": {
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/container_sleep.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
}
}
}
3 changes: 2 additions & 1 deletion test/testdata/template_container_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"cpu_period": 10000,
"cpu_quota": 20000,
"cpu_shares": 512,
"oom_score_adj": 30
"oom_score_adj": 30,
"memory_limit_in_bytes": 268435456
},
"security_context": {
"readonly_rootfs": false,
Expand Down

0 comments on commit dd46e3b

Please sign in to comment.