Skip to content

Commit

Permalink
feat(host): set cgroup pids.max of container (#21051)
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi authored Aug 16, 2024
1 parent 9d7fc0c commit 339e424
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/apis/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ContainerSpec struct {
Privileged bool `json:"privileged"`
Lifecyle *ContainerLifecyle `json:"lifecyle"`
CgroupDevicesAllow []string `json:"cgroup_devices_allow"`
CgroupPidsMax int `json:"cgroup_pids_max"`
SimulateCpu bool `json:"simulate_cpu"`
ShmSizeMB int `json:"shm_size_mb"`
SecurityContext *ContainerSecurityContext `json:"security_context,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/hostman/guestman/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ func (s *sPodGuestInstance) StartContainer(ctx context.Context, userCred mcclien
if err := s.setContainerCgroupDevicesAllow(criId, input.Spec.CgroupDevicesAllow); err != nil {
return nil, errors.Wrap(err, "set cgroup devices allow")
}
if input.Spec.CgroupPidsMax > 0 {
if err := s.getCGUtil().SetPidsMax(criId, input.Spec.CgroupPidsMax); err != nil {
return nil, errors.Wrap(err, "set cgroup pids.max")
}
}
if err := s.doContainerStartPostLifecycle(ctx, criId, input); err != nil {
return nil, errors.Wrap(err, "do container lifecycle")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/pod/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type CgroupUtil interface {
SetMemoryLimitBytes(ctrId string, bytes int64) error
SetCPUCfs(ctrId string, quota int64, period int64) error
SetDevicesAllow(ctrId string, allows []string) error
SetPidsMax(ctrId string, max int) error
}

type podCgroupV1Util struct {
Expand Down Expand Up @@ -86,3 +87,8 @@ func (p podCgroupV1Util) SetDevicesAllow(ctrId string, allows []string) error {
}
return nil
}

func (p podCgroupV1Util) SetPidsMax(ctrId string, max int) error {
pidFp := p.getContainerCGFilePath("pids", ctrId, "pids.max")
return p.write(pidFp, fmt.Sprintf("%d", max))
}

0 comments on commit 339e424

Please sign in to comment.