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

Commit

Permalink
shim: exit out of oom polling if unimplemented
Browse files Browse the repository at this point in the history
This exits out of polling for OOM events if the getOOMEvent
method is unimplemented.

Signed-off-by: Alex Price <[email protected]>
  • Loading branch information
awprice committed May 7, 2020
1 parent b4833a4 commit 86f5810
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
8 changes: 8 additions & 0 deletions containerd-shim-v2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ func isNotFound(err error) bool {
return err == vc.ErrNoSuchContainer || err == syscall.ENOENT ||
strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "not exist")
}

func isGRPCErrorCode(code codes.Code, err error) bool {
s, ok := status.FromError(err)
if !ok {
return false
}
return s != nil && s.Code() == code
}
10 changes: 10 additions & 0 deletions containerd-shim-v2/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package containerdshim

import (
"errors"
"syscall"
"testing"

vc "github.com/kata-containers/runtime/virtcontainers/pkg/types"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestToGRPC(t *testing.T) {
Expand All @@ -27,3 +30,10 @@ func TestToGRPC(t *testing.T) {
assert.True(isGRPCError(err))
}
}

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

assert.True(isGRPCErrorCode(codes.Unimplemented, status.New(codes.Unimplemented, "foobar").Err()))
assert.False(isGRPCErrorCode(codes.Unimplemented, errors.New("foobar")))
}
5 changes: 3 additions & 2 deletions containerd-shim-v2/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func startContainer(ctx context.Context, s *service, c *container) error {
}
go watchSandbox(s)

// Start watching for oom events
go watchOOMEvents(ctx, s)
// We don't rely on the context passed to startContainer as it can be cancelled after
// this rpc call.
go watchOOMEvents(s.ctx, s)
} else {
_, err := s.sandbox.StartContainer(c.id)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions containerd-shim-v2/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestStartStartSandboxSuccess(t *testing.T) {
id: testSandboxID,
sandbox: sandbox,
containers: make(map[string]*container),
ctx: context.Background(),
}

reqCreate := &taskAPI.CreateTaskRequest{
Expand Down Expand Up @@ -93,6 +94,7 @@ func TestStartMissingAnnotation(t *testing.T) {
id: testSandboxID,
sandbox: sandbox,
containers: make(map[string]*container),
ctx: context.Background(),
}

reqCreate := &taskAPI.CreateTaskRequest{
Expand Down Expand Up @@ -159,6 +161,7 @@ func TestStartStartContainerSucess(t *testing.T) {
id: testSandboxID,
sandbox: sandbox,
containers: make(map[string]*container),
ctx: context.Background(),
}

reqCreate := &taskAPI.CreateTaskRequest{
Expand Down
8 changes: 7 additions & 1 deletion containerd-shim-v2/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/mount"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
)

func wait(s *service, c *container, execID string) (int32, error) {
Expand Down Expand Up @@ -141,7 +142,12 @@ func watchOOMEvents(ctx context.Context, s *service) {
default:
containerID, err := s.sandbox.GetOOMEvent()
if err != nil {
logrus.WithError(err).Warn("failed to get oom event from sandbox")
logrus.WithField("sandbox", s.sandbox.ID()).WithError(err).Warn("failed to get OOM event from sandbox")
// If the GetOOMEvent call is not implemented, then the agent is most likely an older version,
// stop attempting to get OOM events.
if isGRPCErrorCode(codes.Unimplemented, err) {
return
}
continue
}

Expand Down

0 comments on commit 86f5810

Please sign in to comment.