Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
runtime: write oom file to notify CRI-O OOM occurred
Browse files Browse the repository at this point in the history
Fixes: #2955

Signed-off-by: bin liu <[email protected]>
  • Loading branch information
liubin committed Sep 16, 2020
1 parent 911a495 commit b90babb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions containerd-shim-v2/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ package containerdshim

import (
"context"
"os"
"path"
"time"

"github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/mount"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -151,6 +153,26 @@ func watchOOMEvents(ctx context.Context, s *service) {
continue
}

// write oom file for CRI-O
if c, ok := s.containers[containerID]; ok && oci.IsCRIOContainerManager(c.spec) {
oomPath := path.Join(c.bundle, "oom")
logrus.WithFields(logrus.Fields{
"sandbox": s.sandbox.ID(),
"container": containerID,
}).Infof("write oom file to notify CRI-O: %s", oomPath)

f, err := os.OpenFile(oomPath, os.O_CREATE, 0666)
if err != nil {
logrus.WithFields(logrus.Fields{
"sandbox": s.sandbox.ID(),
"container": containerID,
}).Warnf("failed to write oom file %s", oomPath)
} else {
f.Close()
}
}

// publish event for containerd
s.send(&events.TaskOOM{
ContainerID: containerID,
})
Expand Down
10 changes: 10 additions & 0 deletions virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,3 +1040,13 @@ func GetOCIConfig(status vc.ContainerStatus) (specs.Spec, error) {

return *status.Spec, nil
}

// IsCRIOContainerManager check if a Pod is created from CRI-O
func IsCRIOContainerManager(spec *specs.Spec) bool {
if val, ok := spec.Annotations[crioAnnotations.ContainerType]; ok {
if val == crioAnnotations.ContainerTypeSandbox || val == crioAnnotations.ContainerTypeContainer {
return true
}
}
return false
}
32 changes: 32 additions & 0 deletions virtcontainers/pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"

"github.com/cri-o/cri-o/pkg/annotations"
crioAnnotations "github.com/cri-o/cri-o/pkg/annotations"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -869,3 +870,34 @@ func TestAddRuntimeAnnotations(t *testing.T) {
assert.Equal(config.NetworkConfig.DisableNewNetNs, true)
assert.Equal(config.NetworkConfig.InterworkingModel, vc.NetXConnectMacVtapModel)
}

func TestIsCRIOContainerManager(t *testing.T) {
assert := assert.New(t)

testCases := []struct {
annotations map[string]string
result bool
}{
{
annotations: map[string]string{crioAnnotations.ContainerType: "abc"},
result: false,
},
{
annotations: map[string]string{crioAnnotations.ContainerType: crioAnnotations.ContainerTypeSandbox},
result: true,
},
{
annotations: map[string]string{crioAnnotations.ContainerType: crioAnnotations.ContainerTypeContainer},
result: true,
},
}

for i := range testCases {
tc := testCases[i]
ocispec := specs.Spec{
Annotations: tc.annotations,
}
result := IsCRIOContainerManager(&ocispec)
assert.Equal(tc.result, result, "test case %d", (i + 1))
}
}

0 comments on commit b90babb

Please sign in to comment.