feat: replace github.com/pkg/errors to errors

Signed-off-by: haoyun <yun.hao@daocloud.io>
Co-authored-by: zounengren <zouyee1989@gmail.com>
This commit is contained in:
haoyun
2022-01-07 10:19:31 +08:00
parent 3ccd43c8f6
commit bbe46b8c43
299 changed files with 1896 additions and 1874 deletions

View File

@@ -18,12 +18,12 @@ package monitor
import (
"context"
"fmt"
"net/url"
"syscall"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -49,7 +49,7 @@ func (s *startChange) apply(ctx context.Context, client *containerd.Client) erro
if s.logURI != "" {
uri, err := url.Parse(s.logURI)
if err != nil {
return errors.Wrapf(err, "failed to parse %v into url", s.logURI)
return fmt.Errorf("failed to parse %v into url: %w", s.logURI, err)
}
log = cio.LogURI(uri)
} else if s.logPath != "" {

View File

@@ -35,7 +35,6 @@ import (
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/containerd/services"
"github.com/containerd/containerd/snapshots"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -94,12 +93,12 @@ func init() {
func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
plugins, err := ic.GetByType(plugin.ServicePlugin)
if err != nil {
return nil, errors.Wrap(err, "failed to get service plugin")
return nil, fmt.Errorf("failed to get service plugin: %w", err)
}
ep, err := ic.Get(plugin.EventPlugin)
if err != nil {
return nil, errors.Wrap(err, "failed to get event plugin")
return nil, fmt.Errorf("failed to get event plugin: %w", err)
}
opts := []containerd.ServicesOpt{
@@ -133,14 +132,14 @@ func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
} {
p := plugins[s]
if p == nil {
return nil, errors.Errorf("service %q not found", s)
return nil, fmt.Errorf("service %q not found", s)
}
i, err := p.Instance()
if err != nil {
return nil, errors.Wrapf(err, "failed to get instance of service %q", s)
return nil, fmt.Errorf("failed to get instance of service %q: %w", s, err)
}
if i == nil {
return nil, errors.Errorf("instance of service %q not found", s)
return nil, fmt.Errorf("instance of service %q not found", s)
}
opts = append(opts, fn(i))
}

View File

@@ -18,10 +18,11 @@ package runtime
import (
"context"
"errors"
"fmt"
"sync"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
)
var (
@@ -109,7 +110,7 @@ func (l *TaskList) AddWithNamespace(namespace string, t Task) error {
l.tasks[namespace] = make(map[string]Task)
}
if _, ok := l.tasks[namespace][id]; ok {
return errors.Wrap(ErrTaskAlreadyExists, id)
return fmt.Errorf("%s: %w", id, ErrTaskAlreadyExists)
}
l.tasks[namespace][id] = t
return nil

View File

@@ -32,7 +32,6 @@ import (
"github.com/containerd/containerd/runtime/v1/shim"
"github.com/containerd/containerd/runtime/v1/shim/client"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
// loadBundle loads an existing bundle from disk
@@ -185,7 +184,7 @@ func (b *bundle) Delete() error {
if err2 == nil {
return err
}
return errors.Wrapf(err, "Failed to remove both bundle and workdir locations: %v", err2)
return fmt.Errorf("Failed to remove both bundle and workdir locations: %v: %w", err2, err)
}
func (b *bundle) legacyShimAddress(namespace string) string {

View File

@@ -21,6 +21,7 @@ package linux
import (
"context"
"errors"
eventstypes "github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/api/types/task"
@@ -28,7 +29,6 @@ import (
"github.com/containerd/containerd/runtime"
shim "github.com/containerd/containerd/runtime/v1/shim/v1"
"github.com/containerd/ttrpc"
"github.com/pkg/errors"
)
// Process implements a linux process

View File

@@ -21,6 +21,7 @@ package linux
import (
"context"
"errors"
"fmt"
"io"
"os"
@@ -48,7 +49,6 @@ import (
"github.com/containerd/typeurl"
ptypes "github.com/gogo/protobuf/types"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
@@ -167,7 +167,7 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
}
if err := identifiers.Validate(id); err != nil {
return nil, errors.Wrapf(err, "invalid task id")
return nil, fmt.Errorf("invalid task id: %w", err)
}
ropts, err := r.getRuncOptions(ctx, id)
@@ -450,7 +450,7 @@ func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns,
ctx = namespaces.WithNamespace(ctx, ns)
if err := r.terminate(ctx, bundle, ns, id); err != nil {
if r.config.ShimDebug {
return errors.Wrap(err, "failed to terminate task, leaving bundle for debugging")
return fmt.Errorf("failed to terminate task, leaving bundle for debugging: %w", err)
}
log.G(ctx).WithError(err).Warn("failed to terminate task")
}

View File

@@ -21,6 +21,8 @@ package linux
import (
"context"
"errors"
"fmt"
"sync"
"github.com/containerd/cgroups"
@@ -36,7 +38,6 @@ import (
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
)
// Task on a linux based system
@@ -229,7 +230,7 @@ func (t *Task) Kill(ctx context.Context, signal uint32, all bool) error {
// Exec creates a new process inside the task
func (t *Task) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.ExecProcess, error) {
if err := identifiers.Validate(id); err != nil {
return nil, errors.Wrapf(err, "invalid exec id")
return nil, fmt.Errorf("invalid exec id: %w", err)
}
request := &shim.ExecProcessRequest{
ID: id,
@@ -333,7 +334,7 @@ func (t *Task) Stats(ctx context.Context) (*types.Any, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.cg == nil {
return nil, errors.Wrap(errdefs.ErrNotFound, "cgroup does not exist")
return nil, fmt.Errorf("cgroup does not exist: %w", errdefs.ErrNotFound)
}
stats, err := t.cg.Stat(cgroups.IgnoreNotExist)
if err != nil {
@@ -347,7 +348,7 @@ func (t *Task) Cgroup() (cgroups.Cgroup, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.cg == nil {
return nil, errors.Wrap(errdefs.ErrNotFound, "cgroup does not exist")
return nil, fmt.Errorf("cgroup does not exist: %w", errdefs.ErrNotFound)
}
return t.cg, nil
}

View File

@@ -21,6 +21,7 @@ package client
import (
"context"
"errors"
"fmt"
"io"
"net"
@@ -40,7 +41,6 @@ import (
"github.com/containerd/containerd/sys"
"github.com/containerd/ttrpc"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
@@ -60,7 +60,7 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
return nil, nil, err
}
if err := RemoveSocket(address); err != nil {
return nil, nil, errors.Wrap(err, "remove already used socket")
return nil, nil, fmt.Errorf("remove already used socket: %w", err)
}
if socket, err = newSocket(address); err != nil {
return nil, nil, err
@@ -69,7 +69,7 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
f, err := socket.File()
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get fd for socket %s", address)
return nil, nil, fmt.Errorf("failed to get fd for socket %s: %w", address, err)
}
defer f.Close()
@@ -77,12 +77,12 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
stderrCopy := io.Discard
stdoutLog, err := v1.OpenShimStdoutLog(ctx, config.WorkDir)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to create stdout log")
return nil, nil, fmt.Errorf("failed to create stdout log: %w", err)
}
stderrLog, err := v1.OpenShimStderrLog(ctx, config.WorkDir)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to create stderr log")
return nil, nil, fmt.Errorf("failed to create stderr log: %w", err)
}
if debug {
stdoutCopy = os.Stdout
@@ -97,7 +97,7 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
return nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, errors.Wrapf(err, "failed to start shim")
return nil, nil, fmt.Errorf("failed to start shim: %w", err)
}
defer func() {
if err != nil {
@@ -143,14 +143,14 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
}
c, clo, err := WithConnect(address, func() {})(ctx, config)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to connect")
return nil, nil, fmt.Errorf("failed to connect: %w", err)
}
return c, clo, nil
}
}
func eaddrinuse(err error) bool {
cause := errors.Cause(err)
cause := errors.Unwrap(err)
netErr, ok := cause.(*net.OpError)
if !ok {
return false
@@ -176,11 +176,11 @@ func setupOOMScore(shimPid int) error {
pid := os.Getpid()
score, err := sys.GetOOMScoreAdj(pid)
if err != nil {
return errors.Wrap(err, "get daemon OOM score")
return fmt.Errorf("get daemon OOM score: %w", err)
}
shimScore := score + 1
if err := sys.AdjustOOMScore(shimPid, shimScore); err != nil {
return errors.Wrap(err, "set shim OOM score")
return fmt.Errorf("set shim OOM score: %w", err)
}
return nil
}
@@ -264,7 +264,7 @@ func (s socket) path() string {
func newSocket(address string) (*net.UnixListener, error) {
if len(address) > socketPathLimit {
return nil, errors.Errorf("%q: unix socket path too long (> %d)", address, socketPathLimit)
return nil, fmt.Errorf("%q: unix socket path too long (> %d)", address, socketPathLimit)
}
var (
sock = socket(address)
@@ -272,12 +272,12 @@ func newSocket(address string) (*net.UnixListener, error) {
)
if !sock.isAbstract() {
if err := os.MkdirAll(filepath.Dir(path), 0600); err != nil {
return nil, errors.Wrapf(err, "%s", path)
return nil, fmt.Errorf("%s: %w", path, err)
}
}
l, err := net.Listen("unix", path)
if err != nil {
return nil, errors.Wrapf(err, "failed to listen to unix socket %q (abstract: %t)", address, sock.isAbstract())
return nil, fmt.Errorf("failed to listen to unix socket %q (abstract: %t): %w", address, sock.isAbstract(), err)
}
if err := os.Chmod(path, 0600); err != nil {
l.Close()

View File

@@ -17,10 +17,10 @@
package client
import (
"fmt"
"syscall"
"github.com/containerd/cgroups"
"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
)
@@ -33,12 +33,12 @@ func getSysProcAttr() *syscall.SysProcAttr {
func setCgroup(cgroupPath string, cmd *exec.Cmd) error {
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath))
if err != nil {
return errors.Wrapf(err, "failed to load cgroup %s", cgroupPath)
return fmt.Errorf("failed to load cgroup %s: %w", cgroupPath, err)
}
if err := cg.Add(cgroups.Process{
Pid: cmd.Process.Pid,
}); err != nil {
return errors.Wrapf(err, "failed to join cgroup %s", cgroupPath)
return fmt.Errorf("failed to join cgroup %s: %w", cgroupPath, err)
}
return nil
}

View File

@@ -45,7 +45,6 @@ import (
"github.com/containerd/typeurl"
ptypes "github.com/gogo/protobuf/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -91,7 +90,7 @@ func NewService(config Config, publisher events.Publisher) (*Service, error) {
}
go s.processExits()
if err := s.initPlatform(); err != nil {
return nil, errors.Wrap(err, "failed to initialized platform behavior")
return nil, fmt.Errorf("failed to initialized platform behavior: %w", err)
}
go s.forward(publisher)
return s, nil
@@ -160,7 +159,7 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
Options: rm.Options,
}
if err := m.Mount(rootfs); err != nil {
return nil, errors.Wrapf(err, "failed to mount rootfs component %v", m)
return nil, fmt.Errorf("failed to mount rootfs component %v: %w", m, err)
}
}
@@ -297,7 +296,7 @@ func (s *Service) ResizePty(ctx context.Context, r *shimapi.ResizePtyRequest) (*
p := s.processes[r.ID]
s.mu.Unlock()
if p == nil {
return nil, errors.Errorf("process does not exist %s", r.ID)
return nil, fmt.Errorf("process does not exist %s", r.ID)
}
if err := p.Resize(ws); err != nil {
return nil, errdefs.ToGRPC(err)
@@ -411,7 +410,7 @@ func (s *Service) ListPids(ctx context.Context, r *shimapi.ListPidsRequest) (*sh
}
a, err := typeurl.MarshalAny(d)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal process %d info", pid)
return nil, fmt.Errorf("failed to marshal process %d info: %w", pid, err)
}
pInfo.Info = a
break
@@ -432,7 +431,7 @@ func (s *Service) CloseIO(ctx context.Context, r *shimapi.CloseIORequest) (*ptyp
}
if stdin := p.Stdin(); stdin != nil {
if err := stdin.Close(); err != nil {
return nil, errors.Wrap(err, "close stdin")
return nil, fmt.Errorf("close stdin: %w", err)
}
}
return empty, nil

View File

@@ -18,6 +18,8 @@ package shim
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
@@ -28,7 +30,6 @@ import (
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/pkg/process"
"github.com/containerd/fifo"
"github.com/pkg/errors"
)
type linuxPlatform struct {
@@ -65,7 +66,7 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
uri, err := url.Parse(stdout)
if err != nil {
return nil, errors.Wrap(err, "unable to parse stdout uri")
return nil, fmt.Errorf("unable to parse stdout uri: %w", err)
}
switch uri.Scheme {
@@ -89,14 +90,14 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
// Create pipe to be used by logging binary for Stdout
outR, outW, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stdout pipes")
return nil, fmt.Errorf("failed to create stdout pipes: %w", err)
}
filesToClose = append(filesToClose, outR)
// Stderr is created for logging binary but unused when terminal is true
serrR, _, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stderr pipes")
return nil, fmt.Errorf("failed to create stderr pipes: %w", err)
}
filesToClose = append(filesToClose, serrR)
@@ -118,18 +119,18 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
}()
if err := cmd.Start(); err != nil {
return nil, errors.Wrap(err, "failed to start logging binary process")
return nil, fmt.Errorf("failed to start logging binary process: %w", err)
}
// Close our side of the pipe after start
if err := w.Close(); err != nil {
return nil, errors.Wrap(err, "failed to close write pipe after start")
return nil, fmt.Errorf("failed to close write pipe after start: %w", err)
}
// Wait for the logging binary to be ready
b := make([]byte, 1)
if _, err := r.Read(b); err != nil && err != io.EOF {
return nil, errors.Wrap(err, "failed to read from logging binary")
return nil, fmt.Errorf("failed to read from logging binary: %w", err)
}
cwg.Wait()
@@ -164,7 +165,7 @@ func (p *linuxPlatform) ShutdownConsole(ctx context.Context, cons console.Consol
}
epollConsole, ok := cons.(*console.EpollConsole)
if !ok {
return errors.Errorf("expected EpollConsole, got %#v", cons)
return fmt.Errorf("expected EpollConsole, got %#v", cons)
}
return epollConsole.Shutdown(p.epoller.CloseConsole)
}
@@ -181,7 +182,7 @@ func (s *Service) initPlatform() error {
}
epoller, err := console.NewEpoller()
if err != nil {
return errors.Wrap(err, "failed to initialize epoller")
return fmt.Errorf("failed to initialize epoller: %w", err)
}
s.platform = &linuxPlatform{
epoller: epoller,

View File

@@ -21,6 +21,7 @@ package shim
import (
"context"
"fmt"
"io"
"net/url"
"os"
@@ -31,7 +32,6 @@ import (
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/pkg/process"
"github.com/containerd/fifo"
"github.com/pkg/errors"
)
type unixPlatform struct {
@@ -55,7 +55,7 @@ func (p *unixPlatform) CopyConsole(ctx context.Context, console console.Console,
}
uri, err := url.Parse(stdout)
if err != nil {
return nil, errors.Wrap(err, "unable to parse stdout uri")
return nil, fmt.Errorf("unable to parse stdout uri: %w", err)
}
switch uri.Scheme {
@@ -78,14 +78,14 @@ func (p *unixPlatform) CopyConsole(ctx context.Context, console console.Console,
// Create pipe to be used by logging binary for Stdout
outR, outW, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stdout pipes")
return nil, fmt.Errorf("failed to create stdout pipes: %w", err)
}
filesToClose = append(filesToClose, outR)
// Stderr is created for logging binary but unused when terminal is true
serrR, _, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stderr pipes")
return nil, fmt.Errorf("failed to create stderr pipes: %w", err)
}
filesToClose = append(filesToClose, serrR)
@@ -107,18 +107,18 @@ func (p *unixPlatform) CopyConsole(ctx context.Context, console console.Console,
}()
if err := cmd.Start(); err != nil {
return nil, errors.Wrap(err, "failed to start logging binary process")
return nil, fmt.Errorf("failed to start logging binary process: %w", err)
}
// Close our side of the pipe after start
if err := w.Close(); err != nil {
return nil, errors.Wrap(err, "failed to close write pipe after start")
return nil, fmt.Errorf("failed to close write pipe after start: %w", err)
}
// Wait for the logging binary to be ready
b := make([]byte, 1)
if _, err := r.Read(b); err != nil && err != io.EOF {
return nil, errors.Wrap(err, "failed to read from logging binary")
return nil, fmt.Errorf("failed to read from logging binary: %w", err)
}
cwg.Wait()

View File

@@ -19,6 +19,7 @@ package v2
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
@@ -32,7 +33,6 @@ import (
"github.com/containerd/containerd/runtime/v2/task"
"github.com/containerd/ttrpc"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -93,7 +93,7 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
}()
f, err := openShimLog(shimCtx, b.bundle, client.AnonDialer)
if err != nil {
return nil, errors.Wrap(err, "open shim log pipe")
return nil, fmt.Errorf("open shim log pipe: %w", err)
}
defer func() {
if err != nil {
@@ -116,7 +116,7 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
}()
out, err := cmd.CombinedOutput()
if err != nil {
return nil, errors.Wrapf(err, "%s", out)
return nil, fmt.Errorf("%s: %w", out, err)
}
address := strings.TrimSpace(string(out))
conn, err := client.Connect(address, client.AnonDialer)
@@ -177,7 +177,7 @@ func (b *binary) Delete(ctx context.Context) (*runtime.Exit, error) {
cmd.Stderr = errb
if err := cmd.Run(); err != nil {
log.G(ctx).WithField("cmd", cmd).WithError(err).Error("failed to delete")
return nil, errors.Wrapf(err, "%s", errb.String())
return nil, fmt.Errorf("%s: %w", errb.String(), err)
}
s := errb.String()
if s != "" {

View File

@@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd/identifiers"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
)
const configFilename = "config.json"
@@ -46,7 +45,7 @@ func LoadBundle(ctx context.Context, root, id string) (*Bundle, error) {
// NewBundle returns a new bundle on disk
func NewBundle(ctx context.Context, root, state, id string, spec []byte) (b *Bundle, err error) {
if err := identifiers.Validate(id); err != nil {
return nil, errors.Wrapf(err, "invalid task id %s", id)
return nil, fmt.Errorf("invalid task id %s: %w", id, err)
}
ns, err := namespaces.NamespaceRequired(ctx)
@@ -121,10 +120,10 @@ func (b *Bundle) Delete() error {
work, werr := os.Readlink(filepath.Join(b.Path, "work"))
rootfs := filepath.Join(b.Path, "rootfs")
if err := mount.UnmountAll(rootfs, 0); err != nil {
return errors.Wrapf(err, "unmount rootfs %s", rootfs)
return fmt.Errorf("unmount rootfs %s: %w", rootfs, err)
}
if err := os.Remove(rootfs); err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "failed to remove bundle rootfs")
return fmt.Errorf("failed to remove bundle rootfs: %w", err)
}
err := atomicDelete(b.Path)
if err == nil {
@@ -141,7 +140,7 @@ func (b *Bundle) Delete() error {
return err
}
}
return errors.Wrapf(err, "failed to remove both bundle and workdir locations: %v", err2)
return fmt.Errorf("failed to remove both bundle and workdir locations: %v: %w", err2, err)
}
// atomicDelete renames the path to a hidden file before removal

View File

@@ -18,6 +18,7 @@ package logging
import (
"context"
"errors"
"fmt"
"net"
"os"
@@ -25,7 +26,6 @@ import (
"syscall"
"github.com/Microsoft/go-winio"
"github.com/pkg/errors"
)
// Run the logging driver
@@ -53,19 +53,19 @@ func runInternal(fn LoggerFunc) error {
return errors.New("'CONTAINER_STDOUT' environment variable missing")
}
if sout, err = winio.DialPipeContext(ctx, soutPipe); err != nil {
return errors.Wrap(err, "unable to dial stdout pipe")
return fmt.Errorf("unable to dial stdout pipe: %w", err)
}
if serrPipe, ok = os.LookupEnv("CONTAINER_STDERR"); !ok {
return errors.New("'CONTAINER_STDERR' environment variable missing")
}
if serr, err = winio.DialPipeContext(ctx, serrPipe); err != nil {
return errors.Wrap(err, "unable to dial stderr pipe")
return fmt.Errorf("unable to dial stderr pipe: %w", err)
}
waitPipe = os.Getenv("CONTAINER_WAIT")
if wait, err = winio.DialPipeContext(ctx, waitPipe); err != nil {
return errors.Wrap(err, "unable to dial wait pipe")
return fmt.Errorf("unable to dial wait pipe: %w", err)
}
config := &Config{

View File

@@ -37,7 +37,6 @@ import (
shimbinary "github.com/containerd/containerd/runtime/v2/shim"
"github.com/containerd/containerd/runtime/v2/task"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
// Config for the v2 runtime
@@ -185,7 +184,7 @@ func (m *ShimManager) Start(ctx context.Context, id string, opts runtime.CreateO
}
if err := m.shims.Add(ctx, shimTask); err != nil {
return nil, errors.Wrap(err, "failed to add task")
return nil, fmt.Errorf("failed to add task: %w", err)
}
return shimTask, nil
@@ -224,7 +223,7 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string,
m.shims.Delete(ctx, id)
})
if err != nil {
return nil, errors.Wrap(err, "start failed")
return nil, fmt.Errorf("start failed: %w", err)
}
return shim, nil
@@ -283,7 +282,7 @@ func (m *ShimManager) resolveRuntimePath(runtime string) (string, error) {
cmdPath = testPath
}
if cmdPath == "" {
return "", errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name)
return "", fmt.Errorf("runtime %q binary not installed %q: %w", runtime, name, os.ErrNotExist)
}
}
}
@@ -368,7 +367,7 @@ func (m *TaskManager) ID() string {
func (m *TaskManager) Create(ctx context.Context, taskID string, opts runtime.CreateOpts) (runtime.Task, error) {
process, err := m.manager.Start(ctx, taskID, opts)
if err != nil {
return nil, errors.Wrap(err, "failed to start shim")
return nil, fmt.Errorf("failed to start shim: %w", err)
}
// Cast to shim task and call task service to create a new container task instance.
@@ -393,7 +392,7 @@ func (m *TaskManager) Create(ctx context.Context, taskID string, opts runtime.Cr
shim.Close()
}
return nil, errors.Wrap(err, "failed to create shim task")
return nil, fmt.Errorf("failed to create shim task: %w", err)
}
return t, nil

View File

@@ -18,13 +18,13 @@ package v2
import (
"context"
"errors"
tasktypes "github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/runtime/v2/task"
"github.com/containerd/ttrpc"
"github.com/pkg/errors"
)
type process struct {

View File

@@ -22,6 +22,7 @@ package runc
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
@@ -37,7 +38,6 @@ import (
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/containerd/runtime/v2/task"
"github.com/containerd/typeurl"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -45,7 +45,7 @@ import (
func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTaskRequest) (_ *Container, retErr error) {
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return nil, errors.Wrap(err, "create namespace")
return nil, fmt.Errorf("create namespace: %w", err)
}
var opts options.Options
@@ -110,7 +110,7 @@ func NewContainer(ctx context.Context, platform stdio.Platform, r *task.CreateTa
Options: rm.Options,
}
if err := m.Mount(rootfs); err != nil {
return nil, errors.Wrapf(err, "failed to mount rootfs component %v", m)
return nil, fmt.Errorf("failed to mount rootfs component %v: %w", m, err)
}
}
@@ -300,13 +300,13 @@ func (c *Container) Process(id string) (process.Process, error) {
defer c.mu.Unlock()
if id == "" {
if c.process == nil {
return nil, errors.Wrapf(errdefs.ErrFailedPrecondition, "container must be created")
return nil, fmt.Errorf("container must be created: %w", errdefs.ErrFailedPrecondition)
}
return c.process, nil
}
p, ok := c.processes[id]
if !ok {
return nil, errors.Wrapf(errdefs.ErrNotFound, "process does not exist %s", id)
return nil, fmt.Errorf("process does not exist %s: %w", id, errdefs.ErrNotFound)
}
return p, nil
}
@@ -453,7 +453,7 @@ func (c *Container) CloseIO(ctx context.Context, r *task.CloseIORequest) error {
}
if stdin := p.Stdin(); stdin != nil {
if err := stdin.Close(); err != nil {
return errors.Wrap(err, "close stdin")
return fmt.Errorf("close stdin: %w", err)
}
}
return nil

View File

@@ -19,6 +19,7 @@ package manager
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -40,7 +41,6 @@ import (
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
)
@@ -141,19 +141,19 @@ func (manager) Start(ctx context.Context, id string, opts shim.StartOpts) (_ str
// grouping functionality where the new process should be run with the same
// shim as an existing container
if !shim.SocketEaddrinuse(err) {
return "", errors.Wrap(err, "create new shim socket")
return "", fmt.Errorf("create new shim socket: %w", err)
}
if shim.CanConnect(address) {
if err := shim.WriteAddress("address", address); err != nil {
return "", errors.Wrap(err, "write existing socket for shim")
return "", fmt.Errorf("write existing socket for shim: %w", err)
}
return address, nil
}
if err := shim.RemoveSocket(address); err != nil {
return "", errors.Wrap(err, "remove pre-existing socket")
return "", fmt.Errorf("remove pre-existing socket: %w", err)
}
if socket, err = shim.NewSocket(address); err != nil {
return "", errors.Wrap(err, "try create new shim socket 2x")
return "", fmt.Errorf("try create new shim socket 2x: %w", err)
}
}
defer func() {
@@ -178,7 +178,7 @@ func (manager) Start(ctx context.Context, id string, opts shim.StartOpts) (_ str
goruntime.LockOSThread()
if os.Getenv("SCHED_CORE") != "" {
if err := schedcore.Create(schedcore.ProcessGroup); err != nil {
return "", errors.Wrap(err, "enable sched core support")
return "", fmt.Errorf("enable sched core support: %w", err)
}
}
@@ -211,20 +211,20 @@ func (manager) Start(ctx context.Context, id string, opts shim.StartOpts) (_ str
if cgroups.Mode() == cgroups.Unified {
cg, err := cgroupsv2.LoadManager("/sys/fs/cgroup", opts.ShimCgroup)
if err != nil {
return "", errors.Wrapf(err, "failed to load cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to load cgroup %s: %w", opts.ShimCgroup, err)
}
if err := cg.AddProc(uint64(cmd.Process.Pid)); err != nil {
return "", errors.Wrapf(err, "failed to join cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to join cgroup %s: %w", opts.ShimCgroup, err)
}
} else {
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(opts.ShimCgroup))
if err != nil {
return "", errors.Wrapf(err, "failed to load cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to load cgroup %s: %w", opts.ShimCgroup, err)
}
if err := cg.Add(cgroups.Process{
Pid: cmd.Process.Pid,
}); err != nil {
return "", errors.Wrapf(err, "failed to join cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to join cgroup %s: %w", opts.ShimCgroup, err)
}
}
}
@@ -232,7 +232,7 @@ func (manager) Start(ctx context.Context, id string, opts shim.StartOpts) (_ str
}
}
if err := shim.AdjustOOMScore(cmd.Process.Pid); err != nil {
return "", errors.Wrap(err, "failed to adjust OOM score for shim")
return "", fmt.Errorf("failed to adjust OOM score for shim: %w", err)
}
return address, nil
}

View File

@@ -21,6 +21,8 @@ package runc
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
@@ -32,7 +34,6 @@ import (
"github.com/containerd/containerd/pkg/process"
"github.com/containerd/containerd/pkg/stdio"
"github.com/containerd/fifo"
"github.com/pkg/errors"
)
var bufPool = sync.Pool{
@@ -48,7 +49,7 @@ var bufPool = sync.Pool{
func NewPlatform() (stdio.Platform, error) {
epoller, err := console.NewEpoller()
if err != nil {
return nil, errors.Wrap(err, "failed to initialize epoller")
return nil, fmt.Errorf("failed to initialize epoller: %w", err)
}
go epoller.Wait()
return &linuxPlatform{
@@ -90,7 +91,7 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
uri, err := url.Parse(stdout)
if err != nil {
return nil, errors.Wrap(err, "unable to parse stdout uri")
return nil, fmt.Errorf("unable to parse stdout uri: %w", err)
}
switch uri.Scheme {
@@ -114,14 +115,14 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
// Create pipe to be used by logging binary for Stdout
outR, outW, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stdout pipes")
return nil, fmt.Errorf("failed to create stdout pipes: %w", err)
}
filesToClose = append(filesToClose, outR)
// Stderr is created for logging binary but unused when terminal is true
serrR, _, err := os.Pipe()
if err != nil {
return nil, errors.Wrap(err, "failed to create stderr pipes")
return nil, fmt.Errorf("failed to create stderr pipes: %w", err)
}
filesToClose = append(filesToClose, serrR)
@@ -143,18 +144,18 @@ func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console
}()
if err := cmd.Start(); err != nil {
return nil, errors.Wrap(err, "failed to start logging binary process")
return nil, fmt.Errorf("failed to start logging binary process: %w", err)
}
// Close our side of the pipe after start
if err := w.Close(); err != nil {
return nil, errors.Wrap(err, "failed to close write pipe after start")
return nil, fmt.Errorf("failed to close write pipe after start: %w", err)
}
// Wait for the logging binary to be ready
b := make([]byte, 1)
if _, err := r.Read(b); err != nil && err != io.EOF {
return nil, errors.Wrap(err, "failed to read from logging binary")
return nil, fmt.Errorf("failed to read from logging binary: %w", err)
}
cwg.Wait()
@@ -191,7 +192,7 @@ func (p *linuxPlatform) ShutdownConsole(ctx context.Context, cons console.Consol
}
epollConsole, ok := cons.(*console.EpollConsole)
if !ok {
return errors.Errorf("expected EpollConsole, got %#v", cons)
return fmt.Errorf("expected EpollConsole, got %#v", cons)
}
return epollConsole.Shutdown(p.epoller.CloseConsole)
}

View File

@@ -21,6 +21,7 @@ package task
import (
"context"
"fmt"
"os"
"sync"
@@ -47,7 +48,6 @@ import (
"github.com/containerd/ttrpc"
"github.com/containerd/typeurl"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -82,7 +82,7 @@ func NewTaskService(ctx context.Context, publisher shim.Publisher, sd shutdown.S
go s.processExits()
runcC.Monitor = reaper.Default
if err := s.initPlatform(); err != nil {
return nil, errors.Wrap(err, "failed to initialized platform behavior")
return nil, fmt.Errorf("failed to initialized platform behavior: %w", err)
}
go s.forward(ctx, publisher)
sd.RegisterCallback(func(context.Context) error {
@@ -377,7 +377,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.Pi
}
a, err := typeurl.MarshalAny(d)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal process %d info", pid)
return nil, fmt.Errorf("failed to marshal process %d info: %w", pid, err)
}
pInfo.Info = a
break

View File

@@ -21,6 +21,7 @@ package v1
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
@@ -49,7 +50,6 @@ import (
"github.com/containerd/typeurl"
"github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
@@ -79,7 +79,7 @@ func New(ctx context.Context, id string, publisher shim.Publisher, shutdown func
runcC.Monitor = reaper.Default
if err := s.initPlatform(); err != nil {
shutdown()
return nil, errors.Wrap(err, "failed to initialized platform behavior")
return nil, fmt.Errorf("failed to initialized platform behavior: %w", err)
}
go s.forward(ctx, publisher)
return s, nil
@@ -144,7 +144,7 @@ func (s *service) StartShim(ctx context.Context, opts shim.StartOpts) (_ string,
return "", err
}
if err := shim.RemoveSocket(address); err != nil {
return "", errors.Wrap(err, "remove already used socket")
return "", fmt.Errorf("remove already used socket: %w", err)
}
if socket, err = shim.NewSocket(address); err != nil {
return "", err
@@ -171,7 +171,7 @@ func (s *service) StartShim(ctx context.Context, opts shim.StartOpts) (_ string,
goruntime.LockOSThread()
if os.Getenv("SCHED_CORE") != "" {
if err := schedcore.Create(schedcore.ProcessGroup); err != nil {
return "", errors.Wrap(err, "enable sched core support")
return "", fmt.Errorf("enable sched core support: %w", err)
}
}
@@ -205,19 +205,19 @@ func (s *service) StartShim(ctx context.Context, opts shim.StartOpts) (_ string,
if opts.ShimCgroup != "" {
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(opts.ShimCgroup))
if err != nil {
return "", errors.Wrapf(err, "failed to load cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to load cgroup %s: %w", opts.ShimCgroup, err)
}
if err := cg.Add(cgroups.Process{
Pid: cmd.Process.Pid,
}); err != nil {
return "", errors.Wrapf(err, "failed to join cgroup %s", opts.ShimCgroup)
return "", fmt.Errorf("failed to join cgroup %s: %w", opts.ShimCgroup, err)
}
}
}
}
}
if err := shim.AdjustOOMScore(cmd.Process.Pid); err != nil {
return "", errors.Wrap(err, "failed to adjust OOM score for shim")
return "", fmt.Errorf("failed to adjust OOM score for shim: %w", err)
}
return address, nil
}
@@ -502,7 +502,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.Pi
}
a, err := typeurl.MarshalAny(d)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal process %d info", pid)
return nil, fmt.Errorf("failed to marshal process %d info: %w", pid, err)
}
pInfo.Info = a
break

View File

@@ -18,6 +18,7 @@ package v2
import (
"context"
"errors"
"fmt"
"io"
"os"
@@ -39,7 +40,6 @@ import (
"github.com/containerd/ttrpc"
ptypes "github.com/gogo/protobuf/types"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -85,7 +85,7 @@ func loadShim(ctx context.Context, bundle *Bundle, onClose func()) (_ *shimTask,
}()
f, err := openShimLog(shimCtx, bundle, client.AnonReconnectDialer)
if err != nil {
return nil, errors.Wrap(err, "open shim log pipe when reload")
return nil, fmt.Errorf("open shim log pipe when reload: %w", err)
}
defer func() {
if err != nil {
@@ -394,7 +394,7 @@ func (s *shimTask) Kill(ctx context.Context, signal uint32, all bool) error {
func (s *shimTask) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.ExecProcess, error) {
if err := identifiers.Validate(id); err != nil {
return nil, errors.Wrapf(err, "invalid exec id %s", id)
return nil, fmt.Errorf("invalid exec id %s: %w", id, err)
}
request := &task.ExecProcessRequest{
ID: s.ID(),

View File

@@ -18,6 +18,7 @@ package shim
import (
"context"
"errors"
"flag"
"fmt"
"io"
@@ -36,7 +37,6 @@ import (
"github.com/containerd/containerd/version"
"github.com/containerd/ttrpc"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -398,7 +398,7 @@ func run(ctx context.Context, manager Manager, initFunc Init, name string, confi
result := p.Init(initContext)
if err := initialized.Add(result); err != nil {
return errors.Wrapf(err, "could not add plugin result to plugin set")
return fmt.Errorf("could not add plugin result to plugin set: %w", err)
}
instance, err := result.Instance()
@@ -419,12 +419,12 @@ func run(ctx context.Context, manager Manager, initFunc Init, name string, confi
server, err := newServer()
if err != nil {
return errors.Wrap(err, "failed creating server")
return fmt.Errorf("failed creating server: %w", err)
}
for _, srv := range ttrpcServices {
if err := srv.RegisterTTRPC(server); err != nil {
return errors.Wrap(err, "failed to register service")
return fmt.Errorf("failed to register service: %w", err)
}
}

View File

@@ -21,6 +21,7 @@ package shim
import (
"context"
"fmt"
"io"
"net"
"os"
@@ -29,7 +30,6 @@ import (
"github.com/containerd/containerd/sys/reaper"
"github.com/containerd/fifo"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
@@ -60,7 +60,7 @@ func serveListener(path string) (net.Listener, error) {
path = "[inherited from parent]"
} else {
if len(path) > socketPathLimit {
return nil, errors.Errorf("%q: unix socket path too long (> %d)", path, socketPathLimit)
return nil, fmt.Errorf("%q: unix socket path too long (> %d)", path, socketPathLimit)
}
l, err = net.Listen("unix", path)
}

View File

@@ -18,12 +18,12 @@ package shim
import (
"context"
"errors"
"io"
"net"
"os"
"github.com/containerd/ttrpc"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

View File

@@ -19,6 +19,7 @@ package shim
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"os"
@@ -29,7 +30,6 @@ import (
"github.com/containerd/containerd/namespaces"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
)

View File

@@ -33,7 +33,6 @@ import (
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/sys"
"github.com/pkg/errors"
)
const (
@@ -54,11 +53,11 @@ func AdjustOOMScore(pid int) error {
parent := os.Getppid()
score, err := sys.GetOOMScoreAdj(parent)
if err != nil {
return errors.Wrap(err, "get parent OOM score")
return fmt.Errorf("get parent OOM score: %w", err)
}
shimScore := score + 1
if err := sys.AdjustOOMScore(pid, shimScore); err != nil {
return errors.Wrap(err, "set shim OOM score")
return fmt.Errorf("set shim OOM score: %w", err)
}
return nil
}
@@ -96,7 +95,7 @@ func NewSocket(address string) (*net.UnixListener, error) {
if !isAbstract {
if err := os.MkdirAll(filepath.Dir(path), 0600); err != nil {
return nil, errors.Wrapf(err, "%s", path)
return nil, fmt.Errorf("%s: %w", path, err)
}
}
l, err := net.Listen("unix", path)

View File

@@ -18,13 +18,13 @@ package shim
import (
"context"
"fmt"
"net"
"os"
"syscall"
"time"
winio "github.com/Microsoft/go-winio"
"github.com/pkg/errors"
)
const shimBinaryFormat = "containerd-shim-%s-%s.exe"
@@ -40,9 +40,9 @@ func AnonReconnectDialer(address string, timeout time.Duration) (net.Conn, error
c, err := winio.DialPipeContext(ctx, address)
if os.IsNotExist(err) {
return nil, errors.Wrap(os.ErrNotExist, "npipe not found on reconnect")
return nil, fmt.Errorf("npipe not found on reconnect: %w", os.ErrNotExist)
} else if err == context.DeadlineExceeded {
return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address)
return nil, fmt.Errorf("timed out waiting for npipe %s: %w", address, err)
} else if err != nil {
return nil, err
}
@@ -65,14 +65,14 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
if os.IsNotExist(err) {
select {
case <-serveTimer.C:
return nil, errors.Wrap(os.ErrNotExist, "pipe not found before timeout")
return nil, fmt.Errorf("pipe not found before timeout: %w", os.ErrNotExist)
default:
// Wait 10ms for the shim to serve and try again.
time.Sleep(10 * time.Millisecond)
continue
}
} else if err == context.DeadlineExceeded {
return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address)
return nil, fmt.Errorf("timed out waiting for npipe %s: %w", address, err)
}
return nil, err
}

View File

@@ -21,6 +21,7 @@ package v2
import (
"context"
"errors"
"io"
"net"
"os"
@@ -28,7 +29,6 @@ import (
"time"
"github.com/containerd/fifo"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

View File

@@ -18,6 +18,7 @@ package v2
import (
"context"
"errors"
"fmt"
"io"
"net"
@@ -26,7 +27,6 @@ import (
"time"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
)
type deferredPipeConnection struct {
@@ -78,7 +78,7 @@ func openShimLog(ctx context.Context, bundle *Bundle, dialer func(string, time.D
time.Second*10,
)
if conerr != nil {
dpc.conerr = errors.Wrap(conerr, "failed to connect to shim log")
dpc.conerr = fmt.Errorf("failed to connect to shim log: %w", conerr)
}
dpc.c = c
dpc.wg.Done()

View File

@@ -18,10 +18,9 @@ package v2
import (
"context"
"errors"
"os"
"testing"
"github.com/pkg/errors"
)
func TestCheckCopyShimLogError(t *testing.T) {