diff --git a/src/runtime/containerd-shim-v2/start.go b/src/runtime/containerd-shim-v2/start.go index 72499a4361..20435e7c40 100644 --- a/src/runtime/containerd-shim-v2/start.go +++ b/src/runtime/containerd-shim-v2/start.go @@ -36,6 +36,9 @@ func startContainer(ctx context.Context, s *service, c *container) error { return err } go watchSandbox(s) + + // Start watching for oom events + go watchOOMEvents(s) } else { _, err := s.sandbox.StartContainer(c.id) if err != nil { diff --git a/src/runtime/containerd-shim-v2/start_test.go b/src/runtime/containerd-shim-v2/start_test.go index 8d018bb522..22a024e5d4 100644 --- a/src/runtime/containerd-shim-v2/start_test.go +++ b/src/runtime/containerd-shim-v2/start_test.go @@ -45,6 +45,7 @@ func TestStartStartSandboxSuccess(t *testing.T) { id: testSandboxID, sandbox: sandbox, containers: make(map[string]*container), + ctx: namespaces.WithNamespace(context.Background(), "UnitTest"), } reqCreate := &taskAPI.CreateTaskRequest{ @@ -93,6 +94,7 @@ func TestStartMissingAnnotation(t *testing.T) { id: testSandboxID, sandbox: sandbox, containers: make(map[string]*container), + ctx: namespaces.WithNamespace(context.Background(), "UnitTest"), } reqCreate := &taskAPI.CreateTaskRequest{ @@ -113,8 +115,7 @@ func TestStartMissingAnnotation(t *testing.T) { testingImpl.StartSandboxFunc = nil }() - ctx := namespaces.WithNamespace(context.Background(), "UnitTest") - _, err = s.Start(ctx, reqStart) + _, err = s.Start(s.ctx, reqStart) assert.Error(err) assert.False(vcmock.IsMockError(err)) } @@ -159,6 +160,7 @@ func TestStartStartContainerSucess(t *testing.T) { id: testSandboxID, sandbox: sandbox, containers: make(map[string]*container), + ctx: namespaces.WithNamespace(context.Background(), "UnitTest"), } reqCreate := &taskAPI.CreateTaskRequest{ diff --git a/src/runtime/containerd-shim-v2/wait.go b/src/runtime/containerd-shim-v2/wait.go index 88b0d198f7..5059fa478b 100644 --- a/src/runtime/containerd-shim-v2/wait.go +++ b/src/runtime/containerd-shim-v2/wait.go @@ -9,6 +9,7 @@ import ( "path" "time" + "github.com/containerd/containerd/api/events" "github.com/containerd/containerd/api/types/task" "github.com/containerd/containerd/mount" "github.com/sirupsen/logrus" @@ -126,3 +127,26 @@ func watchSandbox(s *service) { // Existing container/exec will be cleaned up by its waiters. // No need to send async events here. } + +func watchOOMEvents(s *service) { + if s.sandbox == nil { + return + } + + for { + select { + case <-s.ctx.Done(): + return + default: + containerID, err := s.sandbox.GetOOMEvent() + if err != nil { + logrus.WithError(err).Warn("failed to get oom event from sandbox") + continue + } + + s.send(&events.TaskOOM{ + ContainerID: containerID, + }) + } + } +}