From cf9075163848e9a7c8ee4dbcf2f61743283671e5 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Fri, 12 Apr 2019 02:01:02 -0700 Subject: [PATCH] vc: export vc error types So that shimv2 can convert it into grpc errors. Signed-off-by: Peng Tao --- virtcontainers/api.go | 62 +++++++++++++++--------------- virtcontainers/container.go | 3 +- virtcontainers/errors.go | 19 --------- virtcontainers/pkg/types/errors.go | 19 +++++++++ virtcontainers/sandbox.go | 22 +++++------ 5 files changed, 63 insertions(+), 62 deletions(-) delete mode 100644 virtcontainers/errors.go create mode 100644 virtcontainers/pkg/types/errors.go diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 5466d5f6d..8925f458f 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -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) diff --git a/virtcontainers/container.go b/virtcontainers/container.go index 083186f03..49e477278 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -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) diff --git a/virtcontainers/errors.go b/virtcontainers/errors.go deleted file mode 100644 index 774ce2e31..000000000 --- a/virtcontainers/errors.go +++ /dev/null @@ -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") -) diff --git a/virtcontainers/pkg/types/errors.go b/virtcontainers/pkg/types/errors.go new file mode 100644 index 000000000..214022708 --- /dev/null +++ b/virtcontainers/pkg/types/errors.go @@ -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") +) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 7f88973ad..b398565db 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -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)