Skip to content

Commit

Permalink
pkg: eliminate usage of github.com/pkg/errors.
Browse files Browse the repository at this point in the history
Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Jun 26, 2023
1 parent 1212598 commit b67041d
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 47 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/k8stopologyawareschedwg/noderesourcetopology-api v0.1.0
github.com/onsi/ginkgo/v2 v2.9.1
github.com/onsi/gomega v1.27.4
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.42.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb/go.m
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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=
Expand Down
16 changes: 7 additions & 9 deletions pkg/pidfile/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"strconv"
"strings"
"syscall"

"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -51,18 +49,18 @@ func Write() error {

err := os.MkdirAll(filepath.Dir(pidFilePath), 0755)
if err != nil {
return errors.Wrap(err, "failed to create PID file")
return fmt.Errorf("failed to create PID file: %w", err)
}

pidFile, err = os.OpenFile(pidFilePath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return errors.Wrap(err, "failed to create PID file")
return fmt.Errorf("failed to create PID file: %w", err)
}

_, err = pidFile.Write([]byte(fmt.Sprintf("%d\n", os.Getpid())))
if err != nil {
close()
return errors.Wrap(err, "failed to write PID file")
return fmt.Errorf("failed to write PID file: %w", err)
}

return nil
Expand All @@ -82,11 +80,11 @@ func Read() (int, error) {
if os.IsNotExist(err) {
return 0, nil
}
return -1, errors.Wrap(err, "failed to read PID file")
return -1, fmt.Errorf("failed to read PID file: %w", err)
}

if pid, err = strconv.Atoi(strings.TrimRight(string(buf), "\n")); err != nil {
return -1, errors.Wrapf(err, "invalid PID (%q) in PID file", string(buf))
return -1, fmt.Errorf("invalid PID (%q) in PID file: %w", string(buf), err)
}

return pid, nil
Expand Down Expand Up @@ -134,7 +132,7 @@ func OwnerPid() (int, error) {

p, err = os.FindProcess(pid)
if err != nil {
return -1, errors.Wrapf(err, "FindProcess() failed for PID %d", pid)
return -1, fmt.Errorf("FindProcess() failed for PID %d: %w", pid, err)
}

err = p.Signal(syscall.Signal(0))
Expand All @@ -145,7 +143,7 @@ func OwnerPid() (int, error) {
return pid, nil
}

return -1, errors.Wrapf(err, "failed to check process %d", pid)
return -1, fmt.Errorf("failed to check process %d: %w", pid, err)
}

