docker: oom_score_adj support (#23297)

This commit is contained in:
Piotr Kazmierczak
2024-06-12 10:49:20 +02:00
committed by GitHub
parent 7f1665d326
commit 0e8a67f0e1
5 changed files with 50 additions and 2 deletions

View File

@@ -290,6 +290,13 @@ var (
hclspec.NewLiteral(`5`),
),
// oom_score_adj is the positive integer that can be used to mark the task as
// more likely to be OOM killed
"oom_score_adj": hclspec.NewDefault(
hclspec.NewAttr("oom_score_adj", "number", false),
hclspec.NewLiteral(`0`),
),
// the duration that the driver will wait for activity from the Docker engine during an image pull
// before canceling the request
"pull_activity_timeout": hclspec.NewDefault(
@@ -392,6 +399,7 @@ var (
"mounts": hclspec.NewBlockList("mounts", mountBodySpec),
"network_aliases": hclspec.NewAttr("network_aliases", "list(string)", false),
"network_mode": hclspec.NewAttr("network_mode", "string", false),
"oom_score_adj": hclspec.NewAttr("oom_score_adj", "number", false),
"runtime": hclspec.NewAttr("runtime", "string", false),
"pids_limit": hclspec.NewAttr("pids_limit", "number", false),
"pid_mode": hclspec.NewAttr("pid_mode", "string", false),
@@ -469,6 +477,7 @@ type TaskConfig struct {
Mounts []DockerMount `codec:"mount"`
NetworkAliases []string `codec:"network_aliases"`
NetworkMode string `codec:"network_mode"`
OOMScoreAdj int `codec:"oom_score_adj"`
Runtime string `codec:"runtime"`
PidsLimit int64 `codec:"pids_limit"`
PidMode string `codec:"pid_mode"`
@@ -660,6 +669,7 @@ type DriverConfig struct {
PullActivityTimeout string `codec:"pull_activity_timeout"`
PidsLimit int64 `codec:"pids_limit"`
pullActivityTimeoutDuration time.Duration `codec:"-"`
OOMScoreAdj int `codec:"oom_score_adj"`
ExtraLabels []string `codec:"extra_labels"`
Logging LoggingConfig `codec:"logging"`

View File

@@ -314,6 +314,7 @@ config {
]
network_aliases = ["redis"]
network_mode = "host"
oom_score_adj = 1000
pids_limit = 2000
pid_mode = "host"
ports = ["http", "https"]
@@ -475,6 +476,7 @@ config {
},
NetworkAliases: []string{"redis"},
NetworkMode: "host",
OOMScoreAdj: 1000,
PidsLimit: 2000,
PidMode: "host",
Ports: []string{"http", "https"},
@@ -720,6 +722,35 @@ func TestConfig_DriverConfig_ContainerExistsAttempts(t *testing.T) {
}
}
func TestConfig_DriverConfig_OOMScoreAdj(t *testing.T) {
ci.Parallel(t)
cases := []struct {
name string
config string
expected int
}{
{
name: "default",
config: `{}`,
expected: 0,
},
{
name: "set explicitly",
config: `{ oom_score_adj = 1001 }`,
expected: 1001,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var tc DriverConfig
hclutils.NewConfigParser(configSpec).ParseHCL(t, "config "+c.config, &tc)
must.Eq(t, c.expected, tc.OOMScoreAdj)
})
}
}
func TestConfig_DriverConfig_InfraImagePullTimeout(t *testing.T) {
ci.Parallel(t)

View File

@@ -990,8 +990,9 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
hostConfig := &docker.HostConfig{
// do not set cgroup parent anymore
Memory: memory, // hard limit
MemoryReservation: memoryReservation, // soft limit
Memory: memory, // hard limit
MemoryReservation: memoryReservation, // soft limit
OomScoreAdj: driverConfig.OOMScoreAdj, // ignored on platforms other than linux
CPUShares: task.Resources.LinuxResources.CPUShares,
CPUSetCPUs: task.Resources.LinuxResources.CpusetCpus,