Skip to content

Commit

Permalink
Switch from pkg/errors to Go 1.13+ error wrapping
Browse files Browse the repository at this point in the history
The github.com/pkg/errors is mostly obsoleted since Go 1.13 introduced
%w-style error wrapping. It is also not maintained and is now archived
by the owner.

Some part of this change was done manually, and some changes were done
by using github.com/AkihiroSuda/go-wrap-to-percent-w tool.

In a few places this also:
 - changes '%s' or \"%s\" to %q;
 - removes extra context from the error message (such as, errors from os
   functions dealing with files do contain the file name already, and
   strconv.Atoi errors already contains the string which it failed to
   parse).

Note that there is a single place which uses StackTrace functionality of
pkg/errors, which is removed by this commit.

A few remaining users of pkg/errors vendored here (directly and
indirectly) are:
 - github.com/containerd/go-runc (needs to be bumped to v1.1.0);
 - github.com/microsoft/didx509go (needs microsoft/didx509go#19);
 - github.com/docker/cli (needs docker/cli#3618 fixed);
 - github.com/docker/docker (?)
 - github.com/linuxkit/virtsock (needs linuxkit/virtsock#69 merged);

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Oct 9, 2024
1 parent 3bd12f1 commit 1cabea0
Show file tree
Hide file tree
Showing 152 changed files with 887 additions and 940 deletions.
11 changes: 3 additions & 8 deletions cmd/containerd-shim-runhcs-v1/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package main

import (
"context"
"fmt"

task "github.com/containerd/containerd/api/runtime/task/v2"
"github.com/containerd/errdefs"
"github.com/pkg/errors"
)

type shimExecState string
Expand Down Expand Up @@ -86,11 +86,6 @@ type shimExec interface {
}

func newExecInvalidStateError(tid, eid string, state shimExecState, op string) error {
return errors.Wrapf(
errdefs.ErrFailedPrecondition,
"exec: '%s' in task: '%s' is in invalid state: '%s' for %s",
eid,
tid,
state,
op)
return fmt.Errorf("exec: %q in task: %q is in invalid state: %q for %s: %w",
eid, tid, state, op, errdefs.ErrFailedPrecondition)
}
13 changes: 7 additions & 6 deletions cmd/containerd-shim-runhcs-v1/exec_hcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -13,7 +14,6 @@ import (
"github.com/containerd/containerd/runtime"
"github.com/containerd/errdefs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -42,7 +42,8 @@ func newHcsExec(
id, bundle string,
isWCOW bool,
spec *specs.Process,
io cmd.UpstreamIO) shimExec {
io cmd.UpstreamIO,
) shimExec {
log.G(ctx).WithFields(logrus.Fields{
"tid": tid,
"eid": id, // Init exec ID is always same as Task ID
Expand Down Expand Up @@ -287,7 +288,7 @@ func (he *hcsExec) Kill(ctx context.Context, signal uint32) error {
}
}
if err != nil {
return errors.Wrapf(errdefs.ErrFailedPrecondition, "signal %d: %v", signal, err)
return fmt.Errorf("signal %d: %w: %w", signal, err, errdefs.ErrFailedPrecondition)
}
var delivered bool
if supported && options != nil {
Expand Down Expand Up @@ -331,11 +332,11 @@ func (he *hcsExec) Kill(ctx context.Context, signal uint32) error {
return err
}
if !delivered {
return errors.Wrapf(errdefs.ErrNotFound, "exec: '%s' in task: '%s' not found", he.id, he.tid)
return fmt.Errorf("exec: %q in task: %q: %w", he.id, he.tid, errdefs.ErrNotFound)
}
return nil
case shimExecStateExited:
return errors.Wrapf(errdefs.ErrNotFound, "exec: '%s' in task: '%s' not found", he.id, he.tid)
return fmt.Errorf("exec: %q in task: %q: %w", he.id, he.tid, errdefs.ErrNotFound)
default:
return newExecInvalidStateError(he.tid, he.id, he.state, "kill")
}
Expand All @@ -345,7 +346,7 @@ func (he *hcsExec) ResizePty(ctx context.Context, width, height uint32) error {
he.sl.Lock()
defer he.sl.Unlock()
if !he.io.Terminal() {
return errors.Wrapf(errdefs.ErrFailedPrecondition, "exec: '%s' in task: '%s' is not a tty", he.id, he.tid)
return fmt.Errorf("exec: %q in task: %q is not a tty: %w", he.id, he.tid, errdefs.ErrFailedPrecondition)
}

if he.state == shimExecStateRunning {
Expand Down
6 changes: 3 additions & 3 deletions cmd/containerd-shim-runhcs-v1/exec_wcow_podsandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -13,7 +14,6 @@ import (
containerd_v1_types "github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/runtime"
"github.com/containerd/errdefs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (wpse *wcowPodSandboxExec) Kill(ctx context.Context, signal uint32) error {
close(wpse.exited)
return nil
case shimExecStateExited:
return errors.Wrapf(errdefs.ErrNotFound, "exec: '%s' in task: '%s' not found", wpse.tid, wpse.tid)
return fmt.Errorf("exec: %q in task: %q: %w", wpse.tid, wpse.tid, errdefs.ErrNotFound)
default:
return newExecInvalidStateError(wpse.tid, wpse.tid, wpse.state, "kill")
}
Expand All @@ -177,7 +177,7 @@ func (wpse *wcowPodSandboxExec) ResizePty(ctx context.Context, width, height uin
defer wpse.sl.Unlock()
// We will never have IO for a sandbox container so we wont have a tty
// either.
return errors.Wrapf(errdefs.ErrFailedPrecondition, "exec: '%s' in task: '%s' is not a tty", wpse.tid, wpse.tid)
return fmt.Errorf("exec: %q in task: %q is not a tty: %w", wpse.tid, wpse.tid, errdefs.ErrFailedPrecondition)
}

func (wpse *wcowPodSandboxExec) CloseIO(ctx context.Context, stdin bool) error {
Expand Down
56 changes: 28 additions & 28 deletions cmd/containerd-shim-runhcs-v1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/containerd/containerd/runtime"
"github.com/containerd/errdefs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -73,28 +73,28 @@ func createPod(ctx context.Context, events publisher, req *task.CreateTaskReques
log.G(ctx).WithField("tid", req.ID).Debug("createPod")

if osversion.Build() < osversion.RS5 {
return nil, errors.Wrapf(errdefs.ErrFailedPrecondition, "pod support is not available on Windows versions previous to RS5 (%d)", osversion.RS5)
return nil, fmt.Errorf("pod support is not available on Windows versions previous to RS5 (%d): %w", osversion.RS5, errdefs.ErrFailedPrecondition)
}

ct, sid, err := oci.GetSandboxTypeAndID(s.Annotations)
if err != nil {
return nil, err
}
if ct != oci.KubernetesContainerTypeSandbox {
return nil, errors.Wrapf(
errdefs.ErrFailedPrecondition,
"expected annotation: '%s': '%s' got '%s'",
return nil, fmt.Errorf(
"expected annotation: %q: %q, got %q: %w",
annotations.KubernetesContainerType,
oci.KubernetesContainerTypeSandbox,
ct)
ct,
errdefs.ErrFailedPrecondition)
}
if sid != req.ID {
return nil, errors.Wrapf(
errdefs.ErrFailedPrecondition,
"expected annotation '%s': '%s' got '%s'",
return nil, fmt.Errorf(
"expected annotation %q: %q, got %q: %w",
annotations.KubernetesSandboxID,
req.ID,
sid)
sid,
errdefs.ErrFailedPrecondition)
}

owner := filepath.Base(os.Args[0])
Expand Down Expand Up @@ -168,7 +168,7 @@ func createPod(ctx context.Context, events publisher, req *task.CreateTaskReques
p.jobContainer = true
return &p, nil
} else if !isWCOW {
return nil, errors.Wrap(errdefs.ErrFailedPrecondition, "oci spec does not contain WCOW or LCOW spec")
return nil, fmt.Errorf("oci spec does not contain WCOW or LCOW spec: %w", errdefs.ErrFailedPrecondition)
}

defer func() {
Expand Down Expand Up @@ -208,7 +208,7 @@ func createPod(ctx context.Context, events publisher, req *task.CreateTaskReques

if nsid != "" {
if err := parent.ConfigureNetworking(ctx, nsid); err != nil {
return nil, errors.Wrapf(err, "failed to setup networking for pod %q", req.ID)
return nil, fmt.Errorf("failed to setup networking for pod %q: %w", req.ID, err)
}
}
p.sandboxTask = newWcowPodSandboxTask(ctx, events, req.ID, req.Bundle, parent, nsid)
Expand Down Expand Up @@ -297,16 +297,16 @@ func (p *pod) ID() string {

func (p *pod) CreateTask(ctx context.Context, req *task.CreateTaskRequest, s *specs.Spec) (_ shimTask, err error) {
if req.ID == p.id {
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "task with id: '%s' already exists", req.ID)
return nil, fmt.Errorf("task with id: %q: %w", req.ID, errdefs.ErrAlreadyExists)
}
e, _ := p.sandboxTask.GetExec("")
if e.State() != shimExecStateRunning {
return nil, errors.Wrapf(errdefs.ErrFailedPrecondition, "task with id: '%s' cannot be created in pod: '%s' which is not running", req.ID, p.id)
return nil, fmt.Errorf("task with id: %q cannot be created in pod: %q which is not running: %w", req.ID, p.id, errdefs.ErrFailedPrecondition)
}

_, ok := p.workloadTasks.Load(req.ID)
if ok {
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "task with id: '%s' already exists id pod: '%s'", req.ID, p.id)
return nil, fmt.Errorf("task with id: %q already exists in pod: %q: %w", req.ID, p.id, errdefs.ErrAlreadyExists)
}

if p.jobContainer {
Expand Down Expand Up @@ -334,20 +334,20 @@ func (p *pod) CreateTask(ctx context.Context, req *task.CreateTaskRequest, s *sp
return nil, err
}
if ct != oci.KubernetesContainerTypeContainer {
return nil, errors.Wrapf(
errdefs.ErrFailedPrecondition,
"expected annotation: '%s': '%s' got '%s'",
return nil, fmt.Errorf(
"expected annotation: %q: %q, got %q: %w",
annotations.KubernetesContainerType,
oci.KubernetesContainerTypeContainer,
ct)
ct,
errdefs.ErrFailedPrecondition)
}
if sid != p.id {
return nil, errors.Wrapf(
errdefs.ErrFailedPrecondition,
"expected annotation '%s': '%s' got '%s'",
return nil, fmt.Errorf(
"expected annotation %q: %q, got %q: %w",
annotations.KubernetesSandboxID,
p.id,
sid)
sid,
errdefs.ErrFailedPrecondition)
}

st, err := newHcsTask(ctx, p.events, p.host, false, req, s)
Expand All @@ -365,7 +365,7 @@ func (p *pod) GetTask(tid string) (shimTask, error) {
}
raw, loaded := p.workloadTasks.Load(tid)
if !loaded {
return nil, errors.Wrapf(errdefs.ErrNotFound, "task with id: '%s' not found", tid)
return nil, fmt.Errorf("task with id: %q: %w", tid, errdefs.ErrNotFound)
}
return raw.(shimTask), nil
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (p *pod) KillTask(ctx context.Context, tid, eid string, signal uint32, all
return err
}
if all && eid != "" {
return errors.Wrapf(errdefs.ErrFailedPrecondition, "cannot signal all with non empty ExecID: '%s'", eid)
return fmt.Errorf("cannot signal all with non empty ExecID: %q: %w", eid, errdefs.ErrFailedPrecondition)
}
eg := errgroup.Group{}
if all && tid == p.id {
Expand Down Expand Up @@ -426,15 +426,15 @@ func (p *pod) DeleteTask(ctx context.Context, tid string) error {

t, err := p.GetTask(tid)
if err != nil {
return errors.Wrap(err, "could not find task to delete")
return fmt.Errorf("could not find task to delete: %w", err)
}

e, err := t.GetExec("")
if err != nil {
return errors.Wrap(err, "could not get initial exec")
return fmt.Errorf("could not get initial exec: %w", err)
}
if e.State() == shimExecStateRunning {
return errors.Wrap(errdefs.ErrFailedPrecondition, "cannot delete task with running exec")
return fmt.Errorf("cannot delete task with running exec: %w", errdefs.ErrFailedPrecondition)
}

if p.id != tid {
Expand Down
16 changes: 8 additions & 8 deletions cmd/containerd-shim-runhcs-v1/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand All @@ -16,7 +17,6 @@ import (
task "github.com/containerd/containerd/api/runtime/task/v2"
"github.com/containerd/ttrpc"
typeurl "github.com/containerd/typeurl/v2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/sys/windows"
Expand Down Expand Up @@ -79,7 +79,7 @@ var serveCommand = cli.Command{
// containerd passes the shim options protobuf via stdin.
newShimOpts, err := readOptions(os.Stdin)
if err != nil {
return errors.Wrap(err, "failed to read shim options from stdin")
return fmt.Errorf("failed to read shim options from stdin: %w", err)
} else if newShimOpts != nil {
// We received a valid shim options struct.
shimOpts = newShimOpts
Expand All @@ -100,7 +100,7 @@ var serveCommand = cli.Command{
if shimOpts.LogLevel != "" {
lvl, err := logrus.ParseLevel(shimOpts.LogLevel)
if err != nil {
return errors.Wrapf(err, "failed to parse shim log level %q", shimOpts.LogLevel)
return fmt.Errorf("failed to parse shim log level %q: %w", shimOpts.LogLevel, err)
}
logrus.SetLevel(lvl)
}
Expand Down Expand Up @@ -274,16 +274,16 @@ func trapClosedConnErr(err error) error {
func readOptions(r io.Reader) (*runhcsopts.Options, error) {
d, err := io.ReadAll(r)
if err != nil {
return nil, errors.Wrap(err, "failed to read input")
return nil, fmt.Errorf("failed to read input: %w", err)
}
if len(d) > 0 {
var a anypb.Any
if err := proto.Unmarshal(d, &a); err != nil {
return nil, errors.Wrap(err, "failed unmarshalling into Any")
return nil, fmt.Errorf("failed unmarshalling into Any: %w", err)
}
v, err := typeurl.UnmarshalAny(&a)
if err != nil {
return nil, errors.Wrap(err, "failed unmarshalling by typeurl")
return nil, fmt.Errorf("failed unmarshalling by typeurl: %w", err)
}
return v.(*runhcsopts.Options), nil
}
Expand All @@ -296,15 +296,15 @@ func createEvent(event string) (windows.Handle, error) {
ev, _ := windows.UTF16PtrFromString(event)
sd, err := windows.SecurityDescriptorFromString("D:P(A;;GA;;;BA)(A;;GA;;;SY)")
if err != nil {
return 0, errors.Wrapf(err, "failed to get security descriptor for event '%s'", event)
return 0, fmt.Errorf("failed to get security descriptor for event %q: %w", event, err)
}
var sa windows.SecurityAttributes
sa.Length = uint32(unsafe.Sizeof(sa))
sa.InheritHandle = 1
sa.SecurityDescriptor = sd
h, err := windows.CreateEvent(&sa, 0, 0, ev)
if h == 0 || err != nil {
return 0, errors.Wrapf(err, "failed to create event '%s'", event)
return 0, fmt.Errorf("failed to create event %q: %w", event, err)
}
return h, nil
}
Expand Down
Loading

0 comments on commit 1cabea0

Please sign in to comment.