switch usage directly to errdefs.(ErrAlreadyExists and ErrNotFound)

Signed-off-by: Zou Nengren <zouyee1989@gmail.com>
This commit is contained in:
zounengren
2021-09-24 18:26:58 +08:00
parent 45e0e5a77e
commit fcffe0c83a
19 changed files with 87 additions and 91 deletions

View File

@@ -20,12 +20,13 @@ import (
"sync"
"github.com/containerd/containerd"
"github.com/containerd/containerd/pkg/cri/store/label"
"github.com/containerd/containerd/pkg/cri/store/truncindex"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd/errdefs"
cio "github.com/containerd/containerd/pkg/cri/io"
"github.com/containerd/containerd/pkg/cri/store"
"github.com/containerd/containerd/pkg/cri/store/label"
"github.com/containerd/containerd/pkg/cri/store/truncindex"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
)
// Container contains all resources associated with the container. All methods to
@@ -124,7 +125,7 @@ func (s *Store) Add(c Container) error {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.containers[c.ID]; ok {
return store.ErrAlreadyExist
return errdefs.ErrAlreadyExists
}
if err := s.labels.Reserve(c.ProcessLabel); err != nil {
return err
@@ -144,14 +145,14 @@ func (s *Store) Get(id string) (Container, error) {
id, err := s.idIndex.Get(id)
if err != nil {
if err == truncindex.ErrNotExist {
err = store.ErrNotExist
err = errdefs.ErrNotFound
}
return Container{}, err
}
if c, ok := s.containers[id]; ok {
return c, nil
}
return Container{}, store.ErrNotExist
return Container{}, errdefs.ErrNotFound
}
// List lists all containers.

View File

@@ -21,13 +21,13 @@ import (
"testing"
"time"
"github.com/containerd/containerd/errdefs"
cio "github.com/containerd/containerd/pkg/cri/io"
"github.com/containerd/containerd/pkg/cri/store/label"
"github.com/opencontainers/selinux/go-selinux"
assertlib "github.com/stretchr/testify/assert"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
cio "github.com/containerd/containerd/pkg/cri/io"
"github.com/containerd/containerd/pkg/cri/store"
)
func TestContainerStore(t *testing.T) {
@@ -183,7 +183,7 @@ func TestContainerStore(t *testing.T) {
truncID := genTruncIndex(testID)
t.Logf("add should return already exists error for duplicated container")
assert.Equal(store.ErrAlreadyExist, s.Add(v))
assert.Equal(errdefs.ErrAlreadyExists, s.Add(v))
t.Logf("should be able to delete container")
s.Delete(truncID)
@@ -194,7 +194,7 @@ func TestContainerStore(t *testing.T) {
t.Logf("get should return not exist error after deletion")
c, err := s.Get(truncID)
assert.Equal(Container{}, c)
assert.Equal(store.ErrNotExist, err)
assert.Equal(errdefs.ErrNotFound, err)
}
if selinux.GetEnabled() {

View File

@@ -19,14 +19,14 @@ package store
import (
"testing"
"github.com/containerd/containerd/errdefs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/containerd/containerd/errdefs"
)
func TestStoreErrAlreadyExistGRPCStatus(t *testing.T) {
err := errdefs.ToGRPC(ErrAlreadyExist)
err := errdefs.ToGRPC(errdefs.ErrAlreadyExists)
s, ok := status.FromError(err)
if !ok {
t.Fatalf("failed to convert err: %v to status: %d", err, codes.AlreadyExists)
@@ -37,7 +37,7 @@ func TestStoreErrAlreadyExistGRPCStatus(t *testing.T) {
}
func TestStoreErrNotExistGRPCStatus(t *testing.T) {
err := errdefs.ToGRPC(ErrNotExist)
err := errdefs.ToGRPC(errdefs.ErrNotFound)
s, ok := status.FromError(err)
if !ok {
t.Fatalf("failed to convert err: %v to status: %d", err, codes.NotFound)

View File

@@ -24,14 +24,13 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/pkg/cri/util"
imagedigest "github.com/opencontainers/go-digest"
"github.com/opencontainers/go-digest/digestset"
imageidentity "github.com/opencontainers/image-spec/identity"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
storeutil "github.com/containerd/containerd/pkg/cri/store"
"github.com/containerd/containerd/pkg/cri/util"
)
// Image contains all resources associated with the image. All fields
@@ -159,7 +158,7 @@ func (s *Store) Resolve(ref string) (string, error) {
defer s.lock.RUnlock()
id, ok := s.refCache[ref]
if !ok {
return "", storeutil.ErrNotExist
return "", errdefs.ErrNotFound
}
return id, nil
}
@@ -222,14 +221,14 @@ func (s *store) get(id string) (Image, error) {
digest, err := s.digestSet.Lookup(id)
if err != nil {
if err == digestset.ErrDigestNotFound {
err = storeutil.ErrNotExist
err = errdefs.ErrNotFound
}
return Image{}, err
}
if i, ok := s.images[digest.String()]; ok {
return i, nil
}
return Image{}, storeutil.ErrNotExist
return Image{}, errdefs.ErrNotFound
}
func (s *store) delete(id, ref string) {

View File

@@ -21,10 +21,10 @@ import (
"strings"
"testing"
"github.com/containerd/containerd/errdefs"
"github.com/opencontainers/go-digest/digestset"
assertlib "github.com/stretchr/testify/assert"
storeutil "github.com/containerd/containerd/pkg/cri/store"
)
func TestInternalStore(t *testing.T) {
@@ -128,7 +128,7 @@ func TestInternalStore(t *testing.T) {
t.Logf("should be able to delete image")
s.delete(truncID, newRef)
got, err = s.get(truncID)
assert.Equal(storeutil.ErrNotExist, err)
assert.Equal(errdefs.ErrNotFound, err)
assert.Equal(Image{}, got)
imageNum--
@@ -241,7 +241,7 @@ func TestImageStore(t *testing.T) {
if test.image == nil {
// Shouldn't be able to index by removed ref.
id, err := s.Resolve(test.ref)
assert.Equal(storeutil.ErrNotExist, err)
assert.Equal(errdefs.ErrNotFound, err)
assert.Empty(id)
}
}

View File

@@ -20,10 +20,10 @@ import (
"sync"
"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/pkg/cri/store"
"github.com/containerd/containerd/pkg/cri/store/label"
"github.com/containerd/containerd/pkg/cri/store/truncindex"
"github.com/containerd/containerd/pkg/cri/store"
"github.com/containerd/containerd/pkg/netns"
)
@@ -80,7 +80,7 @@ func (s *Store) Add(sb Sandbox) error {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.sandboxes[sb.ID]; ok {
return store.ErrAlreadyExist
return errdefs.ErrAlreadyExists
}
if err := s.labels.Reserve(sb.ProcessLabel); err != nil {
return err
@@ -100,14 +100,14 @@ func (s *Store) Get(id string) (Sandbox, error) {
id, err := s.idIndex.Get(id)
if err != nil {
if err == truncindex.ErrNotExist {
err = store.ErrNotExist
err = errdefs.ErrNotFound
}
return Sandbox{}, err
}
if sb, ok := s.sandboxes[id]; ok {
return sb, nil
}
return Sandbox{}, store.ErrNotExist
return Sandbox{}, errdefs.ErrNotFound
}
// List lists all sandboxes.

View File

@@ -19,11 +19,11 @@ package sandbox
import (
"testing"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/pkg/cri/store/label"
assertlib "github.com/stretchr/testify/assert"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
"github.com/containerd/containerd/pkg/cri/store"
)
func TestSandboxStore(t *testing.T) {
@@ -140,7 +140,7 @@ func TestSandboxStore(t *testing.T) {
truncID := genTruncIndex(testID)
t.Logf("add should return already exists error for duplicated sandbox")
assert.Equal(store.ErrAlreadyExist, s.Add(v))
assert.Equal(errdefs.ErrAlreadyExists, s.Add(v))
t.Logf("should be able to delete sandbox")
s.Delete(truncID)
@@ -151,6 +151,6 @@ func TestSandboxStore(t *testing.T) {
t.Logf("get should return not exist error after deletion")
sb, err := s.Get(truncID)
assert.Equal(Sandbox{}, sb)
assert.Equal(store.ErrNotExist, err)
assert.Equal(errdefs.ErrNotFound, err)
}
}

View File

@@ -19,9 +19,8 @@ package snapshot
import (
"sync"
"github.com/containerd/containerd/errdefs"
snapshot "github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/pkg/cri/store"
)
// Snapshot contains the information about the snapshot.
@@ -65,7 +64,7 @@ func (s *Store) Get(key string) (Snapshot, error) {
if sn, ok := s.snapshots[key]; ok {
return sn, nil
}
return Snapshot{}, store.ErrNotExist
return Snapshot{}, errdefs.ErrNotFound
}
// List lists all snapshots.

View File

@@ -20,10 +20,10 @@ import (
"testing"
"time"
"github.com/containerd/containerd/errdefs"
snapshot "github.com/containerd/containerd/snapshots"
assertlib "github.com/stretchr/testify/assert"
"github.com/containerd/containerd/pkg/cri/store"
assertlib "github.com/stretchr/testify/assert"
)
func TestSnapshotStore(t *testing.T) {
@@ -80,5 +80,5 @@ func TestSnapshotStore(t *testing.T) {
t.Logf("get should return empty struct and ErrNotExist after deletion")
sn, err := s.Get(testKey)
assert.Equal(Snapshot{}, sn)
assert.Equal(store.ErrNotExist, err)
assert.Equal(errdefs.ErrNotFound, err)
}