This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shimv2: convert vc errors to grpc errors
containerd checks for the grpc error code to determine correct recover action upon grpc errors. We need to provide them properly. Unfortunately ttrpc doesn't support grpc interceptor so we have to modify every service function for it. Fixes: #1527 Signed-off-by: Peng Tao <bergwolf@hyper.sh>
- Loading branch information
Showing
3 changed files
with
180 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2019 hyper.sh | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package containerdshim | ||
|
||
import ( | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
vc "github.com/kata-containers/runtime/virtcontainers/pkg/types" | ||
) | ||
|
||
// toGRPC maps the virtcontainers error into a grpc error, | ||
// using the original error message as a description. | ||
func toGRPC(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if isGRPCError(err) { | ||
// error has already been mapped to grpc | ||
return err | ||
} | ||
|
||
err = errors.Cause(err) | ||
switch { | ||
case isInvalidArgument(err): | ||
return status.Errorf(codes.InvalidArgument, err.Error()) | ||
case isNotFound(err): | ||
return status.Errorf(codes.NotFound, err.Error()) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// toGRPCf maps the error to grpc error codes, assembling the formatting string | ||
// and combining it with the target error string. | ||
func toGRPCf(err error, format string, args ...interface{}) error { | ||
return toGRPC(errors.Wrapf(err, format, args...)) | ||
} | ||
|
||
func isGRPCError(err error) bool { | ||
_, ok := status.FromError(err) | ||
return ok | ||
} | ||
|
||
func isInvalidArgument(err error) bool { | ||
return err == vc.ErrNeedSandbox || err == vc.ErrNeedSandboxID || | ||
err == vc.ErrNeedContainerID || err == vc.ErrNeedState || | ||
err == syscall.EINVAL | ||
} | ||
|
||
func isNotFound(err error) bool { | ||
return err == vc.ErrNoSuchContainer || err == syscall.ENOENT || | ||
strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "not exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2019 hyper.sh | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package containerdshim | ||
|
||
import ( | ||
"syscall" | ||
"testing" | ||
|
||
vc "github.com/kata-containers/runtime/virtcontainers/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToGRPC(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
for _, err := range []error{vc.ErrNeedSandbox, vc.ErrNeedSandboxID, | ||
vc.ErrNeedContainerID, vc.ErrNeedState, syscall.EINVAL, vc.ErrNoSuchContainer, syscall.ENOENT} { | ||
assert.False(isGRPCError(err)) | ||
err = toGRPC(err) | ||
assert.True(isGRPCError(err)) | ||
err = toGRPC(err) | ||
assert.True(isGRPCError(err)) | ||
err = toGRPCf(err, "appending") | ||
assert.True(isGRPCError(err)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters