Skip to content

Commit

Permalink
Merge pull request #6937 from mythi/sandbox-errors
Browse files Browse the repository at this point in the history
sandbox: replace github.com/pkg/errors with native errors
  • Loading branch information
dmcgowan committed May 26, 2022
2 parents da2db02 + 523d069 commit c4e2902
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ require (
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/opencontainers/selinux v1.10.1
github.com/pelletier/go-toml v1.9.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
Expand Down Expand Up @@ -104,6 +103,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/runtime-tools v0.0.0-20190417131837-cd1349b7c47e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down
32 changes: 16 additions & 16 deletions metadata/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package metadata

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/containerd/containerd/namespaces"
api "github.com/containerd/containerd/sandbox"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
)

Expand All @@ -55,7 +55,7 @@ func (s *sandboxStore) Create(ctx context.Context, sandbox api.Sandbox) (api.San
sandbox.UpdatedAt = sandbox.CreatedAt

if err := s.validate(&sandbox); err != nil {
return api.Sandbox{}, errors.Wrap(err, "failed to validate sandbox")
return api.Sandbox{}, fmt.Errorf("failed to validate sandbox: %w", err)
}

if err := s.db.Update(func(tx *bbolt.Tx) error {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpat
if err := update(ctx, s.db, func(tx *bbolt.Tx) error {
parent := getSandboxBucket(tx, ns)
if parent == nil {
return errors.Wrap(errdefs.ErrNotFound, "no sandbox buckets")
return fmt.Errorf("no sandbox buckets: %w", errdefs.ErrNotFound)
}

updated, err := s.read(parent, []byte(sandbox.ID))
Expand All @@ -99,7 +99,7 @@ func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpat
fieldpaths = []string{"labels", "extensions", "spec", "runtime"}

if updated.Runtime.Name != sandbox.Runtime.Name {
return errors.Wrapf(errdefs.ErrInvalidArgument, "sandbox.Runtime.Name field is immutable")
return fmt.Errorf("sandbox.Runtime.Name field is immutable: %w", errdefs.ErrInvalidArgument)
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (s *sandboxStore) Update(ctx context.Context, sandbox api.Sandbox, fieldpat
case "spec":
updated.Spec = sandbox.Spec
default:
return errors.Wrapf(errdefs.ErrInvalidArgument, "cannot update %q field on sandbox %q", path, sandbox.ID)
return fmt.Errorf("cannot update %q field on sandbox %q: %w", path, sandbox.ID, errdefs.ErrInvalidArgument)
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *sandboxStore) Get(ctx context.Context, id string) (api.Sandbox, error)
if err := view(ctx, s.db, func(tx *bbolt.Tx) error {
bucket := getSandboxBucket(tx, ns)
if bucket == nil {
return errors.Wrap(errdefs.ErrNotFound, "no sandbox buckets")
return fmt.Errorf("no sandbox buckets: %w", errdefs.ErrNotFound)
}

out, err := s.read(bucket, []byte(id))
Expand All @@ -192,7 +192,7 @@ func (s *sandboxStore) List(ctx context.Context, fields ...string) ([]api.Sandbo

filter, err := filters.ParseAll(fields...)
if err != nil {
return nil, errors.Wrap(errdefs.ErrInvalidArgument, err.Error())
return nil, fmt.Errorf("%s: %w", err.Error(), errdefs.ErrInvalidArgument)
}

var (
Expand All @@ -209,7 +209,7 @@ func (s *sandboxStore) List(ctx context.Context, fields ...string) ([]api.Sandbo
if err := bucket.ForEach(func(k, v []byte) error {
info, err := s.read(bucket, k)
if err != nil {
return errors.Wrapf(err, "failed to read bucket %q", string(k))
return fmt.Errorf("failed to read bucket %q: %w", string(k), err)
}

if filter.Match(adaptSandbox(&info)) {
Expand Down Expand Up @@ -239,11 +239,11 @@ func (s *sandboxStore) Delete(ctx context.Context, id string) error {
if err := update(ctx, s.db, func(tx *bbolt.Tx) error {
buckets := getSandboxBucket(tx, ns)
if buckets == nil {
return errors.Wrap(errdefs.ErrNotFound, "no sandbox buckets")
return fmt.Errorf("no sandbox buckets: %w", errdefs.ErrNotFound)
}

if err := buckets.DeleteBucket([]byte(id)); err != nil {
return errors.Wrapf(err, "failed to delete sandbox %q", id)
return fmt.Errorf("failed to delete sandbox %q: %w", id, err)
}

return nil
Expand All @@ -269,7 +269,7 @@ func (s *sandboxStore) write(parent *bbolt.Bucket, instance *api.Sandbox, overwr
} else {
bucket = parent.Bucket(id)
if bucket != nil {
return errors.Wrapf(errdefs.ErrAlreadyExists, "sandbox bucket %q already exists", instance.ID)
return fmt.Errorf("sandbox bucket %q already exists: %w", instance.ID, errdefs.ErrAlreadyExists)
}

bucket, err = parent.CreateBucket(id)
Expand Down Expand Up @@ -318,7 +318,7 @@ func (s *sandboxStore) read(parent *bbolt.Bucket, id []byte) (api.Sandbox, error

bucket := parent.Bucket(id)
if bucket == nil {
return api.Sandbox{}, errors.Wrapf(errdefs.ErrNotFound, "bucket %q not found", id)
return api.Sandbox{}, fmt.Errorf("bucket %q not found: %w", id, errdefs.ErrNotFound)
}

inst.ID = string(id)
Expand Down Expand Up @@ -358,19 +358,19 @@ func (s *sandboxStore) read(parent *bbolt.Bucket, id []byte) (api.Sandbox, error

func (s *sandboxStore) validate(new *api.Sandbox) error {
if err := identifiers.Validate(new.ID); err != nil {
return errors.Wrap(err, "invalid sandbox ID")
return fmt.Errorf("invalid sandbox ID: %w", err)
}

if new.CreatedAt.IsZero() {
return errors.Wrap(errdefs.ErrInvalidArgument, "creation date must not be zero")
return fmt.Errorf("creation date must not be zero: %w", errdefs.ErrInvalidArgument)
}

if new.UpdatedAt.IsZero() {
return errors.Wrap(errdefs.ErrInvalidArgument, "updated date must not be zero")
return fmt.Errorf("updated date must not be zero: %w", errdefs.ErrInvalidArgument)
}

if new.Runtime.Name == "" {
return errors.Wrapf(errdefs.ErrInvalidArgument, "sandbox.Runtime.Name must be set")
return fmt.Errorf("sandbox.Runtime.Name must be set: %w", errdefs.ErrInvalidArgument)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package containerd

import (
"context"
"errors"
"fmt"
"time"

Expand All @@ -27,7 +28,6 @@ import (
"github.com/containerd/containerd/protobuf/types"
api "github.com/containerd/containerd/sandbox"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
)

// Sandbox is a high level client to containerd's sandboxes.
Expand Down Expand Up @@ -183,7 +183,7 @@ func WithSandboxRuntime(name string, options interface{}) NewSandboxOpts {

opts, err := typeurl.MarshalAny(options)
if err != nil {
return errors.Wrap(err, "failed to marshal sandbox runtime options")
return fmt.Errorf("failed to marshal sandbox runtime options: %w", err)
}

s.Runtime = api.RuntimeOpts{
Expand All @@ -206,7 +206,7 @@ func WithSandboxSpec(s *oci.Spec, opts ...oci.SpecOpts) NewSandboxOpts {

spec, err := typeurl.MarshalAny(s)
if err != nil {
return errors.Wrap(err, "failed to marshal spec")
return fmt.Errorf("failed to marshal spec: %w", err)
}

sandbox.Spec = spec
Expand All @@ -223,7 +223,7 @@ func WithSandboxExtension(name string, ext interface{}) NewSandboxOpts {

any, err := typeurl.MarshalAny(ext)
if err != nil {
return errors.Wrap(err, "failed to marshal sandbox extension")
return fmt.Errorf("failed to marshal sandbox extension: %w", err)
}

s.Extensions[name] = any
Expand Down

0 comments on commit c4e2902

Please sign in to comment.