Skip to content

Commit

Permalink
add oom-kill-disable
Browse files Browse the repository at this point in the history
Signed-off-by: 宁明晓10296073 <ning.mingxiao@zte.com.cn>
  • Loading branch information
ningmingxiao committed Jun 9, 2022
1 parent b702a25 commit 7f793fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ Cgroup flags:
- :whale: `--cpuset-cpus`: CPUs in which to allow execution (0-3, 0,1)
- :whale: `--cpuset-mems`: Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems
- :whale: `--memory`: Memory limit
- :whale: `--oom-kill-disable`: Disable OOM Killer
- :whale: `--pids-limit`: Tune container pids limit
- :nerd_face: `--cgroup-conf`: Configure cgroup v2 (key=value)
- :whale: `blkio-weight`: Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func setCreateFlags(cmd *cobra.Command) {
// #region cgroups, namespaces, and ulimits flags
cmd.Flags().Float64("cpus", 0.0, "Number of CPUs")
cmd.Flags().StringP("memory", "m", "", "Memory limit")
cmd.Flags().Bool("oom-kill-disable", false, "Disable OOM Killer")
cmd.Flags().String("pid", "", "PID namespace to use")
cmd.RegisterFlagCompletionFunc("pid", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"host"}, cobra.ShellCompDirectiveNoFileComp
Expand Down
31 changes: 28 additions & 3 deletions cmd/nerdctl/run_cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
"github.com/containerd/containerd/oci"
"github.com/containerd/nerdctl/pkg/infoutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/docker/go-units"
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -49,6 +46,15 @@ func generateCgroupOpts(cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
if err != nil {
return nil, err
}

okd, err := cmd.Flags().GetBool("oom-kill-disable")
if err != nil {
return nil, err
}
if memStr == "" && okd {
logrus.Warn("Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.")
}

pidsLimit, err := cmd.Flags().GetInt64("pids-limit")
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,6 +136,10 @@ func generateCgroupOpts(cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
opts = append(opts, oci.WithMemoryLimit(uint64(mem64)))
}

if okd {
opts = append(opts, withDisableOOMKiller(okd))
}

if pidsLimit > 0 {
opts = append(opts, oci.WithPidsLimit(int64(pidsLimit)))
}
Expand Down Expand Up @@ -274,3 +284,18 @@ func withBlkioWeight(blkioWeight uint16) oci.SpecOpts {
return nil
}
}

func withDisableOOMKiller(disableOOMKiller bool) oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
if s.Linux != nil {
if s.Linux.Resources == nil {
s.Linux.Resources = &specs.LinuxResources{}
}
if s.Linux.Resources.Memory == nil {
s.Linux.Resources.Memory = &specs.LinuxMemory{}
}
s.Linux.Resources.Memory.DisableOOMKiller = &disableOOMKiller
}
return nil
}
}

0 comments on commit 7f793fb

Please sign in to comment.