Skip to content

Commit

Permalink
Merge pull request #71 from AkihiroSuda/remove-pkg-errors
Browse files Browse the repository at this point in the history
remove direct dependency on github.com/pkg/errors
  • Loading branch information
dmcgowan authored Aug 27, 2021
2 parents 548a5e1 + b6c559f commit 1697e73
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
cnilibrary "github.com/containernetworking/cni/libcni"
"github.com/containernetworking/cni/pkg/types"
types100 "github.com/containernetworking/cni/pkg/types/100"
"github.com/pkg/errors"
)

type CNI interface {
Expand Down Expand Up @@ -115,7 +114,7 @@ func (c *libcni) Load(opts ...Opt) error {

for _, o := range opts {
if err = o(c); err != nil {
return errors.Wrapf(ErrLoad, fmt.Sprintf("cni config load failed: %v", err))
return fmt.Errorf("cni config load failed: %v: %w", err, ErrLoad)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package cni

import (
"github.com/pkg/errors"
"errors"
)

var (
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/containerd/go-cni
require (
github.com/containernetworking/cni v1.0.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1ls
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
Expand Down
18 changes: 9 additions & 9 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package cni

import (
"fmt"
"sort"
"strings"

cnilibrary "github.com/containernetworking/cni/libcni"
"github.com/pkg/errors"
)

// Opt sets options for a CNI instance
Expand Down Expand Up @@ -204,9 +204,9 @@ func loadFromConfDir(c *libcni, max int) error {
files, err := cnilibrary.ConfFiles(c.pluginConfDir, []string{".conf", ".conflist", ".json"})
switch {
case err != nil:
return errors.Wrapf(ErrRead, "failed to read config file: %v", err)
return fmt.Errorf("failed to read config file: %v: %w", err, ErrRead)
case len(files) == 0:
return errors.Wrapf(ErrCNINotInitialized, "no network config found in %s", c.pluginConfDir)
return fmt.Errorf("no network config found in %s: %w", c.pluginConfDir, ErrCNINotInitialized)
}

// files contains the network config files associated with cni network.
Expand All @@ -224,26 +224,26 @@ func loadFromConfDir(c *libcni, max int) error {
if strings.HasSuffix(confFile, ".conflist") {
confList, err = cnilibrary.ConfListFromFile(confFile)
if err != nil {
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config list file %s: %v", confFile, err)
return fmt.Errorf("failed to load CNI config list file %s: %v: %w", confFile, err, ErrInvalidConfig)
}
} else {
conf, err := cnilibrary.ConfFromFile(confFile)
if err != nil {
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config file %s: %v", confFile, err)
return fmt.Errorf("failed to load CNI config file %s: %v: %w", confFile, err, ErrInvalidConfig)
}
// Ensure the config has a "type" so we know what plugin to run.
// Also catches the case where somebody put a conflist into a conf file.
if conf.Network.Type == "" {
return errors.Wrapf(ErrInvalidConfig, "network type not found in %s", confFile)
return fmt.Errorf("network type not found in %s: %w", confFile, ErrInvalidConfig)
}

confList, err = cnilibrary.ConfListFromConf(conf)
if err != nil {
return errors.Wrapf(ErrInvalidConfig, "failed to convert CNI config file %s to CNI config list: %v", confFile, err)
return fmt.Errorf("failed to convert CNI config file %s to CNI config list: %v: %w", confFile, err, ErrInvalidConfig)
}
}
if len(confList.Plugins) == 0 {
return errors.Wrapf(ErrInvalidConfig, "CNI config list in config file %s has no networks, skipping", confFile)
return fmt.Errorf("CNI config list in config file %s has no networks, skipping: %w", confFile, ErrInvalidConfig)

}
networks = append(networks, &Network{
Expand All @@ -257,7 +257,7 @@ func loadFromConfDir(c *libcni, max int) error {
}
}
if len(networks) == 0 {
return errors.Wrapf(ErrCNINotInitialized, "no valid networks found in %s", c.pluginDirs)
return fmt.Errorf("no valid networks found in %s: %w", c.pluginDirs, ErrCNINotInitialized)
}
c.networks = append(c.networks, networks...)
return nil
Expand Down
6 changes: 3 additions & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package cni

import (
"fmt"
"net"

"github.com/containernetworking/cni/pkg/types"
types100 "github.com/containernetworking/cni/pkg/types/100"
"github.com/pkg/errors"
)

type IPConfig struct {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (c *libcni) createResult(results []*types100.Result) (*Result, error) {
// interfaces
for _, ipConf := range result.IPs {
if err := validateInterfaceConfig(ipConf, len(result.Interfaces)); err != nil {
return nil, errors.Wrapf(ErrInvalidResult, "invalid interface config: %v", err)
return nil, fmt.Errorf("invalid interface config: %v: %w", err, ErrInvalidResult)
}
name := c.getInterfaceName(result.Interfaces, ipConf)
r.Interfaces[name].IPConfigs = append(r.Interfaces[name].IPConfigs,
Expand All @@ -97,7 +97,7 @@ func (c *libcni) createResult(results []*types100.Result) (*Result, error) {
r.Routes = append(r.Routes, result.Routes...)
}
if _, ok := r.Interfaces[defaultInterface(c.prefix)]; !ok {
return nil, errors.Wrapf(ErrNotFound, "default network not found for: %s", defaultInterface(c.prefix))
return nil, fmt.Errorf("default network not found for: %s: %w", defaultInterface(c.prefix), ErrNotFound)
}
return r, nil
}
Expand Down

0 comments on commit 1697e73

Please sign in to comment.