Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

CRI: add oom_score_adj isolator #658

Merged
merged 1 commit into from
Sep 23, 2016
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
2 changes: 1 addition & 1 deletion actool/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func patchManifest(im *schema.ImageManifest) error {
_, ok := types.ResourceIsolatorNames[name]

switch name {
case types.LinuxNoNewPrivilegesName:
case types.LinuxNoNewPrivilegesName, types.LinuxOOMScoreAdjName:
ok = true
kv := strings.Split(is, ",")
if len(kv) != 2 {
Expand Down
44 changes: 44 additions & 0 deletions schema/types/isolator_linux_specific.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package types
import (
"encoding/json"
"errors"
"fmt"
"unicode"
)

Expand All @@ -26,6 +27,7 @@ const (
LinuxNoNewPrivilegesName = "os/linux/no-new-privileges"
LinuxSeccompRemoveSetName = "os/linux/seccomp-remove-set"
LinuxSeccompRetainSetName = "os/linux/seccomp-retain-set"
LinuxOOMScoreAdjName = "os/linux/oom-score-adj"
)

var LinuxIsolatorNames = make(map[ACIdentifier]struct{})
Expand All @@ -35,6 +37,7 @@ func init() {
LinuxCapabilitiesRevokeSetName: func() IsolatorValue { return &LinuxCapabilitiesRevokeSet{} },
LinuxCapabilitiesRetainSetName: func() IsolatorValue { return &LinuxCapabilitiesRetainSet{} },
LinuxNoNewPrivilegesName: func() IsolatorValue { v := LinuxNoNewPrivileges(false); return &v },
LinuxOOMScoreAdjName: func() IsolatorValue { v := LinuxOOMScoreAdj(0); return &v },
LinuxSeccompRemoveSetName: func() IsolatorValue { return &LinuxSeccompRemoveSet{} },
LinuxSeccompRetainSetName: func() IsolatorValue { return &LinuxSeccompRetainSet{} },
} {
Expand Down Expand Up @@ -321,3 +324,44 @@ func (l LinuxSeccompRemoveSet) AsIsolator() (*Isolator, error) {
value: &l,
}, nil
}

// LinuxOOMScoreAdj is equivalent to /proc/[pid]/oom_score_adj
type LinuxOOMScoreAdj int // -1000 to 1000
func (l LinuxOOMScoreAdj) AssertValid() error {
if l < -1000 || l > 1000 {
return fmt.Errorf("%s must be between -1000 and 1000, got %d", LinuxOOMScoreAdjName, l)
}
return nil
}

func (l LinuxOOMScoreAdj) multipleAllowed() bool {
return false
}

func (l LinuxOOMScoreAdj) Conflicts() []ACIdentifier {
return nil
}

func (l *LinuxOOMScoreAdj) UnmarshalJSON(b []byte) error {
var v int
err := json.Unmarshal(b, &v)
if err != nil {
return err
}

*l = LinuxOOMScoreAdj(v)
return nil
}

func (l LinuxOOMScoreAdj) AsIsolator() Isolator {
b, err := json.Marshal(l)
if err != nil {
panic(err)
}
rm := json.RawMessage(b)
return Isolator{
Name: LinuxOOMScoreAdjName,
ValueRaw: &rm,
value: &l,
}
}
28 changes: 28 additions & 0 deletions schema/types/isolator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ func TestIsolatorUnmarshal(t *testing.T) {
msg string
werr bool
}{
{
`{
"name": "os/linux/oom-score-adj",
"value": 250
}`,
false,
},
{
`{
"name": "os/linux/oom-score-adj",
"value": -250
}`,
false,
},
{
`{
"name": "os/linux/oom-score-adj",
"value": -2500
}`,
true,
},
{
`{
"name": "os/linux/oom-score-adj",
"value": "pants"
}`,
true,
},
{
`{
"name": "os/linux/no-new-privileges",
Expand Down