Skip to content

Commit 1ebc1b1

Browse files
Merge pull request awslabs#15 from austinvazquez/remove-errors
chore: remove dependency on errors package
2 parents 59f6b29 + 76b35c8 commit 1ebc1b1

File tree

7 files changed

+43
-47
lines changed

7 files changed

+43
-47
lines changed

cmd/tc-redirect-tap/main.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package main
1515

1616
import (
1717
"encoding/json"
18+
"errors"
19+
"fmt"
1820
"os"
1921
"strconv"
2022
"strings"
@@ -26,7 +28,6 @@ import (
2628
"github.com/containernetworking/plugins/pkg/ns"
2729
"github.com/containernetworking/plugins/pkg/utils/buildversion"
2830
"github.com/hashicorp/go-multierror"
29-
"github.com/pkg/errors"
3031

3132
pluginargs "github.com/awslabs/tc-redirect-tap/cmd/tc-redirect-tap/args"
3233
"github.com/awslabs/tc-redirect-tap/internal"
@@ -47,7 +48,7 @@ func add(args *skel.CmdArgs) error {
4748
}
4849

4950
if p.netNS == nil {
50-
return errors.Errorf("netns path %q does not exist", args.Netns)
51+
return fmt.Errorf("netns path %q does not exist", args.Netns)
5152
}
5253

5354
if p.currentResult == nil {
@@ -84,7 +85,7 @@ func check(args *skel.CmdArgs) error {
8485
}
8586

8687
if p.netNS == nil {
87-
return errors.Errorf("netns path %q does not exist", args.Netns)
88+
return fmt.Errorf("netns path %q does not exist", args.Netns)
8889
}
8990

9091
if p.currentResult == nil {
@@ -107,7 +108,7 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
107108
case ns.NSPathNotExistErr:
108109
netNS = nil
109110
default:
110-
return nil, errors.Wrapf(err, "failed to open netns at path %q", args.Netns)
111+
return nil, fmt.Errorf("failed to open netns at path %q: %w", args.Netns, err)
111112
}
112113
}
113114

@@ -117,7 +118,7 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
117118
case *NoPreviousResultError:
118119
currentResult = nil
119120
default:
120-
return nil, errors.Wrapf(err, "failure parsing previous CNI result")
121+
return nil, fmt.Errorf("failure parsing previous CNI result: %w", err)
121122
}
122123
}
123124

