runtime: write oom file to notify CRI-O tha OOM occurred

CRI-O is not use event like containerd, it's depending on
file name `oom` to dectect if an OOM occurred.

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu 2020-09-16 10:35:24 +08:00
parent 15065e4472
commit d7c77b69dc
3 changed files with 59 additions and 0 deletions

View File

@ -7,6 +7,7 @@ package containerdshim
import ( import (
"context" "context"
"os"
"path" "path"
"time" "time"
@ -15,6 +16,8 @@ import (
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
) )
func wait(s *service, c *container, execID string) (int32, error) { func wait(s *service, c *container, execID string) (int32, error) {
@ -152,6 +155,20 @@ func watchOOMEvents(ctx context.Context, s *service) {
continue continue
} }
// write oom file for CRI-O
if c, ok := s.containers[containerID]; ok && oci.IsCRIOContainerManager(c.spec) {
oomPath := path.Join(c.bundle, "oom")
shimLog.Infof("write oom file to notify CRI-O: %s", oomPath)
f, err := os.OpenFile(oomPath, os.O_CREATE, 0666)
if err != nil {
shimLog.WithError(err).Warnf("failed to write oom file %s", oomPath)
} else {
f.Close()
}
}
// publish event for containerd
s.send(&events.TaskOOM{ s.send(&events.TaskOOM{
ContainerID: containerID, ContainerID: containerID,
}) })

View File

@ -1036,3 +1036,13 @@ func GetOCIConfig(status vc.ContainerStatus) (specs.Spec, error) {
return *status.Spec, nil 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
}

View File

@ -17,6 +17,7 @@ import (
"testing" "testing"
"github.com/cri-o/cri-o/pkg/annotations" "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" specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -866,3 +867,34 @@ func TestAddRuntimeAnnotations(t *testing.T) {
assert.Equal(config.NetworkConfig.DisableNewNetNs, true) assert.Equal(config.NetworkConfig.DisableNewNetNs, true)
assert.Equal(config.NetworkConfig.InterworkingModel, vc.NetXConnectMacVtapModel) 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))
}
}