Skip to content

Commit

Permalink
fix: make cpuquota, MemorySwappiness validation compatible with docker
Browse files Browse the repository at this point in the history
refactor: logCreateOption before validation, it enable developer to see what's wrong.

Signed-off-by: zhuangqh <zhuangqhc@gmail.com>
  • Loading branch information
zhuangqh committed Nov 19, 2018
1 parent 8acae57 commit 335c90c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 35 deletions.
5 changes: 3 additions & 2 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ func (s *Server) createContainer(ctx context.Context, rw http.ResponseWriter, re
if err := json.NewDecoder(reader).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("container", config)

// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("container", config)

name := req.FormValue("name")
//consider set specific id by url params
specificID := req.FormValue("specificId")
Expand Down
8 changes: 4 additions & 4 deletions apis/server/exec_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func (s *Server) createContainerExec(ctx context.Context, rw http.ResponseWriter
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
name := mux.Vars(req)["name"]

logCreateOptions("container exec for "+name, config)

// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

name := mux.Vars(req)["name"]

logCreateOptions("container exec for "+name, config)

id, err := s.ContainerMgr.CreateExec(ctx, name, config)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions apis/server/network_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func (s *Server) createNetwork(ctx context.Context, rw http.ResponseWriter, req
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("network", config)

// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("network", config)

network, err := s.NetworkMgr.Create(ctx, *config)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions apis/server/volume_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func (s *Server) createVolume(ctx context.Context, rw http.ResponseWriter, req *
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("volume", config)

// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

logCreateOptions("volume", config)

name := config.Name
driver := config.Driver
options := config.DriverOpts
Expand Down
5 changes: 2 additions & 3 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,6 @@ definitions:
Microseconds of CPU time that the container can get in a CPU period."
type: "integer"
format: "int64"
minimum: 1000
x-nullable: false
x-omitempty: false
CpuRealtimePeriod:
Expand Down Expand Up @@ -2660,10 +2659,10 @@ definitions:
x-nullable: false
x-omitempty: false
MemorySwappiness:
description: "Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100."
description: "Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. -1 is also accepted, as a legacy alias of 0."
type: "integer"
format: "int64"
minimum: 0
minimum: -1
maximum: 100
x-omitempty: false
NanoCpus:
Expand Down
22 changes: 2 additions & 20 deletions apis/types/resources.go

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

2 changes: 1 addition & 1 deletion daemon/mgr/container_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func validateResource(r *types.Resources, update bool) ([]string, error) {
r.MemorySwappiness = nil
}
if r.MemorySwappiness != nil && *r.MemorySwappiness != -1 && (*r.MemorySwappiness < 0 || *r.MemorySwappiness > 100) {
return warnings, fmt.Errorf("MemorySwappiness should in range [0, 100]")
return warnings, fmt.Errorf("MemorySwappiness should in range [0, 100] or -1 as a legacy alias of 0")
}
if r.OomKillDisable != nil && !cgroupInfo.Memory.OOMKillDisable {
logrus.Warn(OOMKillWarn)
Expand Down
2 changes: 1 addition & 1 deletion test/cli_run_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (suite *PouchRunMemorySuite) TestRunWithMemoryswappiness(c *check.C) {
"--memory-swappiness", "-1",
"--name", cname, busyboxImage, "top")
DelContainerForceMultyTime(c, cname)
c.Assert(res.ExitCode, check.Equals, 1)
c.Assert(res.ExitCode, check.Equals, 0)

cname = "TestRunWithMemoryswappiness"
memory := "100m"
Expand Down

0 comments on commit 335c90c

Please sign in to comment.