X Tutup
Skip to content

Commit fcffe0c

Browse files
author
zounengren
committed
switch usage directly to errdefs.(ErrAlreadyExists and ErrNotFound)
Signed-off-by: Zou Nengren <zouyee1989@gmail.com>
1 parent 45e0e5a commit fcffe0c

File tree

19 files changed

+86
-90
lines changed

19 files changed

+86
-90
lines changed

.empty-mod/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
// containerd to be the source of truth, helping us catch missing go.mod rules,
1010
// or version changes early.
1111
module empty-mod
12+
13+
go 1.13

pkg/cri/server/container_remove.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ import (
2020
"github.com/containerd/containerd"
2121
"github.com/containerd/containerd/errdefs"
2222
"github.com/containerd/containerd/log"
23+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
24+
2325
"github.com/pkg/errors"
2426
"github.com/sirupsen/logrus"
2527
"golang.org/x/net/context"
2628
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
27-
28-
"github.com/containerd/containerd/pkg/cri/store"
29-
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3029
)
3130

3231
// RemoveContainer removes the container.
3332
func (c *criService) RemoveContainer(ctx context.Context, r *runtime.RemoveContainerRequest) (_ *runtime.RemoveContainerResponse, retErr error) {
3433
container, err := c.containerStore.Get(r.GetContainerId())
3534
if err != nil {
36-
if err != store.ErrNotExist {
35+
if !errdefs.IsNotFound(err) {
3736
return nil, errors.Wrapf(err, "an error occurred when try to find container %q", r.GetContainerId())
3837
}
3938
// Do not return error if container metadata doesn't exist.

pkg/cri/server/container_status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package server
1919
import (
2020
"encoding/json"
2121

22+
"github.com/containerd/containerd/errdefs"
23+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
24+
2225
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
2326
"github.com/pkg/errors"
2427
"golang.org/x/net/context"
2528
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
26-
27-
"github.com/containerd/containerd/pkg/cri/store"
28-
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
2929
)
3030

3131
// ContainerStatus inspects the container and returns the status.
@@ -44,7 +44,7 @@ func (c *criService) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
4444
imageRef := container.ImageRef
4545
image, err := c.imageStore.Get(imageRef)
4646
if err != nil {
47-
if err != store.ErrNotExist {
47+
if !errdefs.IsNotFound(err) {
4848
return nil, errors.Wrapf(err, "failed to get image %q", imageRef)
4949
}
5050
} else {

pkg/cri/server/container_stop.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ import (
2424
eventtypes "github.com/containerd/containerd/api/events"
2525
"github.com/containerd/containerd/errdefs"
2626
"github.com/containerd/containerd/log"
27-
"github.com/pkg/errors"
28-
"golang.org/x/net/context"
29-
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
30-
31-
"github.com/containerd/containerd/pkg/cri/store"
3227
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3328
ctrdutil "github.com/containerd/containerd/pkg/cri/util"
29+
3430
"github.com/moby/sys/signal"
31+
"github.com/pkg/errors"
32+
"golang.org/x/net/context"
33+
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
3534
)
3635

3736
// StopContainer stops a running container with a grace period (i.e., timeout).
@@ -116,7 +115,7 @@ func (c *criService) stopContainer(ctx context.Context, container containerstore
116115
// TODO(random-liu): Remove this logic when containerd 1.2 is deprecated.
117116
image, err := c.imageStore.Get(container.ImageRef)
118117
if err != nil {
119-
if err != store.ErrNotExist {
118+
if !errdefs.IsNotFound(err) {
120119
return errors.Wrapf(err, "failed to get image %q", container.ImageRef)
121120
}
122121
log.G(ctx).Warningf("Image %q not found, stop container with signal %q", container.ImageRef, stopSignal)

pkg/cri/server/events.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ import (
2525
containerdio "github.com/containerd/containerd/cio"
2626
"github.com/containerd/containerd/errdefs"
2727
"github.com/containerd/containerd/events"
28+
"github.com/containerd/containerd/pkg/cri/constants"
29+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
30+
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
31+
ctrdutil "github.com/containerd/containerd/pkg/cri/util"
2832
"github.com/containerd/typeurl"
33+
2934
gogotypes "github.com/gogo/protobuf/types"
3035
"github.com/pkg/errors"
3136
"github.com/sirupsen/logrus"
3237
"golang.org/x/net/context"
3338
"k8s.io/apimachinery/pkg/util/clock"
34-
35-
"github.com/containerd/containerd/pkg/cri/constants"
36-
"github.com/containerd/containerd/pkg/cri/store"
37-
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
38-
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
39-
ctrdutil "github.com/containerd/containerd/pkg/cri/util"
4039
)
4140

4241
const (
@@ -141,7 +140,7 @@ func (em *eventMonitor) startSandboxExitMonitor(ctx context.Context, id string,
141140
return err
142141
}
143142
return nil
144-
} else if err != store.ErrNotExist {
143+
} else if !errdefs.IsNotFound(err) {
145144
return errors.Wrapf(err, "failed to get sandbox %s", e.ID)
146145
}
147146
return nil
@@ -192,7 +191,7 @@ func (em *eventMonitor) startContainerExitMonitor(ctx context.Context, id string
192191
return err
193192
}
194193
return nil
195-
} else if err != store.ErrNotExist {
194+
} else if !errdefs.IsNotFound(err) {
196195
return errors.Wrapf(err, "failed to get container %s", e.ID)
197196
}
198197
return nil
@@ -318,7 +317,7 @@ func (em *eventMonitor) handleEvent(any interface{}) error {
318317
return errors.Wrap(err, "failed to handle container TaskExit event")
319318
}
320319
return nil
321-
} else if err != store.ErrNotExist {
320+
} else if !errdefs.IsNotFound(err) {
322321
return errors.Wrap(err, "can't find container for TaskExit event")
323322
}
324323
sb, err := em.c.sandboxStore.Get(e.ID)
@@ -327,7 +326,7 @@ func (em *eventMonitor) handleEvent(any interface{}) error {
327326
return errors.Wrap(err, "failed to handle sandbox TaskExit event")
328327
}
329328
return nil
330-
} else if err != store.ErrNotExist {
329+
} else if !errdefs.IsNotFound(err) {
331330
return errors.Wrap(err, "can't find sandbox for TaskExit event")
332331
}
333332
return nil
@@ -336,7 +335,7 @@ func (em *eventMonitor) handleEvent(any interface{}) error {
336335
// For TaskOOM, we only care which container it belongs to.
337336
cntr, err := em.c.containerStore.Get(e.ContainerID)
338337
if err != nil {
339-
if err != store.ErrNotExist {
338+
if !errdefs.IsNotFound(err) {
340339
return errors.Wrap(err, "can't find container for TaskOOM event")
341340
}
342341
return nil

pkg/cri/server/helpers.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ import (
2323
"strconv"
2424
"strings"
2525

26-
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
2726
"github.com/containerd/containerd"
2827
"github.com/containerd/containerd/containers"
28+
"github.com/containerd/containerd/errdefs"
29+
criconfig "github.com/containerd/containerd/pkg/cri/config"
30+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
31+
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
32+
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
33+
runtimeoptions "github.com/containerd/containerd/pkg/runtimeoptions/v1"
2934
"github.com/containerd/containerd/plugin"
3035
"github.com/containerd/containerd/reference/docker"
3136
"github.com/containerd/containerd/runtime/linux/runctypes"
3237
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
3338
"github.com/containerd/typeurl"
39+
40+
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
3441
imagedigest "github.com/opencontainers/go-digest"
3542
"github.com/pelletier/go-toml"
3643
"github.com/pkg/errors"
3744
"golang.org/x/net/context"
3845
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
39-
40-
criconfig "github.com/containerd/containerd/pkg/cri/config"
41-
"github.com/containerd/containerd/pkg/cri/store"
42-
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
43-
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
44-
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
45-
runtimeoptions "github.com/containerd/containerd/pkg/runtimeoptions/v1"
4646
)
4747

4848
const (
@@ -222,7 +222,7 @@ func getUserFromImage(user string) (*int64, string) {
222222
// pulled yet, the function will pull the image.
223223
func (c *criService) ensureImageExists(ctx context.Context, ref string, config *runtime.PodSandboxConfig) (*imagestore.Image, error) {
224224
image, err := c.localResolve(ref)
225-
if err != nil && err != store.ErrNotExist {
225+
if err != nil && !errdefs.IsNotFound(err) {
226226
return nil, errors.Wrapf(err, "failed to get image %q", ref)
227227
}
228228
if err == nil {

pkg/cri/server/helpers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/containerd/containerd/errdefs"
2526
"github.com/containerd/containerd/oci"
27+
criconfig "github.com/containerd/containerd/pkg/cri/config"
28+
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
29+
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
2630
"github.com/containerd/containerd/plugin"
2731
"github.com/containerd/containerd/reference/docker"
2832
"github.com/containerd/containerd/runtime/linux/runctypes"
2933
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
34+
3035
imagedigest "github.com/opencontainers/go-digest"
3136
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
3237
"github.com/pelletier/go-toml"
3338
"github.com/stretchr/testify/assert"
3439
"github.com/stretchr/testify/require"
35-
36-
criconfig "github.com/containerd/containerd/pkg/cri/config"
37-
"github.com/containerd/containerd/pkg/cri/store"
38-
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
39-
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
4040
)
4141

4242
// TestGetUserFromImage tests the logic of getting image uid or user name of image user.
@@ -189,7 +189,7 @@ func TestLocalResolve(t *testing.T) {
189189
assert.Equal(t, image, img)
190190
}
191191
img, err := c.localResolve("randomid")
192-
assert.Equal(t, store.ErrNotExist, err)
192+
assert.Equal(t, errdefs.IsNotFound(err), true)
193193
assert.Equal(t, imagestore.Image{}, img)
194194
}
195195

pkg/cri/server/image_remove.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package server
1919
import (
2020
"github.com/containerd/containerd/errdefs"
2121
"github.com/containerd/containerd/images"
22+
2223
"github.com/pkg/errors"
2324
"golang.org/x/net/context"
2425
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
25-
26-
"github.com/containerd/containerd/pkg/cri/store"
2726
)
2827

2928
// RemoveImage removes the image.
@@ -35,7 +34,7 @@ import (
3534
func (c *criService) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequest) (*runtime.RemoveImageResponse, error) {
3635
image, err := c.localResolve(r.GetImage().GetImage())
3736
if err != nil {
38-
if err == store.ErrNotExist {
37+
if errdefs.IsNotFound(err) {
3938
// return empty without error when image not found.
4039
return &runtime.RemoveImageResponse{}, nil
4140
}

pkg/cri/server/image_status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ package server
1919
import (
2020
"encoding/json"
2121

22+
"github.com/containerd/containerd/errdefs"
2223
"github.com/containerd/containerd/log"
24+
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
25+
2326
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2427
"github.com/pkg/errors"
2528
"golang.org/x/net/context"
2629
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
27-
28-
"github.com/containerd/containerd/pkg/cri/store"
29-
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
3030
)
3131

3232
// ImageStatus returns the status of the image, returns nil if the image isn't present.
@@ -35,7 +35,7 @@ import (
3535
func (c *criService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequest) (*runtime.ImageStatusResponse, error) {
3636
image, err := c.localResolve(r.GetImage().GetImage())
3737
if err != nil {
38-
if err == store.ErrNotExist {
38+
if errdefs.IsNotFound(err) {
3939
// return empty without error when image not found.
4040
return &runtime.ImageStatusResponse{}, nil
4141
}

pkg/cri/server/sandbox_remove.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ import (
2020
"github.com/containerd/containerd"
2121
"github.com/containerd/containerd/errdefs"
2222
"github.com/containerd/containerd/log"
23+
2324
"github.com/pkg/errors"
2425
"github.com/sirupsen/logrus"
2526
"golang.org/x/net/context"
2627
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
27-
28-
"github.com/containerd/containerd/pkg/cri/store"
2928
)
3029

3130
// RemovePodSandbox removes the sandbox. If there are running containers in the
3231
// sandbox, they should be forcibly removed.
3332
func (c *criService) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodSandboxRequest) (*runtime.RemovePodSandboxResponse, error) {
3433
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
3534
if err != nil {
36-
if err != store.ErrNotExist {
35+
if !errdefs.IsNotFound(err) {
3736
return nil, errors.Wrapf(err, "an error occurred when try to find sandbox %q",
3837
r.GetPodSandboxId())
3938
}

0 commit comments

Comments
 (0)
X Tutup