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

Add oom-score-adj option while running #1255

Merged
merged 1 commit into from
Jul 23, 2022
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ Network flags:
- :whale: `--add-host`: Add a custom host-to-IP mapping (host:ip)
- :whale: `--ip`: Specific static IP address(es) to use

Cgroup flags:
Resource flags:
- :whale: `--cpus`: Number of CPUs
- :whale: `--cpu-quota`: Limit the CPU CFS (Completely Fair Scheduler) quota
- :whale: `--cpu-period`: Limit the CPU CFS (Completely Fair Scheduler) period
Expand All @@ -407,6 +407,7 @@ Cgroup flags:
- :whale: `--memory-swappiness`: Tune container memory swappiness (0 to 100) (default -1)
- :whale: `--kernel-memory`: Kernel memory limit (deprecated)
- :whale: `--oom-kill-disable`: Disable OOM Killer
- :whale: `--oom-score-adj`: Tune container’s OOM preferences (-1000 to 1000)
- :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 Expand Up @@ -522,7 +523,7 @@ Verify flags:
Unimplemented `docker run` flags:
`--attach`, `--blkio-weight-device`, `--cgroup-parent`, `--cpu-rt-*`, `--detach-keys`, `--device-*`,
`--disable-content-trust`, `--domainname`, `--expose`, `--health-*`, `--ip6`, `--isolation`, `--no-healthcheck`,
`--oom-score-adj`, `--link*`, `--mac-address`, `--publish-all`, `--sig-proxy`, `--storage-opt`,
`--link*`, `--mac-address`, `--publish-all`, `--sig-proxy`, `--storage-opt`,
manugupt1 marked this conversation as resolved.
Show resolved Hide resolved
`--userns`, `--uts`, `--volume-driver`, `--volumes-from`

### :whale: :blue_square: nerdctl exec
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func setCreateFlags(cmd *cobra.Command) {
cmd.Flags().Int64("memory-swappiness", -1, "Tune container memory swappiness (0 to 100) (default -1)")
cmd.Flags().String("kernel-memory", "", "Kernel memory limit (deprecated)")
cmd.Flags().Bool("oom-kill-disable", false, "Disable OOM Killer")
cmd.Flags().Int("oom-score-adj", 0, "Tune container’s OOM preferences (-1000 to 1000)")
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
26 changes: 26 additions & 0 deletions cmd/nerdctl/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,31 @@ func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]o
return nil, fmt.Errorf("error: %v", "invalid ipc value, supported values are 'private' or 'host'")
}

opts, err = setOOMScoreAdj(opts, cmd)
if err != nil {
return nil, err
}

return opts, nil
}

func setOOMScoreAdj(opts []oci.SpecOpts, cmd *cobra.Command) ([]oci.SpecOpts, error) {
score, err := cmd.Flags().GetInt("oom-score-adj")
if err != nil {
return nil, err
}

if score < -1000 || score > 1000 {
return nil, fmt.Errorf("invalid value %d, range for oom score adj is [-1000, 1000]", score)
}

opts = append(opts, withOOMScoreAdj(score))
return opts, nil
}

func withOOMScoreAdj(score int) oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
s.Process.OOMScoreAdj = &score
return nil
}
}
12 changes: 12 additions & 0 deletions cmd/nerdctl/run_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"testing"
"time"

"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/testutil"

Expand Down Expand Up @@ -331,3 +332,14 @@ func TestRunWithFluentdLogDriverWithLogOpt(t *testing.T) {
assert.Equal(t, true, strings.Contains(logData, "test2"))
assert.Equal(t, true, strings.Contains(logData, inspectedContainer.ID))
}

func TestRunWithOOMScoreAdj(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it works well on docker because I have docker added in the sudo'ers group.
Code here shows rootless mode cannot be supported: https://github.com/moby/moby/blob/cf867587b9dc8edf92efd8372fcc6f86f117e386/daemon/daemon_unix.go#L1642-L1656

if rootlessutil.IsRootless() {
t.Skip("test skipped for rootless containers.")
}
t.Parallel()
base := testutil.NewBase(t)
var score = "-42"

base.Cmd("run", "--rm", "--oom-score-adj", score, testutil.AlpineImage, "cat", "/proc/self/oom_score_adj").AssertOutContains(score)
}