@@ -147,15 +148,15 @@ func newPlugin(args *skel.CmdArgs) (*plugin, error) {
147148
if tapUIDVal, wasDefined := parsedArgs[pluginargs.TCRedirectTapUID]; wasDefined {
148149
tapUID, err := strconv.Atoi(tapUIDVal)
149150
if err != nil {
150-
return nil, errors.Wrapf(err, "tapUID should be numeric convertible, got %q", tapUIDVal)
151+
return nil, fmt.Errorf("tapUID should be numeric convertible, got %q: %w", tapUIDVal, err)
151152
}
152153
plugin.tapUID = tapUID
153154
}
154155

155156
if tapGIDVal, wasDefined := parsedArgs[pluginargs.TCRedirectTapGID]; wasDefined {
156157
tapGID, err := strconv.Atoi(tapGIDVal)
157158
if err != nil {
158-
return nil, errors.Wrapf(err, "tapGID should be numeric convertible, got %q", tapGIDVal)
159+
return nil, fmt.Errorf("tapGID should be numeric convertible, got %q: %w", tapGIDVal, err)
159160
}
160161
plugin.tapGID = tapGID
161162
}
@@ -168,12 +169,12 @@ func getCurrentResult(args *skel.CmdArgs) (*current.Result, error) {
168169
cniConf := types.NetConf{}
169170
err := json.Unmarshal(args.StdinData, &cniConf)
170171
if err != nil {
171-
return nil, errors.Wrap(err, "failure checking for previous result output")
172+
return nil, fmt.Errorf("failure checking for previous result output: %w", err)
172173
}
173174

174175
err = version.ParsePrevResult(&cniConf)
175176
if err != nil {
176-
return nil, errors.Wrap(err, "failed to parse previous CNI result")
177+
return nil, fmt.Errorf("failed to parse previous CNI result: %w", err)
177178
}
178179

179180
if cniConf.PrevResult == nil {
@@ -182,8 +183,7 @@ func getCurrentResult(args *skel.CmdArgs) (*current.Result, error) {
182183

183184
currentResult, err := current.NewResultFromResult(cniConf.PrevResult)
184185
if err != nil {
185-
return nil, errors.Wrap(err,
186-
"failed to generate current result from previous CNI result")
186+
return nil, fmt.Errorf("failed to generate current result from previous CNI result: %w", err)
187187
}
188188

189189
return currentResult, nil
@@ -225,8 +225,7 @@ func (p plugin) add() error {
225225
return p.netNS.Do(func(_ ns.NetNS) error {
226226
redirectLink, err := p.GetLink(p.redirectInterfaceName)
227227
if err != nil {
228-
return errors.Wrapf(err,
229-
"failed to find redirect interface %q", p.redirectInterfaceName)
228+
return fmt.Errorf("failed to find redirect interface %q: %w", p.redirectInterfaceName, err)
230229
}
231230

232231
redirectIPs := internal.InterfaceIPs(
@@ -308,16 +307,18 @@ func (p plugin) del() error {
308307
case nil, *internal.QdiscNotFoundError:
309308
// we removed successfully or there already wasn't a qdisc, nothing to do
310309
default:
311-
multiErr = multierror.Append(multiErr, errors.Wrapf(err,
312-
"failed to remove ingress qdisc from %q", redirectLink.Attrs().Name))
310+
multiErr = multierror.Append(multiErr,
311+
fmt.Errorf("failed to remove ingres qdisc from %q: %w", redirectLink.Attrs().Name, err),
312+
)
313313
}
314314

315315
case *internal.LinkNotFoundError:
316316
// if the link doesn't exist, there's nothing to do
317317

318318
default:
319319
multiErr = multierror.Append(multiErr,
320-
errors.Wrapf(err, "failure finding device %q", p.redirectInterfaceName))
320+
fmt.Errorf("failure finding device %q: %w", p.redirectInterfaceName, err),
321+
)
321322
}
322323

323324
// if there was no previous result, we can't find the vm-tap pair, so we are done here
@@ -335,8 +336,9 @@ func (p plugin) del() error {
335336
case nil, *internal.LinkNotFoundError:
336337
// we removed successfully or someone else beat us to removing it first
337338
default:
338-
multiErr = multierror.Append(multiErr, errors.Wrapf(err,
339-
"failure removing device %q", tapIface.Name))
339+
multiErr = multierror.Append(multiErr,
340+
fmt.Errorf("failure removing device %q: %w", tapIface.Name, err),
341+
)
340342
}
341343

342344
case *internal.LinkNotFoundError:
@@ -406,7 +408,7 @@ func extractArgs(args string) (map[string]string, error) {
406408
for _, pairStr := range argumentsPairs {
407409
pair := strings.SplitN(pairStr, "=", 2)
408410
if len(pair) < 2 {
409-
return result, errors.Errorf("Invalid cni arguments format, %q", pairStr)
411+
return result, fmt.Errorf("Invalid cni arguments format, %q", pairStr)
410412
}
411413
result[pair[0]] = pair[1]
412414
}

cmd/tc-redirect-tap/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ package main
1515

1616
import (
1717
"encoding/json"
18+
"errors"
1819
"net"
1920
"testing"
2021

2122
"github.com/containernetworking/cni/pkg/skel"
2223
"github.com/containernetworking/cni/pkg/types"
2324
current "github.com/containernetworking/cni/pkg/types/100"
2425
"github.com/containernetworking/cni/pkg/version"
25-
"github.com/pkg/errors"
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828
"github.com/vishvananda/netlink"

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/containernetworking/cni v1.0.1
77
github.com/containernetworking/plugins v1.0.1
88
github.com/hashicorp/go-multierror v1.1.0
9-
github.com/pkg/errors v0.9.1
109
github.com/stretchr/testify v1.6.1
1110
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
1211
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR
473473
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
474474
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
475475
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
476-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
477476
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
478477
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
479478
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

internal/cniutil.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"fmt"
1818

1919
current "github.com/containernetworking/cni/pkg/types/100"
20-
"github.com/pkg/errors"
2120
)
2221

2322
// InterfaceIPs returns the IPs associated with the interface possessing the provided name and
@@ -78,7 +77,7 @@ func VMTapPair(
7877
) {
7978
vmIfaces, otherIfaces := FilterBySandbox(vmID, result.Interfaces...)
8079
if len(vmIfaces) > 1 {
81-
return nil, nil, errors.Errorf(
80+
return nil, nil, fmt.Errorf(
8281
"expected to find at most 1 interface in sandbox %q, but instead found %d",
8382
vmID, len(vmIfaces))
8483
} else if len(vmIfaces) == 0 {
@@ -94,7 +93,7 @@ func VMTapPair(
9493

9594
tapIfaces := IfacesWithName(tapName, otherIfaces...)
9695
if len(tapIfaces) > 1 {
97-
return nil, nil, errors.Errorf(
96+
return nil, nil, fmt.Errorf(
9897
"expected to find at most 1 interface with name %q, but instead found %d",
9998
tapName, len(tapIfaces))
10099
} else if len(tapIfaces) == 0 {

internal/netlink.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
package internal
1515

1616
import (
17+
"errors"
1718
"fmt"
1819

19-
"github.com/pkg/errors"
2020
"github.com/vishvananda/netlink"
2121
"golang.org/x/sys/unix"
2222
)
@@ -83,10 +83,10 @@ var _ NetlinkOps = &defaultNetlinkOps{}
8383
func (ops defaultNetlinkOps) AddIngressQdisc(link netlink.Link) error {
8484
err := netlink.QdiscAdd(ops.ingressQdisc(link))
8585
if err != nil {
86-
err = errors.Wrapf(err, "failed to add ingress qdisc to device %q", link.Attrs().Name)
86+
return fmt.Errorf("failed to add ingress qdisc to device %q: %w", link.Attrs().Name, err)
8787
}
8888

89-
return err
89+
return nil
9090
}
9191

9292
func (ops defaultNetlinkOps) RemoveIngressQdisc(link netlink.Link) error {
@@ -97,7 +97,7 @@ func (ops defaultNetlinkOps) RemoveIngressQdisc(link netlink.Link) error {
9797

9898
err = netlink.QdiscDel(qdisc)
9999
if err != nil {
100-
return errors.Wrapf(err, "failed to remove ingress qdisc from device %q", link.Attrs().Name)
100+
return fmt.Errorf("failed to remove ingress qdisc from device %q: %w", link.Attrs().Name, err)
101101
}
102102

103103
return nil
@@ -106,7 +106,7 @@ func (ops defaultNetlinkOps) RemoveIngressQdisc(link netlink.Link) error {
106106
func (ops defaultNetlinkOps) GetIngressQdisc(link netlink.Link) (netlink.Qdisc, error) {
107107
qdiscs, err := netlink.QdiscList(link)
108108
if err != nil {
109-
return nil, errors.Wrapf(err, "failed to list qdiscs for link %q", link.Attrs().Name)
109+
return nil, fmt.Errorf("failed to list qdiscs for link %q: %w", link.Attrs().Name, err)
110110
}
111111

112112
expectedQdisc := ops.ingressQdisc(link)
@@ -146,9 +146,10 @@ func (ops defaultNetlinkOps) AddRedirectFilter(sourceLink netlink.Link, targetLi
146146
},
147147
})
148148
if err != nil {
149-
err = errors.Wrapf(err,
149+
err = fmt.Errorf(
150150
"failed to add u32 filter redirecting from device %q to device %q, does %q exist and have a qdisc attached to its ingress?",
151-
sourceLink.Attrs().Name, targetLink.Attrs().Name, sourceLink.Attrs().Name)
151+
sourceLink.Attrs().Name, targetLink.Attrs().Name, sourceLink.Attrs().Name,
152+
)
152153
}
153154

154155
return err
@@ -157,7 +158,7 @@ func (ops defaultNetlinkOps) AddRedirectFilter(sourceLink netlink.Link, targetLi
157158
func (ops defaultNetlinkOps) GetRedirectFilter(sourceLink netlink.Link, targetLink netlink.Link) (netlink.Filter, error) {
158159
filters, err := netlink.FilterList(sourceLink, RootFilterHandle())
159160
if err != nil {
160-
return nil, errors.Wrapf(err, "failed to list filters for device %q", sourceLink.Attrs().Name)
161+
return nil, fmt.Errorf("failed to list filters for device %q: %w", sourceLink.Attrs().Name, err)
161162
}
162163

163164
for _, filter := range filters {
@@ -214,31 +215,29 @@ func (defaultNetlinkOps) CreateTap(name string, mtu int, ownerUID, ownerGID int)
214215

215216
err := netlink.LinkAdd(tapLink)
216217
if err != nil {
217-
return nil, errors.Wrap(err, "failed to create tap device")
218+
return nil, fmt.Errorf("failed to create tap device: %w", err)
218219
}
219220

220221
for _, tapFd := range tapLink.Fds {
221222
err = unix.IoctlSetInt(int(tapFd.Fd()), unix.TUNSETOWNER, ownerUID)
222223
if err != nil {
223-
return nil, errors.Wrapf(err, "failed to set tap %s owner to uid %d",
224-
name, ownerUID)
224+
return nil, fmt.Errorf("failed to set tap %s owner to uid %d: %w", name, ownerUID, err)
225225
}
226226

227227
err = unix.IoctlSetInt(int(tapFd.Fd()), unix.TUNSETGROUP, ownerGID)
228228
if err != nil {
229-
return nil, errors.Wrapf(err, "failed to set tap %s group to gid %d",
230-
name, ownerGID)
229+
return nil, fmt.Errorf("failed to set tap %s group to gid %d: %w", name, ownerGID, err)
231230
}
232231
}
233232

234233
err = netlink.LinkSetMTU(tapLink, mtu)
235234
if err != nil {
236-
return nil, errors.Wrapf(err, "failed to set tap device MTU to %d", mtu)
235+
return nil, fmt.Errorf("failed to set tap device MTU to %d: %w", mtu, err)
237236
}
238237

239238
err = netlink.LinkSetUp(tapLink)
240239
if err != nil {
241-
return nil, errors.Wrap(err, "failed to set tap up")
240+
return nil, errors.New("failed to set tap up")
242241
}
243242

244243
return tapLink, nil

vmconf/vmconf.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/containernetworking/cni/pkg/types"
3636
current "github.com/containernetworking/cni/pkg/types/100"
3737
"github.com/containernetworking/plugins/pkg/ns"
38-
"github.com/pkg/errors"
3938

4039
"github.com/awslabs/tc-redirect-tap/internal"
4140
)
@@ -153,7 +152,7 @@ func (c StaticNetworkConf) IPBootParam() string {
153152
func StaticNetworkConfFrom(result types.Result, containerID string) (*StaticNetworkConf, error) {
154153
currentResult, err := current.NewResultFromResult(result)
155154
if err != nil {
156-
return nil, errors.Wrap(err, "failed to parse cni result")
155+
return nil, fmt.Errorf("failed to parse cni result: %w", err)
157156
}
158157

159158
// 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
170169
// find the IP associated with the VM iface
171170
vmIPs := internal.InterfaceIPs(currentResult, vmIface.Name, vmIface.Sandbox)
172171
if len(vmIPs) != 1 {
173-
return nil, errors.Errorf("expected to find 1 IP for vm interface %q, but instead found %+v",
172+
return nil, fmt.Errorf("expected to find 1 IP for vm interface %q, but instead found %+v",
174173
vmIface.Name, vmIPs)
175174
}
176175
vmIP := vmIPs[0]
177176

178177
netNS, err := ns.GetNS(tapIface.Sandbox)
179178
if err != nil {
180-
return nil, errors.Wrapf(err, "failed to find netns at path %q", tapIface.Sandbox)
179+
return nil, fmt.Errorf("failed to find netns at path %q: %w", tapIface.Sandbox, err)
181180
}
182181

183182
tapMTU, err := mtuOf(tapIface.Name, netNS, internal.DefaultNetlinkOps())
@@ -204,15 +203,14 @@ func mtuOf(ifaceName string, netNS ns.NetNS, netlinkOps internal.NetlinkOps) (in
204203
err := netNS.Do(func(_ ns.NetNS) error {
205204
link, err := netlinkOps.GetLink(ifaceName)
206205
if err != nil {
207-
return errors.Wrapf(err, "failed to find device %q in netns %q",
208-
ifaceName, netNS.Path())
206+
return fmt.Errorf("failed to find device %q in netns %q: %w", ifaceName, netNS.Path(), err)
209207
}
210208
mtu = link.Attrs().MTU
211209

212210
return nil
213211
})
214212
if err != nil {
215-
return 0, errors.Wrap(err, "failed to find MTU")
213+
return 0, fmt.Errorf("failed to find MTU: %w", err)
216214
}
217215

218216
return mtu, nil

0 commit comments

Comments
 (0)