Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add oom flag #934

Merged
merged 1 commit into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,14 @@ definitions:
type: "string"
OomScoreAdj:
type: "integer"
description: "An integer value containing the score given to the container in order to tune OOM killer preferences."
example: 500
description: |
An integer value containing the score given to the container in order to tune OOM killer preferences.
The range is in [-1000, 1000].
type: "integer"
format: "int"
x-nullable: false
minimum: -1000
maximum: 1000
PidMode:
type: "string"
description: |
Expand Down
25 changes: 25 additions & 0 deletions apis/types/host_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
flagSet.Int64Var(&c.memoryWmarkRatio, "memory-wmark-ratio", 0, "Represent this container's memory low water mark percentage, range in [0, 100]. The value of memory low water mark is memory.limit_in_bytes * MemoryWmarkRatio")
flagSet.Int64Var(&c.memoryExtra, "memory-extra", 0, "Represent container's memory high water mark percentage, range in [0, 100]")
flagSet.Int64Var(&c.memoryForceEmptyCtl, "memory-force-empty-ctl", 0, "Whether to reclaim page cache when deleting the cgroup of container")
flagSet.BoolVar(&c.oomKillDisable, "oom-kill-disable", false, "Disable OOM Killer")
flagSet.Int64Var(&c.oomScoreAdj, "oom-score-adj", -500, "Tune host's OOM preferences (-1000 to 1000)")

flagSet.StringVar(&c.name, "name", "", "Specify name of container")

Expand Down
17 changes: 17 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type container struct {
memoryExtra int64
memoryForceEmptyCtl int64
scheLatSwitch int64
oomKillDisable bool

devices []string
enableLxcfs bool
Expand All @@ -61,6 +62,7 @@ type container struct {
blkioDeviceWriteIOps ThrottleIOpsDevice
IntelRdtL3Cbm string
diskQuota []string
oomScoreAdj int64

cgroupParent string

Expand Down Expand Up @@ -115,6 +117,10 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
return nil, err
}

if err := validateOOMScore(c.oomScoreAdj); err != nil {
return nil, err
}

var networkMode string
if len(c.networks) == 0 {
networkMode = "bridge"
Expand Down Expand Up @@ -218,6 +224,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
MemoryExtra: &c.memoryExtra,
MemoryForceEmptyCtl: c.memoryForceEmptyCtl,
ScheLatSwitch: c.scheLatSwitch,
OomKillDisable: &c.oomKillDisable,

// blkio
BlkioWeight: c.blkioWeight,
Expand All @@ -243,6 +250,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
CapAdd: c.capAdd,
CapDrop: c.capDrop,
PortBindings: portBindings,
OomScoreAdj: c.oomScoreAdj,
},

NetworkingConfig: networkingConfig,
Expand Down Expand Up @@ -444,3 +452,12 @@ func parseDiskQuota(quotas []string) (map[string]string, error) {

return quotaMaps, nil
}

// validateOOMScore validates oom score
func validateOOMScore(score int64) error {
if score < -1000 || score > 1000 {
return fmt.Errorf("oom-score-adj should be in range [-1000, 1000]")
}

return nil
}
2 changes: 2 additions & 0 deletions daemon/mgr/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ var setupFunc = []SetupFunc{
setupProcessUser,
setupCap,
setupNoNewPrivileges,
setupOOMScoreAdj,

// cgroup
setupCgroupCPUShare,
setupCgroupCPUSet,
setupCgroupMemory,
setupCgroupMemorySwap,
setupCgroupMemorySwappiness,
setupDisableOOMKill,

// namespaces
setupUserNamespace,
Expand Down
12 changes: 12 additions & 0 deletions daemon/mgr/spec_cgroup_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ func setupCgroupMemorySwappiness(ctx context.Context, meta *ContainerMeta, spec
mem.Swappiness = &v
return nil
}

func setupDisableOOMKill(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {
s := spec.s
mem := getCgroupMemory(s)

var v bool
if meta.HostConfig.OomKillDisable != nil {
v = bool(*meta.HostConfig.OomKillDisable)
}
mem.DisableOOMKiller = &v
return nil
}
5 changes: 5 additions & 0 deletions daemon/mgr/spec_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func setupNoNewPrivileges(ctx context.Context, meta *ContainerMeta, spec *SpecWr
}

spec.s.Process.NoNewPrivileges = meta.NoNewPrivileges
return nil
}

func setupOOMScoreAdj(ctx context.Context, c *ContainerMeta, spec *SpecWrapper) (err error) {
v := int(c.HostConfig.OomScoreAdj)
spec.s.Process.OOMScoreAdj = &v
return nil
}
20 changes: 20 additions & 0 deletions test/api_container_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,23 @@ func (suite *APIContainerCreateSuite) TestAliOSOptions(c *check.C) {
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 201)
}

func (suite *APIContainerCreateSuite) TestCreateOOMOption(c *check.C) {
cname := "TestCreateOOMOption"
q := url.Values{}
q.Add("name", cname)
query := request.WithQuery(q)

obj := map[string]interface{}{
"Image": busyboxImage,
"HostConfig": map[string]interface{}{
"OomScoreAdj": 100,
"OomKillDisable": true,
},
}
body := request.WithJSONBody(obj)

resp, err := request.Post("/containers/create", query, body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 201)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It tested that the container creation works. Please test further to check if the ContainerMeta contains this field with the inspect API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be test in test/cli_create_test.go

}
18 changes: 18 additions & 0 deletions test/cli_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,21 @@ func (suite *PouchCreateSuite) TestCreateWithAliOSMemoryOptions(c *check.C) {
c.Assert(result.HostConfig.MemoryForceEmptyCtl, check.Equals, int64(1))
c.Assert(result.HostConfig.ScheLatSwitch, check.Equals, int64(1))
}

// TestCreateWithOOMOption tests creating container with oom options.
func (suite *PouchCreateSuite) TestCreateWithOOMOption(c *check.C) {
name := "TestCreateWithOOMOption"
oomScore := "100"

res := command.PouchRun("create", "--name", name, "--oom-score-adj", oomScore, "--oom-kill-disable", busyboxImage)
res.Assert(c, icmd.Success)

output := command.PouchRun("inspect", name).Stdout()

result := &types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
c.Assert(result.HostConfig.OomScoreAdj, check.Equals, int64(100))
c.Assert(*result.HostConfig.OomKillDisable, check.Equals, true)
}