vc: export vc error types

So that shimv2 can convert it into grpc errors.

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Peng Tao 2019-04-12 02:01:02 -07:00
parent da08b3afc9
commit cf90751638
5 changed files with 63 additions and 62 deletions

View File

@ -132,7 +132,7 @@ func DeleteSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -165,7 +165,7 @@ func FetchSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -202,7 +202,7 @@ func StartSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -234,7 +234,7 @@ func StopSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandbox
return nil, vcTypes.ErrNeedSandbox
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -328,7 +328,7 @@ func StatusSandbox(ctx context.Context, sandboxID string) (SandboxStatus, error)
defer span.Finish()
if sandboxID == "" {
return SandboxStatus{}, errNeedSandboxID
return SandboxStatus{}, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -374,7 +374,7 @@ func CreateContainer(ctx context.Context, sandboxID string, containerConfig Cont
defer span.Finish()
if sandboxID == "" {
return nil, nil, errNeedSandboxID
return nil, nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -405,11 +405,11 @@ func DeleteContainer(ctx context.Context, sandboxID, containerID string) (VCCont
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -434,11 +434,11 @@ func StartContainer(ctx context.Context, sandboxID, containerID string) (VCConta
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -463,11 +463,11 @@ func StopContainer(ctx context.Context, sandboxID, containerID string) (VCContai
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -492,11 +492,11 @@ func EnterContainer(ctx context.Context, sandboxID, containerID string, cmd type
defer span.Finish()
if sandboxID == "" {
return nil, nil, nil, errNeedSandboxID
return nil, nil, nil, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return nil, nil, nil, errNeedContainerID
return nil, nil, nil, vcTypes.ErrNeedContainerID
}
lockFile, err := rLockSandbox(ctx, sandboxID)
@ -526,11 +526,11 @@ func StatusContainer(ctx context.Context, sandboxID, containerID string) (Contai
defer span.Finish()
if sandboxID == "" {
return ContainerStatus{}, errNeedSandboxID
return ContainerStatus{}, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return ContainerStatus{}, errNeedContainerID
return ContainerStatus{}, vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -607,11 +607,11 @@ func KillContainer(ctx context.Context, sandboxID, containerID string, signal sy
defer span.Finish()
if sandboxID == "" {
return errNeedSandboxID
return vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return errNeedContainerID
return vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -654,11 +654,11 @@ func ProcessListContainer(ctx context.Context, sandboxID, containerID string, op
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
lockFile, err := rLockSandbox(ctx, sandboxID)
@ -683,11 +683,11 @@ func UpdateContainer(ctx context.Context, sandboxID, containerID string, resourc
defer span.Finish()
if sandboxID == "" {
return errNeedSandboxID
return vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return errNeedContainerID
return vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -712,11 +712,11 @@ func StatsContainer(ctx context.Context, sandboxID, containerID string) (Contain
defer span.Finish()
if sandboxID == "" {
return ContainerStats{}, errNeedSandboxID
return ContainerStats{}, vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return ContainerStats{}, errNeedContainerID
return ContainerStats{}, vcTypes.ErrNeedContainerID
}
lockFile, err := rLockSandbox(ctx, sandboxID)
if err != nil {
@ -736,11 +736,11 @@ func StatsContainer(ctx context.Context, sandboxID, containerID string) (Contain
func togglePauseContainer(ctx context.Context, sandboxID, containerID string, pause bool) error {
if sandboxID == "" {
return errNeedSandboxID
return vcTypes.ErrNeedSandboxID
}
if containerID == "" {
return errNeedContainerID
return vcTypes.ErrNeedContainerID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -784,7 +784,7 @@ func AddDevice(ctx context.Context, sandboxID string, info deviceConfig.DeviceIn
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -804,7 +804,7 @@ func AddDevice(ctx context.Context, sandboxID string, info deviceConfig.DeviceIn
func toggleInterface(ctx context.Context, sandboxID string, inf *vcTypes.Interface, add bool) (*vcTypes.Interface, error) {
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -848,7 +848,7 @@ func ListInterfaces(ctx context.Context, sandboxID string) ([]*vcTypes.Interface
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rLockSandbox(ctx, sandboxID)
@ -872,7 +872,7 @@ func UpdateRoutes(ctx context.Context, sandboxID string, routes []*vcTypes.Route
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rwLockSandbox(ctx, sandboxID)
@ -896,7 +896,7 @@ func ListRoutes(ctx context.Context, sandboxID string) ([]*vcTypes.Route, error)
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
lockFile, err := rLockSandbox(ctx, sandboxID)

View File

@ -19,6 +19,7 @@ import (
"github.com/containerd/cgroups"
"github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/kata-containers/runtime/virtcontainers/utils"
specs "github.com/opencontainers/runtime-spec/specs-go"
@ -421,7 +422,7 @@ func (c *Container) loadDevices() ([]ContainerDevice, error) {
// container.
func (c *Container) setContainerState(state types.StateString) error {
if state == "" {
return errNeedState
return vcTypes.ErrNeedState
}
c.Logger().Debugf("Setting container state from %v to %v", c.state.State, state)

View File

@ -1,19 +0,0 @@
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"errors"
)
// common error objects used for argument checking
var (
errNeedSandbox = errors.New("Sandbox must be specified")
errNeedSandboxID = errors.New("Sandbox ID cannot be empty")
errNeedContainerID = errors.New("Container ID cannot be empty")
errNeedState = errors.New("State cannot be empty")
errNoSuchContainer = errors.New("Container does not exist")
)

View File

@ -0,0 +1,19 @@
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package types
import (
"errors"
)
// common error objects used for argument checking
var (
ErrNeedSandbox = errors.New("Sandbox must be specified")
ErrNeedSandboxID = errors.New("Sandbox ID cannot be empty")
ErrNeedContainerID = errors.New("Container ID cannot be empty")
ErrNeedState = errors.New("State cannot be empty")
ErrNoSuchContainer = errors.New("Container does not exist")
)

View File

@ -627,7 +627,7 @@ func unlockSandbox(ctx context.Context, sandboxID, token string) error {
func fetchSandbox(ctx context.Context, sandboxID string) (sandbox *Sandbox, err error) {
virtLog.Info("fetch sandbox")
if sandboxID == "" {
return nil, errNeedSandboxID
return nil, vcTypes.ErrNeedSandboxID
}
sandbox, err = globalSandboxList.lookupSandbox(sandboxID)
@ -665,11 +665,11 @@ func fetchSandbox(ctx context.Context, sandboxID string) (sandbox *Sandbox, err
// sandbox structure, based on a container ID.
func (s *Sandbox) findContainer(containerID string) (*Container, error) {
if s == nil {
return nil, errNeedSandbox
return nil, vcTypes.ErrNeedSandbox
}
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
for id, c := range s.containers {
@ -686,11 +686,11 @@ func (s *Sandbox) findContainer(containerID string) (*Container, error) {
// sandbox structure, based on a container ID.
func (s *Sandbox) removeContainer(containerID string) error {
if s == nil {
return errNeedSandbox
return vcTypes.ErrNeedSandbox
}
if containerID == "" {
return errNeedContainerID
return vcTypes.ErrNeedContainerID
}
if _, ok := s.containers[containerID]; !ok {
@ -1130,7 +1130,7 @@ func (s *Sandbox) KillContainer(containerID string, signal syscall.Signal, all b
// DeleteContainer deletes a container from the sandbox
func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
if containerID == "" {
return nil, errNeedContainerID
return nil, vcTypes.ErrNeedContainerID
}
// Fetch the container.
@ -1178,7 +1178,7 @@ func (s *Sandbox) ProcessListContainer(containerID string, options ProcessListOp
// TODO: update container status properly, see kata-containers/runtime#253
func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
if containerID == "" {
return ContainerStatus{}, errNeedContainerID
return ContainerStatus{}, vcTypes.ErrNeedContainerID
}
for id, c := range s.containers {
@ -1198,7 +1198,7 @@ func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
}
}
return ContainerStatus{}, errNoSuchContainer
return ContainerStatus{}, vcTypes.ErrNoSuchContainer
}
// EnterContainer is the virtcontainers container command execution entry point.
@ -1405,7 +1405,7 @@ func (s *Sandbox) enter(args []string) error {
// sandbox.
func (s *Sandbox) setSandboxState(state types.StateString) error {
if state == "" {
return errNeedState
return vcTypes.ErrNeedState
}
// update in-memory state
@ -1467,7 +1467,7 @@ func (s *Sandbox) decrementSandboxBlockIndex() error {
func (s *Sandbox) setContainersState(state types.StateString) error {
if state == "" {
return errNeedState
return vcTypes.ErrNeedState
}
for _, c := range s.containers {
@ -1485,7 +1485,7 @@ func togglePauseSandbox(ctx context.Context, sandboxID string, pause bool) (*San
defer span.Finish()
if sandboxID == "" {
return nil, errNeedSandbox
return nil, vcTypes.ErrNeedSandbox
}
lockFile, err := rwLockSandbox(ctx, sandboxID)