From 534d566c1076af2fc2d886b8e98e4ce5df57bf5f Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Wed, 4 May 2022 17:37:04 +0000 Subject: [PATCH] Remove pkg/errors This commit removes the package with the help of https://github.com/xdg-go/go-rewrap-errors. fmt.Errorf() can wrap errors nowadays. Signed-off-by: Kazuyoshi Kato --- cni/internal/cniutil.go | 7 ++--- cni/internal/netlink.go | 31 +++++++++---------- cni/vmconf/vmconf.go | 14 ++++----- go.mod | 1 - machine.go | 7 +++-- network.go | 66 ++++++++++++++++++----------------------- vsock/dial.go | 20 ++++++------- vsock/listener.go | 6 ++-- 8 files changed, 71 insertions(+), 81 deletions(-) diff --git a/cni/internal/cniutil.go b/cni/internal/cniutil.go index 92653cb8..d4ff6f17 100644 --- a/cni/internal/cniutil.go +++ b/cni/internal/cniutil.go @@ -17,7 +17,6 @@ import ( "fmt" current "github.com/containernetworking/cni/pkg/types/100" - "github.com/pkg/errors" ) // InterfaceIPs returns the IPs associated with the interface possessing the provided name and @@ -78,8 +77,7 @@ func VMTapPair( ) { vmIfaces, otherIfaces := FilterBySandbox(vmID, result.Interfaces...) if len(vmIfaces) > 1 { - return nil, nil, errors.Errorf( - "expected to find at most 1 interface in sandbox %q, but instead found %d", + return nil, nil, fmt.Errorf("expected to find at most 1 interface in sandbox %q, but instead found %d", vmID, len(vmIfaces)) } else if len(vmIfaces) == 0 { return nil, nil, LinkNotFoundError{device: fmt.Sprintf("pseudo-device for %s", vmID)} @@ -94,8 +92,7 @@ func VMTapPair( tapIfaces := IfacesWithName(tapName, otherIfaces...) if len(tapIfaces) > 1 { - return nil, nil, errors.Errorf( - "expected to find at most 1 interface with name %q, but instead found %d", + return nil, nil, fmt.Errorf("expected to find at most 1 interface with name %q, but instead found %d", tapName, len(tapIfaces)) } else if len(tapIfaces) == 0 { return nil, nil, LinkNotFoundError{device: tapName} diff --git a/cni/internal/netlink.go b/cni/internal/netlink.go index 4f134de0..bd125642 100644 --- a/cni/internal/netlink.go +++ b/cni/internal/netlink.go @@ -16,7 +16,6 @@ package internal import ( "fmt" - "github.com/pkg/errors" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" ) @@ -83,7 +82,7 @@ var _ NetlinkOps = &defaultNetlinkOps{} func (ops defaultNetlinkOps) AddIngressQdisc(link netlink.Link) error { err := netlink.QdiscAdd(ops.ingressQdisc(link)) if err != nil { - err = errors.Wrapf(err, "failed to add ingress qdisc to device %q", link.Attrs().Name) + err = fmt.Errorf("failed to add ingress qdisc to device %q: %w", link.Attrs().Name, err) } return err @@ -97,7 +96,7 @@ func (ops defaultNetlinkOps) RemoveIngressQdisc(link netlink.Link) error { err = netlink.QdiscDel(qdisc) if err != nil { - return errors.Wrapf(err, "failed to remove ingress qdisc from device %q", link.Attrs().Name) + return fmt.Errorf("failed to remove ingress qdisc from device %q: %w", link.Attrs().Name, err) } return nil @@ -106,7 +105,7 @@ func (ops defaultNetlinkOps) RemoveIngressQdisc(link netlink.Link) error { func (ops defaultNetlinkOps) GetIngressQdisc(link netlink.Link) (netlink.Qdisc, error) { qdiscs, err := netlink.QdiscList(link) if err != nil { - return nil, errors.Wrapf(err, "failed to list qdiscs for link %q", link.Attrs().Name) + return nil, fmt.Errorf("failed to list qdiscs for link %q: %w", link.Attrs().Name, err) } expectedQdisc := ops.ingressQdisc(link) @@ -146,9 +145,9 @@ func (ops defaultNetlinkOps) AddRedirectFilter(sourceLink netlink.Link, targetLi }, }) if err != nil { - err = errors.Wrapf(err, - "failed to add u32 filter redirecting from device %q to device %q, does %q exist and have a qdisc attached to its ingress?", - sourceLink.Attrs().Name, targetLink.Attrs().Name, sourceLink.Attrs().Name) + err = fmt.Errorf("failed to add u32 filter redirecting from device %q to device %q, does %q exist and have a qdisc attached to its ingress?: %w", + sourceLink.Attrs().Name, targetLink.Attrs().Name, sourceLink.Attrs().Name, err) + } return err @@ -157,7 +156,7 @@ func (ops defaultNetlinkOps) AddRedirectFilter(sourceLink netlink.Link, targetLi func (ops defaultNetlinkOps) GetRedirectFilter(sourceLink netlink.Link, targetLink netlink.Link) (netlink.Filter, error) { filters, err := netlink.FilterList(sourceLink, RootFilterHandle()) if err != nil { - return nil, errors.Wrapf(err, "failed to list filters for device %q", sourceLink.Attrs().Name) + return nil, fmt.Errorf("failed to list filters for device %q: %w", sourceLink.Attrs().Name, err) } for _, filter := range filters { @@ -214,31 +213,33 @@ func (defaultNetlinkOps) CreateTap(name string, mtu int, ownerUID, ownerGID int) err := netlink.LinkAdd(tapLink) if err != nil { - return nil, errors.Wrap(err, "failed to create tap device") + return nil, fmt.Errorf("failed to create tap device: %w", err) } for _, tapFd := range tapLink.Fds { err = unix.IoctlSetInt(int(tapFd.Fd()), unix.TUNSETOWNER, ownerUID) if err != nil { - return nil, errors.Wrapf(err, "failed to set tap %s owner to uid %d", - name, ownerUID) + return nil, fmt.Errorf("failed to set tap %s owner to uid %d: %w", + name, ownerUID, err) + } err = unix.IoctlSetInt(int(tapFd.Fd()), unix.TUNSETGROUP, ownerGID) if err != nil { - return nil, errors.Wrapf(err, "failed to set tap %s group to gid %d", - name, ownerGID) + return nil, fmt.Errorf("failed to set tap %s group to gid %d: %w", + name, ownerGID, err) + } } err = netlink.LinkSetMTU(tapLink, mtu) if err != nil { - return nil, errors.Wrapf(err, "failed to set tap device MTU to %d", mtu) + return nil, fmt.Errorf("failed to set tap device MTU to %d: %w", mtu, err) } err = netlink.LinkSetUp(tapLink) if err != nil { - return nil, errors.Wrap(err, "failed to set tap up") + return nil, fmt.Errorf("failed to set tap up: %w", err) } return tapLink, nil diff --git a/cni/vmconf/vmconf.go b/cni/vmconf/vmconf.go index dd1142f8..f0f7e2ab 100644 --- a/cni/vmconf/vmconf.go +++ b/cni/vmconf/vmconf.go @@ -35,7 +35,6 @@ import ( "github.com/containernetworking/cni/pkg/types" current "github.com/containernetworking/cni/pkg/types/100" "github.com/containernetworking/plugins/pkg/ns" - "github.com/pkg/errors" "github.com/firecracker-microvm/firecracker-go-sdk/cni/internal" ) @@ -153,7 +152,7 @@ func (c StaticNetworkConf) IPBootParam() string { func StaticNetworkConfFrom(result types.Result, containerID string) (*StaticNetworkConf, error) { currentResult, err := current.NewResultFromResult(result) if err != nil { - return nil, errors.Wrap(err, "failed to parse cni result") + return nil, fmt.Errorf("failed to parse cni result: %w", err) } // As specified in the vmconf package docstring, we are looking for an interface who's @@ -170,14 +169,14 @@ func StaticNetworkConfFrom(result types.Result, containerID string) (*StaticNetw // find the IP associated with the VM iface vmIPs := internal.InterfaceIPs(currentResult, vmIface.Name, vmIface.Sandbox) if len(vmIPs) != 1 { - return nil, errors.Errorf("expected to find 1 IP for vm interface %q, but instead found %+v", + return nil, fmt.Errorf("expected to find 1 IP for vm interface %q, but instead found %+v", vmIface.Name, vmIPs) } vmIP := vmIPs[0] netNS, err := ns.GetNS(tapIface.Sandbox) if err != nil { - return nil, errors.Wrapf(err, "failed to find netns at path %q", tapIface.Sandbox) + return nil, fmt.Errorf("failed to find netns at path %q: %w", tapIface.Sandbox, err) } tapMTU, err := mtuOf(tapIface.Name, netNS, internal.DefaultNetlinkOps()) @@ -204,15 +203,16 @@ func mtuOf(ifaceName string, netNS ns.NetNS, netlinkOps internal.NetlinkOps) (in err := netNS.Do(func(_ ns.NetNS) error { link, err := netlinkOps.GetLink(ifaceName) if err != nil { - return errors.Wrapf(err, "failed to find device %q in netns %q", - ifaceName, netNS.Path()) + return fmt.Errorf("failed to find device %q in netns %q: %w", + ifaceName, netNS.Path(), err) + } mtu = link.Attrs().MTU return nil }) if err != nil { - return 0, errors.Wrap(err, "failed to find MTU") + return 0, fmt.Errorf("failed to find MTU: %w", err) } return mtu, nil diff --git a/go.mod b/go.mod index 8d1ac943..2fdaeb2f 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/google/uuid v1.3.0 github.com/hashicorp/go-multierror v1.1.1 github.com/mdlayher/vsock v1.1.1 - github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.1 github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5 diff --git a/machine.go b/machine.go index 5bdadc92..af55a2b9 100644 --- a/machine.go +++ b/machine.go @@ -16,6 +16,7 @@ package firecracker import ( "context" "encoding/json" + "errors" "fmt" "io" "net" @@ -33,7 +34,7 @@ import ( "github.com/containernetworking/plugins/pkg/ns" "github.com/google/uuid" "github.com/hashicorp/go-multierror" - "github.com/pkg/errors" + log "github.com/sirupsen/logrus" models "github.com/firecracker-microvm/firecracker-go-sdk/client/models" @@ -320,7 +321,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) if cfg.VMID == "" { id, err := uuid.NewRandom() if err != nil { - return nil, errors.Wrap(err, "failed to create random ID for VMID") + return nil, fmt.Errorf("failed to create random ID for VMID: %w", err) } cfg.VMID = id.String() } @@ -549,7 +550,7 @@ func (m *Machine) startVMM(ctx context.Context) error { // Wait for firecracker to initialize: err = m.waitForSocket(time.Duration(m.client.firecrackerInitTimeout)*time.Second, errCh) if err != nil { - err = errors.Wrapf(err, "Firecracker did not create API socket %s", m.Cfg.SocketPath) + err = fmt.Errorf("Firecracker did not create API socket %s: %w", m.Cfg.SocketPath, err) m.fatalErr = err close(m.exitCh) diff --git a/network.go b/network.go index 6321abd1..35352996 100644 --- a/network.go +++ b/network.go @@ -25,7 +25,7 @@ import ( "github.com/containernetworking/cni/pkg/types" current "github.com/containernetworking/cni/pkg/types/100" "github.com/containernetworking/plugins/pkg/ns" - "github.com/pkg/errors" + log "github.com/sirupsen/logrus" "golang.org/x/sys/unix" @@ -50,8 +50,7 @@ func (networkInterfaces NetworkInterfaces) validate(kernelArgs kernelArgs) error hasStaticIP := hasStaticInterface && iface.StaticConfiguration.IPConfiguration != nil if !hasCNI && !hasStaticInterface { - return errors.Errorf( - "must specify at least one of CNIConfiguration or StaticConfiguration for network interfaces: %+v", networkInterfaces) + return fmt.Errorf("must specify at least one of CNIConfiguration or StaticConfiguration for network interfaces: %+v", networkInterfaces) } if hasCNI && hasStaticInterface { @@ -60,21 +59,18 @@ func (networkInterfaces NetworkInterfaces) validate(kernelArgs kernelArgs) error // specified statically rather than parsed from the CNI result via vmconf. // This may be useful in some scenarios, but the full implications of enabling it have not yet been considered or // tested, so for now providing both is blocked to prevent any regrettable one-way doors. - return errors.Errorf( - "cannot provide both CNIConfiguration and StaticConfiguration for a network interface: %+v", iface) + return fmt.Errorf("cannot provide both CNIConfiguration and StaticConfiguration for a network interface: %+v", iface) } if hasCNI || hasStaticIP { // due to limitations of using "ip=" kernel boot param, currently only one network interface can be provided // when a static IP is going to be configured. if len(networkInterfaces) > 1 { - return errors.Errorf( - "cannot specify CNIConfiguration or IPConfiguration when multiple network interfaces are provided: %+v", networkInterfaces) + return fmt.Errorf("cannot specify CNIConfiguration or IPConfiguration when multiple network interfaces are provided: %+v", networkInterfaces) } if argVal, ok := kernelArgs["ip"]; ok { - return errors.Errorf( - `CNIConfiguration or IPConfiguration cannot be specified when "ip=" provided in kernel boot args, value found: "%v"`, argVal) + return fmt.Errorf(`CNIConfiguration or IPConfiguration cannot be specified when "ip=" provided in kernel boot args, value found: "%v"`, argVal) } } @@ -121,13 +117,13 @@ func (networkInterfaces NetworkInterfaces) setupNetwork( err, netnsCleanupFuncs := cniNetworkInterface.CNIConfiguration.initializeNetNS() cleanupFuncs = append(cleanupFuncs, netnsCleanupFuncs...) if err != nil { - return errors.Wrap(err, "failed to initialize netns"), cleanupFuncs + return fmt.Errorf("failed to initialize netns: %w", err), cleanupFuncs } cniResult, err, cniCleanupFuncs := cniNetworkInterface.CNIConfiguration.invokeCNI(ctx, logger) cleanupFuncs = append(cleanupFuncs, cniCleanupFuncs...) if err != nil { - return errors.Wrap(err, "failure when invoking CNI"), cleanupFuncs + return fmt.Errorf("failure when invoking CNI: %w", err), cleanupFuncs } // If static configuration is not already set for the network device, fill it out @@ -136,10 +132,8 @@ func (networkInterfaces NetworkInterfaces) setupNetwork( if cniNetworkInterface.StaticConfiguration == nil { vmNetConf, err := vmconf.StaticNetworkConfFrom(*cniResult, cniNetworkInterface.CNIConfiguration.containerID) if err != nil { - return errors.Wrap(err, - "failed to parse VM network configuration from CNI output, ensure CNI is configured with a plugin "+ - "that supports automatic VM network configuration such as tc-redirect-tap", - ), cleanupFuncs + return fmt.Errorf("failed to parse VM network configuration from CNI output, ensure CNI is configured with a plugin "+ + "that supports automatic VM network configuration such as tc-redirect-tap"+": %w", err), cleanupFuncs } cniNetworkInterface.StaticConfiguration = &StaticNetworkConfiguration{ @@ -290,11 +284,11 @@ type CNIConfiguration struct { func (cniConf CNIConfiguration) validate() error { if cniConf.NetworkName == "" && cniConf.NetworkConfig == nil { - return errors.Errorf("must specify either NetworkName or NetworkConfig in CNIConfiguration: %+v", cniConf) + return fmt.Errorf("must specify either NetworkName or NetworkConfig in CNIConfiguration: %+v", cniConf) } if cniConf.NetworkName != "" && cniConf.NetworkConfig != nil { - return errors.Errorf("must not specify both NetworkName and NetworkConfig in CNIConfiguration: %+v", cniConf) + return fmt.Errorf("must not specify both NetworkName and NetworkConfig in CNIConfiguration: %+v", cniConf) } return nil @@ -335,8 +329,8 @@ func (cniConf CNIConfiguration) invokeCNI(ctx context.Context, logger *log.Entry if networkConf == nil { networkConf, err = libcni.LoadConfList(cniConf.ConfDir, cniConf.NetworkName) if err != nil { - return nil, errors.Wrapf(err, "failed to load CNI configuration from dir %q for network %q", - cniConf.ConfDir, cniConf.NetworkName), cleanupFuncs + return nil, fmt.Errorf("failed to load CNI configuration from dir %q for network %q: %w", + cniConf.ConfDir, cniConf.NetworkName, err), cleanupFuncs } } @@ -345,7 +339,7 @@ func (cniConf CNIConfiguration) invokeCNI(ctx context.Context, logger *log.Entry delNetworkFunc := func() error { err := cniPlugin.DelNetworkList(ctx, networkConf, runtimeConf) if err != nil { - return errors.Wrapf(err, "failed to delete CNI network list %q", cniConf.NetworkName) + return fmt.Errorf("failed to delete CNI network list %q: %w", cniConf.NetworkName, err) } return nil } @@ -364,7 +358,7 @@ func (cniConf CNIConfiguration) invokeCNI(ctx context.Context, logger *log.Entry if !cniConf.Force { // something actually went wrong deleting the network, return an error and Force wasn't used so we don't // try to create a new network on top of a possibly half-deleted previous one. - return nil, errors.Wrap(err, errMsg), cleanupFuncs + return nil, fmt.Errorf(errMsg+": %w", err), cleanupFuncs } logger.Error(err, errMsg) } @@ -375,7 +369,7 @@ func (cniConf CNIConfiguration) invokeCNI(ctx context.Context, logger *log.Entry cleanupFuncs = append(cleanupFuncs, delNetworkFunc) cniResult, err := cniPlugin.AddNetworkList(ctx, networkConf, runtimeConf) if err != nil { - return nil, errors.Wrap(err, "failed to create CNI network"), cleanupFuncs + return nil, fmt.Errorf("failed to create CNI network: %w", err), cleanupFuncs } return &cniResult, nil, cleanupFuncs @@ -393,12 +387,12 @@ func (cniConf CNIConfiguration) initializeNetNS() (error, []func() error) { return nil, cleanupFuncs case ns.NSPathNotNSErr: // if the path exists but isn't a netns, return an error - return errors.Wrapf(err, "path %q does not appear to be a mounted netns", cniConf.netNSPath), cleanupFuncs + return fmt.Errorf("path %q does not appear to be a mounted netns: %w", cniConf.netNSPath, err), cleanupFuncs case ns.NSPathNotExistErr: // if the path doesn't exist, continue on to creating a new netns at the path default: // if something else bad happened return the error - return errors.Wrapf(err, "failure checking if %q is a mounted netns", cniConf.netNSPath), cleanupFuncs + return fmt.Errorf("failure checking if %q is a mounted netns: %w", cniConf.netNSPath, err), cleanupFuncs } // the path doesn't exist, so we need to create a new netns and mount it at the path @@ -409,7 +403,7 @@ func (cniConf CNIConfiguration) initializeNetNS() (error, []func() error) { if os.IsNotExist(err) { err = os.MkdirAll(parentDir, 0600) if err != nil { - return errors.Wrapf(err, "failed to create netns parent dir at %q", parentDir), cleanupFuncs + return fmt.Errorf("failed to create netns parent dir at %q: %w", parentDir, err), cleanupFuncs } cleanupFuncs = append(cleanupFuncs, func() error { @@ -418,26 +412,25 @@ func (cniConf CNIConfiguration) initializeNetNS() (error, []func() error) { // concurrently with us. err := os.Remove(parentDir) if err != nil { - return errors.Wrapf(err, "failed to remove netns parent dir %q", parentDir) + return fmt.Errorf("failed to remove netns parent dir %q: %w", parentDir, err) } return nil }) } else if err != nil { - return errors.Wrapf(err, "failed to check if netns parent dir exists at %q", parentDir), cleanupFuncs + return fmt.Errorf("failed to check if netns parent dir exists at %q: %w", parentDir, err), cleanupFuncs } // We need a file to exist at the path in order for the bind mount to succeed. fd, err := os.OpenFile(cniConf.netNSPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) if err != nil { - return errors.Wrapf(err, - "failed to open new netns path at %q", cniConf.netNSPath), cleanupFuncs + return fmt.Errorf("failed to open new netns path at %q: %w", cniConf.netNSPath, err), cleanupFuncs } fd.Close() cleanupFuncs = append(cleanupFuncs, func() error { err := os.Remove(cniConf.netNSPath) if err != nil { - return errors.Wrapf(err, "failed to remove netns path %q", cniConf.netNSPath) + return fmt.Errorf("failed to remove netns path %q: %w", cniConf.netNSPath, err) } return nil }) @@ -455,21 +448,21 @@ func (cniConf CNIConfiguration) initializeNetNS() (error, []func() error) { // create a new net ns err := unix.Unshare(unix.CLONE_NEWNET) if err != nil { - doneCh <- errors.Wrap(err, "failed to unshare netns") + doneCh <- fmt.Errorf("failed to unshare netns: %w", err) return } // mount the new netns at the destination path err = unix.Mount("/proc/thread-self/ns/net", cniConf.netNSPath, "none", unix.MS_BIND, "none") if err != nil { - doneCh <- errors.Wrapf(err, "failed to mount netns at path %q", cniConf.netNSPath) + doneCh <- fmt.Errorf("failed to mount netns at path %q: %w", cniConf.netNSPath, err) return } cleanupFuncs = append(cleanupFuncs, func() error { err := unix.Unmount(cniConf.netNSPath, unix.MNT_DETACH) if err != nil { - return errors.Wrapf(err, "failed to unmount netns at %q", cniConf.netNSPath) + return fmt.Errorf("failed to unmount netns at %q: %w", cniConf.netNSPath, err) } return nil }) @@ -496,8 +489,7 @@ type StaticNetworkConfiguration struct { func (staticConf StaticNetworkConfiguration) validate() error { if staticConf.HostDevName == "" { - return errors.Errorf( - "HostDevName must be provided if StaticNetworkConfiguration is provided: %+v", staticConf) + return fmt.Errorf("HostDevName must be provided if StaticNetworkConfiguration is provided: %+v", staticConf) } if staticConf.IPConfiguration != nil { @@ -533,12 +525,12 @@ func (ipConf IPConfiguration) validate() error { // Make sure only ipv4 is being provided (for now). for _, ip := range []net.IP{ipConf.IPAddr.IP, ipConf.Gateway} { if ip.To4() == nil { - return errors.Errorf("invalid ip, only ipv4 addresses are supported: %+v", ip) + return fmt.Errorf("invalid ip, only ipv4 addresses are supported: %+v", ip) } } if len(ipConf.Nameservers) > 2 { - return errors.Errorf("cannot specify more than 2 nameservers: %+v", ipConf.Nameservers) + return fmt.Errorf("cannot specify more than 2 nameservers: %+v", ipConf.Nameservers) } return nil diff --git a/vsock/dial.go b/vsock/dial.go index 58a48ab8..a3dda36a 100644 --- a/vsock/dial.go +++ b/vsock/dial.go @@ -16,13 +16,13 @@ package vsock import ( "bufio" "context" + "errors" "fmt" "io/ioutil" "net" "strings" "time" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -129,11 +129,11 @@ func dial(ctx context.Context, udsPath string, port uint32, c config) (net.Conn, case <-tickerCh: conn, err := tryConnect(logger, udsPath, port, c) if isTemporaryNetErr(err) { - err = errors.Wrap(err, "temporary vsock dial failure") + err = fmt.Errorf("temporary vsock dial failure: %w", err) logger.WithError(err).Debug() continue } else if err != nil { - err = errors.Wrap(err, "non-temporary vsock dial failure") + err = fmt.Errorf("non-temporary vsock dial failure: %w", err) logger.WithError(err).Error() return nil, err } @@ -156,7 +156,7 @@ func connectMsg(port uint32) string { func tryConnect(logger *logrus.Entry, udsPath string, port uint32, c config) (net.Conn, error) { conn, err := net.DialTimeout("unix", udsPath, c.DialTimeout) if err != nil { - return nil, errors.Wrapf(err, "failed to dial %q within %s", udsPath, c.DialTimeout) + return nil, fmt.Errorf("failed to dial %q within %s: %w", udsPath, c.DialTimeout, err) } defer func() { @@ -173,14 +173,14 @@ func tryConnect(logger *logrus.Entry, udsPath string, port uint32, c config) (ne err = tryConnWrite(conn, msg, c.ConnectMsgTimeout) if err != nil { return nil, connectMsgError{ - cause: errors.Wrapf(err, `failed to write %q within %s`, msg, c.ConnectMsgTimeout), + cause: fmt.Errorf(`failed to write %q within %s: %w`, msg, c.ConnectMsgTimeout, err), } } line, err := tryConnReadUntil(conn, '\n', c.AckMsgTimeout) if err != nil { return nil, ackError{ - cause: errors.Wrapf(err, `failed to read "OK " within %s`, c.AckMsgTimeout), + cause: fmt.Errorf(`failed to read "OK " within %s: %w`, c.AckMsgTimeout, err), } } @@ -188,7 +188,7 @@ func tryConnect(logger *logrus.Entry, udsPath string, port uint32, c config) (ne // https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md#host-initiated-connections if !strings.HasPrefix(line, "OK ") { return nil, ackError{ - cause: errors.Errorf(`expected to read "OK ", but instead read %q`, line), + cause: fmt.Errorf(`expected to read "OK ", but instead read %q`, line), } } return conn, nil @@ -220,7 +220,7 @@ func tryConnWrite(conn net.Conn, expectedWrite string, timeout time.Duration) er return err } if bytesWritten != len(expectedWrite) { - return errors.Errorf("incomplete write, expected %d bytes but wrote %d", + return fmt.Errorf("incomplete write, expected %d bytes but wrote %d", len(expectedWrite), bytesWritten) } @@ -232,7 +232,7 @@ type connectMsgError struct { } func (e connectMsgError) Error() string { - return errors.Wrap(e.cause, "vsock connect message failure").Error() + return fmt.Errorf("vsock connect message failure: %w", e.cause).Error() } func (e connectMsgError) Temporary() bool { @@ -248,7 +248,7 @@ type ackError struct { } func (e ackError) Error() string { - return errors.Wrap(e.cause, "vsock ack message failure").Error() + return fmt.Errorf("vsock ack message failure: %w", e.cause).Error() } func (e ackError) Temporary() bool { diff --git a/vsock/listener.go b/vsock/listener.go index fec9e978..e66222ea 100644 --- a/vsock/listener.go +++ b/vsock/listener.go @@ -15,11 +15,11 @@ package vsock import ( "context" + "fmt" "net" "time" "github.com/mdlayher/vsock" - "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -64,11 +64,11 @@ func (l listener) Accept() (net.Conn, error) { case <-tickerCh: conn, err := tryAccept(logger, l.listener, l.port) if isTemporaryNetErr(err) { - err = errors.Wrap(err, "temporary vsock accept failure") + err = fmt.Errorf("temporary vsock accept failure: %w", err) logger.WithError(err).Debug() continue } else if err != nil { - err = errors.Wrap(err, "non-temporary vsock accept failure") + err = fmt.Errorf("non-temporary vsock accept failure: %w", err) logger.WithError(err).Error() return nil, err }