Use errors.Cause() when looking at storage errors

The storage library uses github.com/pkg/errors to wrap errors that it
returns from many of its functions, so when passing them to
os.IsNotExist() or comparing them to specific errors defined in the
storage library, unwrap them using errors.Cause().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2017-08-02 11:17:45 -04:00
parent d4f023918c
commit 0b7348b35c
8 changed files with 21 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import (
"github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/kubernetes-incubator/cri-o/pkg/storage"
rspec "github.com/opencontainers/runtime-spec/specs-go" rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
) )
@ -156,7 +157,7 @@ func (c *ContainerServer) Update() error {
defer c.updateLock.Unlock() defer c.updateLock.Unlock()
containers, err := c.store.Containers() containers, err := c.store.Containers()
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(errors.Cause(err)) {
logrus.Warnf("could not read containers and sandboxes: %v", err) logrus.Warnf("could not read containers and sandboxes: %v", err)
return err return err
} }

View file

@ -365,7 +365,7 @@ func GetContainerCopyData(store storage.Store, name string) (*CopyData, error) {
var err error var err error
if name != "" { if name != "" {
data, err = openCopyData(store, name) data, err = openCopyData(store, name)
if os.IsNotExist(err) { if os.IsNotExist(errors.Cause(err)) {
data, err = importCopyData(store, name, "") data, err = importCopyData(store, name, "")
} }
} }

View file

@ -2,7 +2,6 @@ package storage
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"time" "time"
@ -13,6 +12,7 @@ import (
"github.com/containers/image/types" "github.com/containers/image/types"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/opencontainers/image-spec/specs-go/v1" "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
) )
var ( var (
@ -175,7 +175,7 @@ func (r *runtimeService) createContainerOrPodSandbox(systemContext *types.System
} }
} }
img, err := istorage.Transport.GetStoreImage(r.storageImageServer.GetStore(), ref) img, err := istorage.Transport.GetStoreImage(r.storageImageServer.GetStore(), ref)
if img == nil && err == storage.ErrImageUnknown && imageName == r.pauseImage { if img == nil && errors.Cause(err) == storage.ErrImageUnknown && imageName == r.pauseImage {
image := imageID image := imageID
if imageName != "" { if imageName != "" {
image = imageName image = imageName
@ -194,7 +194,7 @@ func (r *runtimeService) createContainerOrPodSandbox(systemContext *types.System
} }
logrus.Debugf("successfully pulled image %q", image) logrus.Debugf("successfully pulled image %q", image)
} }
if img == nil && err == storage.ErrImageUnknown { if img == nil && errors.Cause(err) == storage.ErrImageUnknown {
if imageID == "" { if imageID == "" {
return ContainerInfo{}, fmt.Errorf("image %q not present in image store", imageName) return ContainerInfo{}, fmt.Errorf("image %q not present in image store", imageName)
} }
@ -331,7 +331,7 @@ func (r *runtimeService) CreateContainer(systemContext *types.SystemContext, pod
func (r *runtimeService) RemovePodSandbox(idOrName string) error { func (r *runtimeService) RemovePodSandbox(idOrName string) error {
container, err := r.storageImageServer.GetStore().Container(idOrName) container, err := r.storageImageServer.GetStore().Container(idOrName)
if err != nil { if err != nil {
if err == storage.ErrContainerUnknown { if errors.Cause(err) == storage.ErrContainerUnknown {
return ErrInvalidSandboxID return ErrInvalidSandboxID
} }
return err return err
@ -384,7 +384,7 @@ func (r *runtimeService) GetContainerMetadata(idOrName string) (RuntimeContainer
func (r *runtimeService) StartContainer(idOrName string) (string, error) { func (r *runtimeService) StartContainer(idOrName string) (string, error) {
container, err := r.storageImageServer.GetStore().Container(idOrName) container, err := r.storageImageServer.GetStore().Container(idOrName)
if err != nil { if err != nil {
if err == storage.ErrContainerUnknown { if errors.Cause(err) == storage.ErrContainerUnknown {
return "", ErrInvalidContainerID return "", ErrInvalidContainerID
} }
return "", err return "", err
@ -422,7 +422,7 @@ func (r *runtimeService) StopContainer(idOrName string) error {
func (r *runtimeService) GetWorkDir(id string) (string, error) { func (r *runtimeService) GetWorkDir(id string) (string, error) {
container, err := r.storageImageServer.GetStore().Container(id) container, err := r.storageImageServer.GetStore().Container(id)
if err != nil { if err != nil {
if err == storage.ErrContainerUnknown { if errors.Cause(err) == storage.ErrContainerUnknown {
return "", ErrInvalidContainerID return "", ErrInvalidContainerID
} }
return "", err return "", err
@ -433,7 +433,7 @@ func (r *runtimeService) GetWorkDir(id string) (string, error) {
func (r *runtimeService) GetRunDir(id string) (string, error) { func (r *runtimeService) GetRunDir(id string) (string, error) {
container, err := r.storageImageServer.GetStore().Container(id) container, err := r.storageImageServer.GetStore().Container(id)
if err != nil { if err != nil {
if err == storage.ErrContainerUnknown { if errors.Cause(err) == storage.ErrContainerUnknown {
return "", ErrInvalidContainerID return "", ErrInvalidContainerID
} }
return "", err return "", err

View file

@ -6,6 +6,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
) )
@ -34,7 +35,7 @@ func (s *Server) ImageStatus(ctx context.Context, req *pb.ImageStatusRequest) (*
image = images[0] image = images[0]
status, err := s.StorageImageServer().ImageStatus(s.ImageContext(), image) status, err := s.StorageImageServer().ImageStatus(s.ImageContext(), image)
if err != nil { if err != nil {
if err == storage.ErrImageUnknown { if errors.Cause(err) == storage.ErrImageUnknown {
return &pb.ImageStatusResponse{}, nil return &pb.ImageStatusResponse{}, nil
} }
return nil, err return nil, err

View file

@ -8,6 +8,7 @@ import (
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox" "github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
"github.com/kubernetes-incubator/cri-o/oci" "github.com/kubernetes-incubator/cri-o/oci"
pkgstorage "github.com/kubernetes-incubator/cri-o/pkg/storage" pkgstorage "github.com/kubernetes-incubator/cri-o/pkg/storage"
"github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
) )
@ -75,7 +76,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
s.removeContainer(podInfraContainer) s.removeContainer(podInfraContainer)
// Remove the files related to the sandbox // Remove the files related to the sandbox
if err := s.StorageRuntimeServer().StopContainer(sb.ID()); err != nil && err != storage.ErrContainerUnknown { if err := s.StorageRuntimeServer().StopContainer(sb.ID()); err != nil && errors.Cause(err) != storage.ErrContainerUnknown {
logrus.Warnf("failed to stop sandbox container in pod sandbox %s: %v", sb.ID(), err) logrus.Warnf("failed to stop sandbox container in pod sandbox %s: %v", sb.ID(), err)
} }
if err := s.StorageRuntimeServer().RemovePodSandbox(sb.ID()); err != nil && err != pkgstorage.ErrInvalidSandboxID { if err := s.StorageRuntimeServer().RemovePodSandbox(sb.ID()); err != nil && err != pkgstorage.ErrInvalidSandboxID {

View file

@ -19,6 +19,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups/systemd" "github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
@ -162,7 +163,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
namespace, namespace,
attempt, attempt,
nil) nil)
if err == storage.ErrDuplicateName { if errors.Cause(err) == storage.ErrDuplicateName {
return nil, fmt.Errorf("pod sandbox with name %q already exists", name) return nil, fmt.Errorf("pod sandbox with name %q already exists", name)
} }
if err != nil { if err != nil {

View file

@ -11,6 +11,7 @@ import (
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox" "github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
"github.com/kubernetes-incubator/cri-o/oci" "github.com/kubernetes-incubator/cri-o/oci"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
"github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
@ -80,7 +81,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
if c.ID() == podInfraContainer.ID() { if c.ID() == podInfraContainer.ID() {
continue continue
} }
if err := s.StorageRuntimeServer().StopContainer(c.ID()); err != nil && err != storage.ErrContainerUnknown { if err := s.StorageRuntimeServer().StopContainer(c.ID()); err != nil && errors.Cause(err) != storage.ErrContainerUnknown {
// assume container already umounted // assume container already umounted
logrus.Warnf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err) logrus.Warnf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err)
} }
@ -108,7 +109,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
} }
} }
} }
if err := s.StorageRuntimeServer().StopContainer(sb.ID()); err != nil && err != storage.ErrContainerUnknown { if err := s.StorageRuntimeServer().StopContainer(sb.ID()); err != nil && errors.Cause(err) != storage.ErrContainerUnknown {
logrus.Warnf("failed to stop sandbox container in pod sandbox %s: %v", sb.ID(), err) logrus.Warnf("failed to stop sandbox container in pod sandbox %s: %v", sb.ID(), err)
} }

View file

@ -16,6 +16,7 @@ import (
"github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/kubernetes-incubator/cri-o/pkg/storage"
"github.com/kubernetes-incubator/cri-o/server/apparmor" "github.com/kubernetes-incubator/cri-o/server/apparmor"
"github.com/kubernetes-incubator/cri-o/server/seccomp" "github.com/kubernetes-incubator/cri-o/server/seccomp"
"github.com/pkg/errors"
knet "k8s.io/apimachinery/pkg/util/net" knet "k8s.io/apimachinery/pkg/util/net"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
"k8s.io/kubernetes/pkg/kubelet/network/hostport" "k8s.io/kubernetes/pkg/kubelet/network/hostport"
@ -77,7 +78,7 @@ func (s *Server) GetPortForward(req *pb.PortForwardRequest) (*pb.PortForwardResp
func (s *Server) restore() { func (s *Server) restore() {
containers, err := s.Store().Containers() containers, err := s.Store().Containers()
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(errors.Cause(err)) {
logrus.Warnf("could not read containers and sandboxes: %v", err) logrus.Warnf("could not read containers and sandboxes: %v", err)
} }
pods := map[string]*storage.RuntimeContainerMetadata{} pods := map[string]*storage.RuntimeContainerMetadata{}