// defaultPath returns the default pidfile path.
Expand Down
4 changes: 2 additions & 2 deletions pkg/pidfile/pidfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package pidfile

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -244,7 +244,7 @@ func TestOwnerPid(t *testing.T) {
func mkTestDir(t *testing.T) (string, error) {
tmp, err := ioutil.TempDir("", ".pidfile-test*")
if err != nil {
return "", errors.Wrapf(err, "failed to create test directory")
return "", fmt.Errorf("failed to create test directory: %w", err)
}

t.Cleanup(func() {
Expand Down
9 changes: 4 additions & 5 deletions pkg/resmgr/cache/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
corev1 "k8s.io/api/core/v1"

nri "github.com/containerd/nri/pkg/api"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -871,9 +870,9 @@ func setupSysFsDevice(dev string) error {
fi, err := os.Stat(dev)
if err != nil {
if os.IsNotExist(err) {
return errors.Wrapf(err, "no such file %s", dev)
return fmt.Errorf("no such file %s: %w", dev, err)
}
return errors.Wrapf(err, "unable to get stat for %s", dev)
return fmt.Errorf("unable to get stat for %s: %w", dev, err)
}

devType := "block"
Expand All @@ -888,12 +887,12 @@ func setupSysFsDevice(dev string) error {
major := int64(unix.Major(rdev))
minor := int64(unix.Minor(rdev))
if major == 0 {
return errors.Errorf("%s is a virtual device node", dev)
return fmt.Errorf("%s is a virtual device node", dev)
}

err = createSysFsDevice(devType, major, minor)
if err != nil {
return errors.Wrapf(err, "failed to find sysfs device for %s", dev)
return fmt.Errorf("failed to find sysfs device for %s: %w", dev, err)
}

return nil
Expand Down
18 changes: 8 additions & 10 deletions pkg/resmgr/nri.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/containers/nri-plugins/pkg/resmgr/cache"
"github.com/containers/nri-plugins/pkg/resmgr/events"
"github.com/containers/nri-plugins/pkg/resmgr/policy"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"

"github.com/containerd/nri/pkg/api"
Expand Down Expand Up @@ -61,7 +60,7 @@ func (p *nriPlugin) createStub() error {
p.Info("creating plugin stub...")

if p.stub, err = stub.New(p, opts...); err != nil {
return errors.Wrap(err, "failed to create NRI plugin stub")
return fmt.Errorf("failed to create NRI plugin stub: %w", err)
}

return nil
Expand All @@ -79,7 +78,7 @@ func (p *nriPlugin) start() error {
}

if err := p.stub.Start(context.Background()); err != nil {
return errors.Wrap(err, "failed to start NRI plugin")
return fmt.Errorf("failed to start NRI plugin: %w", err)
}

return nil
Expand Down Expand Up @@ -199,8 +198,7 @@ func (p *nriPlugin) Synchronize(ctx context.Context, pods []*api.PodSandbox, con
}

if err := m.policy.Start(allocated, released); err != nil {
return nil, errors.Wrapf(err,
"failed to start policy %s", policy.ActivePolicy())
return nil, fmt.Errorf("failed to start policy %s: %w", policy.ActivePolicy(), err)
}

m.updateTopologyZones()
Expand Down Expand Up @@ -328,13 +326,13 @@ func (p *nriPlugin) CreateContainer(ctx context.Context, podSandbox *api.PodSand

c, err := m.cache.InsertContainer(container)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to cache container")
return nil, nil, fmt.Errorf("failed to cache container: %w", err)
}
c.UpdateState(cache.ContainerStateCreating)

if err := m.policy.AllocateResources(c); err != nil {
c.UpdateState(cache.ContainerStateStale)
return nil, nil, errors.Wrap(err, "failed to allocate resources")
return nil, nil, fmt.Errorf("failed to allocate resources: %w", err)
}
c.UpdateState(cache.ContainerStateCreated)

Expand All @@ -349,7 +347,7 @@ func (p *nriPlugin) CreateContainer(ctx context.Context, podSandbox *api.PodSand
m.Error("%s: failed to run post-allocate hooks for %s: %v",
event, container.GetName(), err)
p.runPostReleaseHooks(event, c)
return nil, nil, errors.Wrap(err, "failed to allocate container resources")
return nil, nil, fmt.Errorf("failed to allocate container resources: %w", err)
}

m.policy.ExportResourceData(c)
Expand Down Expand Up @@ -440,7 +438,7 @@ func (p *nriPlugin) UpdateContainer(ctx context.Context, pod *api.PodSandbox, co
//r := cache.EstimateResourceRequirements(res, c.GetQOSClass())

if err := m.policy.UpdateResources(c); err != nil {
return nil, errors.Wrap(err, "failed to update resources")
return nil, fmt.Errorf("failed to update resources: %w", err)
}

return p.getPendingUpdates(nil), nil
Expand Down Expand Up @@ -473,7 +471,7 @@ func (p *nriPlugin) StopContainer(ctx context.Context, pod *api.PodSandbox, cont
}

if err := m.policy.ReleaseResources(c); err != nil {
return nil, errors.Wrap(err, "failed to release resources")
return nil, fmt.Errorf("failed to release resources: %w", err)
}

c.UpdateState(cache.ContainerStateExited)
Expand Down
5 changes: 1 addition & 4 deletions pkg/topology/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/containers/nri-plugins/pkg/topology

go 1.19

require (
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.1.0
)
require golang.org/x/sys v0.1.0
2 changes: 0 additions & 2 deletions pkg/topology/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25 changes: 12 additions & 13 deletions pkg/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"
"syscall"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -73,11 +72,11 @@ func ResetLogger() {
func getDevicesFromVirtual(realDevPath string) (devs []string, err error) {
relPath, err := filepath.Rel("/sys/devices/virtual", realDevPath)
if err != nil {
return nil, errors.Wrap(err, "unable to find relative path")
return nil, fmt.Errorf("unable to find relative path: %w", err)
}

if strings.HasPrefix(relPath, "..") {
return nil, errors.Errorf("%s is not a virtual device", realDevPath)
return nil, fmt.Errorf("%s is not a virtual device", realDevPath)
}

dir, file := filepath.Split(relPath)
Expand All @@ -86,12 +85,12 @@ func getDevicesFromVirtual(realDevPath string) (devs []string, err error) {
iommuGroup := filepath.Join(sysRoot, "/sys/kernel/iommu_groups", file, "devices")
files, err := os.ReadDir(iommuGroup)
if err != nil {
return nil, errors.Wrapf(err, "failed to read IOMMU group %s", iommuGroup)
return nil, fmt.Errorf("failed to read IOMMU group %s: %w", iommuGroup, err)
}
for _, file := range files {
realDev, err := filepath.EvalSymlinks(filepath.Join(iommuGroup, file.Name()))
if err != nil {
return nil, errors.Wrapf(err, "failed to get real path for %s", file.Name())
return nil, fmt.Errorf("failed to get real path for %s: %w", file.Name(), err)
}
devs = append(devs, realDev)
}
Expand Down Expand Up @@ -160,7 +159,7 @@ func NewTopologyHints(devPath string) (hints Hints, err error) {
hints = make(Hints)
realDevPath, err := filepath.EvalSymlinks(devPath)
if err != nil {
return nil, errors.Wrapf(err, "failed get realpath for %s", devPath)
return nil, fmt.Errorf("failed get realpath for %s: %w", devPath, err)
}
for p := realDevPath; strings.HasPrefix(p, sysRoot+"/sys/devices/"); p = filepath.Dir(p) {
hint, err := getTopologyHint(p)
Expand Down Expand Up @@ -230,7 +229,7 @@ func FindSysFsDevice(dev string) (string, error) {
if os.IsNotExist(err) {
return "", nil
}
return "", errors.Wrapf(err, "unable to get stat for %s", dev)
return "", fmt.Errorf("unable to get stat for %s: %w", dev, err)
}

devType := "block"
Expand All @@ -245,12 +244,12 @@ func FindSysFsDevice(dev string) (string, error) {
major := int64(unix.Major(rdev))
minor := int64(unix.Minor(rdev))
if major == 0 {
return "", errors.Errorf("%s is a virtual device node", dev)
return "", fmt.Errorf("%s is a virtual device node: %w", dev, err)
}

realDevPath, err := findSysFsDevice(devType, major, minor)
if err != nil {
return "", errors.Wrapf(err, "failed to find sysfs device for %s", dev)
return "", fmt.Errorf("failed to find sysfs device for %s: %w", dev, err)
}

return realDevPath, nil
Expand All @@ -271,8 +270,8 @@ func FindGivenSysFsDevice(devType string, major, minor int64) (string, error) {

realDevPath, err := findSysFsDevice(devType, major, minor)
if err != nil {
return "", errors.Wrapf(err, "failed find sysfs device for %s device %d/%d",
devType, major, minor)
return "", fmt.Errorf("failed find sysfs device for %s device %d/%d: %w",
devType, major, minor, err)
}

return realDevPath, nil
Expand All @@ -282,7 +281,7 @@ func findSysFsDevice(devType string, major, minor int64) (string, error) {
devPath := fmt.Sprintf("/sys/dev/%s/%d:%d", devType, major, minor)
realDevPath, err := filepath.EvalSymlinks(devPath)
if err != nil {
return "", errors.Wrapf(err, "failed to get realpath for %s", devPath)
return "", fmt.Errorf("failed to get realpath for %s: %w", devPath, err)
}
return filepath.Join(sysRoot, realDevPath), nil
}
Expand All @@ -295,7 +294,7 @@ func readFilesInDirectory(fileMap map[string]*string, dir string) error {
if os.IsNotExist(err) {
continue
}
return errors.Wrapf(err, "%s: unable to read file %q", dir, k)
return fmt.Errorf("%s: unable to read file %q: %w", dir, k, err)
}
*v = strings.TrimSpace(string(b))
}
Expand Down

0 comments on commit b67041d

Please sign in to comment.