Skip to content

Commit

Permalink
Add OomScoreAdj
Browse files Browse the repository at this point in the history
Signed-off-by: plaurent <[email protected]>
  • Loading branch information
psaintlaurent committed Jun 10, 2024
1 parent 465208e commit 7c5a646
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/command/container/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type containerOptions struct {
stdin bool
tty bool
oomKillDisable bool
oomScoreAdj int
oomScoreAdj int64
containerIDFile string
entrypoint string
hostname string
Expand Down
1 change: 1 addition & 0 deletions cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.SetAnnotation(flagSysCtl, "version", []string{"1.40"})
flags.Var(&opts.ulimits, flagUlimit, "Ulimit options")
flags.SetAnnotation(flagUlimit, "version", []string{"1.41"})
flags.Var(&opts.oomScoreAdj, flagOomScoreAdj, "oom score adjustment (-1000 to 1000)")

flags.Var(cliopts.NewListOptsRef(&opts.resources.resGenericResources, ValidateSingleGenericResource), "generic-resource", "User defined resources")
flags.SetAnnotation(flagHostAdd, "version", []string{"1.32"})
Expand Down
2 changes: 2 additions & 0 deletions cli/command/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestSetConfigsWithCredSpecAndConfigs(t *testing.T) {
service := &swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
OomScoreAdj: 500,
Privileges: &swarm.Privileges{
CredentialSpec: opts.credentialSpec.value,
},
Expand Down Expand Up @@ -140,6 +141,7 @@ func TestSetConfigsOnlyCredSpec(t *testing.T) {
service := &swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
OomScoreAdj: 500,
Privileges: &swarm.Privileges{
CredentialSpec: opts.credentialSpec.value,
},
Expand Down
4 changes: 4 additions & 0 deletions cli/command/service/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ type serviceOptions struct {
capAdd opts.ListOpts
capDrop opts.ListOpts
ulimits opts.UlimitOpt
oomScoreAdj opts.OomScoreAdj

resources resourceOptions
stopGrace opts.DurationOpt
Expand Down Expand Up @@ -575,6 +576,7 @@ func newServiceOptions() *serviceOptions {
capAdd: opts.NewListOpts(nil),
capDrop: opts.NewListOpts(nil),
ulimits: *opts.NewUlimitOpt(nil),
oomScoreAdj: opts.OomScoreAdj(0),
}
}

Expand Down Expand Up @@ -747,6 +749,7 @@ func (options *serviceOptions) ToService(ctx context.Context, apiClient client.N
CapabilityAdd: capAdd,
CapabilityDrop: capDrop,
Ulimits: options.ulimits.GetList(),
OomScoreAdj: options.oomScoreAdj.Value(),
},
Networks: networks,
Resources: resources,
Expand Down Expand Up @@ -1043,6 +1046,7 @@ const (
flagUlimit = "ulimit"
flagUlimitAdd = "ulimit-add"
flagUlimitRemove = "ulimit-rm"
flagOomScoreAdj = "oom-score-adj"
)

func validateAPIVersion(c swarm.ServiceSpec, serverAPIVersion string) error {
Expand Down
24 changes: 24 additions & 0 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"path"
"regexp"
"strconv"
"strings"

"github.com/docker/docker/api/types/filters"
Expand Down Expand Up @@ -515,3 +516,26 @@ func (m *MemSwapBytes) UnmarshalJSON(s []byte) error {
b := MemBytes(*m)
return b.UnmarshalJSON(s)
}

type OomScoreAdj int64

func (o *OomScoreAdj) Type() string { return "int64" }

func (o *OomScoreAdj) Value() int64 { return int64(*o) }

func (o *OomScoreAdj) String() string {

val := strconv.FormatInt(int64(*o), 10)
return val
}

func (o *OomScoreAdj) Set(value string) error {

var conv int64
conv, err := strconv.ParseInt(value, 10, 64)
if err != nil || conv < -1000 || conv > 1000 {
return err
}
*o = OomScoreAdj(conv)
return nil
}

0 comments on commit 7c5a646

Please sign in to comment.