Skip to content

Commit

Permalink
Remove pkg/errors
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kzys committed May 4, 2022
1 parent f3b934a commit 534d566
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 81 deletions.
7 changes: 2 additions & 5 deletions cni/internal/cniutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}
Expand All @@ -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}
Expand Down
31 changes: 16 additions & 15 deletions cni/internal/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package internal
import (
"fmt"

"github.com/pkg/errors"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions cni/vmconf/vmconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package firecracker
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)

Expand Down
Loading

0 comments on commit 534d566

Please sign in to comment.