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 pidslimit implement #1227

Merged
merged 1 commit into from
May 17, 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
1 change: 1 addition & 0 deletions cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {

flagSet.StringVarP(&c.workdir, "workdir", "w", "", "Set the working directory in a container")
flagSet.Var(&c.ulimit, "ulimit", "Set container ulimit")
flagSet.Int64Var(&c.pidsLimit, "pids-limit", -1, "Set container pids limit, -1 for unlimited")

flagSet.BoolVar(&c.rich, "rich", false, "Start container in rich container mode. (default false)")
flagSet.StringVar(&c.richMode, "rich-mode", "", "Choose one rich container mode. dumb-init(default), systemd, sbin-init")
Expand Down
2 changes: 2 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type container struct {
specAnnotation []string
cgroupParent string
ulimit Ulimit
pidsLimit int64

//add for rich container mode
rich bool
Expand Down Expand Up @@ -223,6 +224,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
IntelRdtL3Cbm: intelRdtL3Cbm,
CgroupParent: c.cgroupParent,
Ulimits: c.ulimit.value(),
PidsLimit: c.pidsLimit,
},
EnableLxcfs: c.enableLxcfs,
Privileged: c.privileged,
Expand Down
5 changes: 4 additions & 1 deletion daemon/mgr/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ func setupResource(ctx context.Context, c *Container, s *specs.Spec) error {
return err
}

//TODO: nedd support Pids, HugepageLimits, Network cgroup set
// start to setup pids limit
s.Linux.Resources.Pids = &specs.LinuxPids{
Limit: c.HostConfig.PidsLimit,
}

return nil
}
Expand Down
15 changes: 15 additions & 0 deletions test/cli_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,18 @@ func (suite *PouchCreateSuite) TestCreateWithUlimit(c *check.C) {
c.Assert(int(ul.Hard), check.Equals, 21)
c.Assert(int(ul.Soft), check.Equals, 21)
}

// TestCreateWithPidsLimit tests running container with --pids-limit flag.
func (suite *PouchRunSuite) TestCreateWithPidsLimit(c *check.C) {
cname := "TestCreateWithPidsLimit"
res := command.PouchRun("create", "--pids-limit", "10", "--name", cname, busyboxImage)
res.Assert(c, icmd.Success)

output := command.PouchRun("inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
pl := result[0].HostConfig.PidsLimit
c.Assert(int(pl), check.Equals, 10)
}
19 changes: 19 additions & 0 deletions test/cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,22 @@ func (suite *PouchRunSuite) TestRunWithUlimit(c *check.C) {
c.Assert(int(ul.Soft), check.Equals, 256)

}

// TestRunWithPidsLimit tests running container with --pids-limit flag.
func (suite *PouchRunSuite) TestRunWithPidsLimit(c *check.C) {
cname := "TestRunWithPidsLimit"
pidfile := "/sys/fs/cgroup/pids/pids.max"
res := command.PouchRun("run", "--pids-limit", "10", "--name", cname, busyboxImage, "cat", pidfile)
res.Assert(c, icmd.Success)

out := res.Stdout()
c.Assert(out, check.Equals, "10\n")

output := command.PouchRun("inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
pl := result[0].HostConfig.PidsLimit
c.Assert(int(pl), check.Equals, 10)
}
11 changes: 11 additions & 0 deletions test/cli_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,14 @@ func (suite *PouchStartSuite) TestStartWithUlimit(c *check.C) {

command.PouchRun("start", name).Assert(c, icmd.Success)
}

// TestStartWithPidsLimit tests running container with --pids-limit flag.
func (suite *PouchStartSuite) TestStartWithPidsLimit(c *check.C) {
name := "TestStartWithPidsLimit"
pidfile := "/sys/fs/cgroup/pids/pids.max"
res := command.PouchRun("create", "--pids-limit", "10", "--name", name, busyboxImage, "cat", pidfile)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)

command.PouchRun("start", name).Assert(c, icmd.Success)
}