notify: simplify usage

Instead of passing the whole map of paths, pass the path to the memory
controller which these functions actually require.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2020-05-06 18:29:59 -07:00
parent 2b31437caa
commit 9a3e632625
3 changed files with 13 additions and 19 deletions

View File

@ -655,7 +655,7 @@ func (c *linuxContainer) NotifyOOM() (<-chan struct{}, error) {
}
return notifyOnOOMV2(path)
}
return notifyOnOOM(c.cgroupManager.GetPaths())
return notifyOnOOM(c.cgroupManager.GetPaths()["memory"])
}
func (c *linuxContainer) NotifyMemoryPressure(level PressureLevel) (<-chan struct{}, error) {
@ -663,7 +663,7 @@ func (c *linuxContainer) NotifyMemoryPressure(level PressureLevel) (<-chan struc
if c.config.RootlessCgroups {
logrus.Warn("getting memory pressure notifications may fail if you don't have the full access to cgroups")
}
return notifyMemoryPressure(c.cgroupManager.GetPaths(), level)
return notifyMemoryPressure(c.cgroupManager.GetPaths()["memory"], level)
}
var criuFeatures *criurpc.CriuFeatures

View File

@ -3,6 +3,7 @@
package libcontainer
import (
"errors"
"fmt"
"io/ioutil"
"os"
@ -11,8 +12,6 @@ import (
"golang.org/x/sys/unix"
)
const oomCgroupName = "memory"
type PressureLevel uint
const (
@ -66,19 +65,17 @@ func registerMemoryEvent(cgDir string, evName string, arg string) (<-chan struct
// notifyOnOOM returns channel on which you can expect event about OOM,
// if process died without OOM this channel will be closed.
func notifyOnOOM(paths map[string]string) (<-chan struct{}, error) {
dir := paths[oomCgroupName]
func notifyOnOOM(dir string) (<-chan struct{}, error) {
if dir == "" {
return nil, fmt.Errorf("path %q missing", oomCgroupName)
return nil, errors.New("memory controller missing")
}
return registerMemoryEvent(dir, "memory.oom_control", "")
}
func notifyMemoryPressure(paths map[string]string, level PressureLevel) (<-chan struct{}, error) {
dir := paths[oomCgroupName]
func notifyMemoryPressure(dir string, level PressureLevel) (<-chan struct{}, error) {
if dir == "" {
return nil, fmt.Errorf("path %q missing", oomCgroupName)
return nil, errors.New("memory controller missing")
}
if level > CriticalPressure {

View File

@ -14,7 +14,7 @@ import (
"golang.org/x/sys/unix"
)
type notifyFunc func(paths map[string]string) (<-chan struct{}, error)
type notifyFunc func(path string) (<-chan struct{}, error)
func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ string) {
memoryPath, err := ioutil.TempDir("", "testmemnotification-"+evName)
@ -29,10 +29,7 @@ func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ
if err := ioutil.WriteFile(eventPath, []byte{}, 0700); err != nil {
t.Fatal(err)
}
paths := map[string]string{
"memory": memoryPath,
}
ch, err := notify(paths)
ch, err := notify(memoryPath)
if err != nil {
t.Fatal("expected no error, got:", err)
}
@ -102,8 +99,8 @@ func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ
}
func TestNotifyOnOOM(t *testing.T) {
f := func(paths map[string]string) (<-chan struct{}, error) {
return notifyOnOOM(paths)
f := func(path string) (<-chan struct{}, error) {
return notifyOnOOM(path)
}
testMemoryNotification(t, "memory.oom_control", f, "")
@ -117,8 +114,8 @@ func TestNotifyMemoryPressure(t *testing.T) {
}
for level, arg := range tests {
f := func(paths map[string]string) (<-chan struct{}, error) {
return notifyMemoryPressure(paths, level)
f := func(path string) (<-chan struct{}, error) {
return notifyMemoryPressure(path, level)
}
testMemoryNotification(t, "memory.pressure_level", f, arg)