Skip to content

Commit

Permalink
Merge pull request #99 from klihub/fixes/deps/github-pkg-errors
Browse files Browse the repository at this point in the history
cmd, pkg, schema: stop using github.com/pkg/errors.
  • Loading branch information
elezar authored Nov 25, 2022
2 parents 80359a1 + dd57a82 commit e93a674
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 120 deletions.
11 changes: 5 additions & 6 deletions cmd/cdi/cmd/cdi-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
oci "github.com/opencontainers/runtime-spec/specs-go"
gen "github.com/opencontainers/runtime-tools/generate"
"github.com/pkg/errors"
)

func cdiListVendors() {
Expand Down Expand Up @@ -149,8 +148,8 @@ func cdiInjectDevices(format string, ociSpec *oci.Spec, patterns []string) error
for _, glob := range patterns {
match, err := filepath.Match(glob, device)
if err != nil {
return errors.Wrapf(err, "failed to match pattern %q against %q",
glob, device)
return fmt.Errorf("failed to match pattern %q against %q: %w",
glob, device, err)
}
if match {
matches[device] = struct{}{}
Expand All @@ -171,7 +170,7 @@ func cdiInjectDevices(format string, ociSpec *oci.Spec, patterns []string) error
}
}
if err != nil {
return errors.Wrapf(err, "OCI device injection failed")
return fmt.Errorf("OCI device injection failed: %w", err)
}

fmt.Printf("Updated OCI Spec:\n")
Expand All @@ -190,7 +189,7 @@ func cdiResolveDevices(ociSpecFiles ...string) error {
)

if cache, err = cdi.NewCache(); err != nil {
return errors.Wrap(err, "failed to create CDI cache instance")
return fmt.Errorf("failed to create CDI cache instance: %w", err)
}

for _, ociSpecFile := range ociSpecFiles {
Expand All @@ -209,7 +208,7 @@ func cdiResolveDevices(ociSpecFiles ...string) error {
}
}
if err != nil {
return errors.Wrapf(err, "failed to resolve devices for OCI Spec %q", ociSpecFile)
return fmt.Errorf("failed to resolve devices for OCI Spec %q: %w", ociSpecFile, err)
}

format := chooseFormat(injectCfg.output, ociSpecFile)
Expand Down
6 changes: 2 additions & 4 deletions cmd/cdi/cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"io/ioutil"
"os"

"github.com/pkg/errors"

oci "github.com/opencontainers/runtime-spec/specs-go"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -74,12 +72,12 @@ func readOCISpec(path string) (*oci.Spec, error) {
}

if err != nil {
return nil, errors.Wrapf(err, "failed to read OCI Spec (%q)", path)
return nil, fmt.Errorf("failed to read OCI Spec (%q): %w", path, err)
}

spec = &oci.Spec{}
if err = yaml.Unmarshal(data, spec); err != nil {
return nil, errors.Wrapf(err, "failed to parse OCI Spec (%q)", path)
return nil, fmt.Errorf("failed to parse OCI Spec (%q): %w", path, err)
}

return spec, nil
Expand Down
7 changes: 3 additions & 4 deletions cmd/cdi/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/spf13/cobra"

cdi "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
Expand Down Expand Up @@ -137,8 +136,8 @@ func monitorSpecDirs(args ...string) {
func monitorDirectories(dirs ...string) (*fsnotify.Watcher, error) {
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, errors.Wrapf(err, "failed to create directory watch for %s",
strings.Join(dirs, ","))
return nil, fmt.Errorf("failed to create directory watch for %s: %w",
strings.Join(dirs, ","), err)
}

for _, dir := range dirs {
Expand All @@ -148,7 +147,7 @@ func monitorDirectories(dirs ...string) (*fsnotify.Watcher, error) {
}

if err = w.Add(dir); err != nil {
return nil, errors.Wrapf(err, "failed to add %q to fsnotify watch", dir)
return nil, fmt.Errorf("failed to add %q to fsnotify watch: %w", dir, err)
}
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/opencontainers/runc v1.1.2
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.7.0
github.com/xeipuuv/gojsonschema v1.2.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626/go.
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU=
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
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/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
20 changes: 10 additions & 10 deletions pkg/cdi/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package cdi

import (
"errors"
"fmt"
"strings"

"github.com/pkg/errors"
)

const (
Expand All @@ -34,14 +34,14 @@ const (
func UpdateAnnotations(annotations map[string]string, plugin string, deviceID string, devices []string) (map[string]string, error) {
key, err := AnnotationKey(plugin, deviceID)
if err != nil {
return annotations, errors.Wrap(err, "CDI annotation failed")
return annotations, fmt.Errorf("CDI annotation failed: %w", err)
}
if _, ok := annotations[key]; ok {
return annotations, errors.Errorf("CDI annotation failed, key %q used", key)
return annotations, fmt.Errorf("CDI annotation failed, key %q used", key)
}
value, err := AnnotationValue(devices)
if err != nil {
return annotations, errors.Wrap(err, "CDI annotation failed")
return annotations, fmt.Errorf("CDI annotation failed: %w", err)
}

if annotations == nil {
Expand Down Expand Up @@ -70,7 +70,7 @@ func ParseAnnotations(annotations map[string]string) ([]string, []string, error)
}
for _, d := range strings.Split(value, ",") {
if !IsQualifiedName(d) {
return nil, nil, errors.Errorf("invalid CDI device name %q", d)
return nil, nil, fmt.Errorf("invalid CDI device name %q", d)
}
devices = append(devices, d)
}
Expand Down Expand Up @@ -98,11 +98,11 @@ func AnnotationKey(pluginName, deviceID string) (string, error) {
name := pluginName + "_" + strings.ReplaceAll(deviceID, "/", "_")

if len(name) > maxNameLen {
return "", errors.Errorf("invalid plugin+deviceID %q, too long", name)
return "", fmt.Errorf("invalid plugin+deviceID %q, too long", name)
}

if c := rune(name[0]); !isAlphaNumeric(c) {
return "", errors.Errorf("invalid name %q, first '%c' should be alphanumeric",
return "", fmt.Errorf("invalid name %q, first '%c' should be alphanumeric",
name, c)
}
if len(name) > 2 {
Expand All @@ -111,13 +111,13 @@ func AnnotationKey(pluginName, deviceID string) (string, error) {
case isAlphaNumeric(c):
case c == '_' || c == '-' || c == '.':
default:
return "", errors.Errorf("invalid name %q, invalid charcter '%c'",
return "", fmt.Errorf("invalid name %q, invalid charcter '%c'",
name, c)
}
}
}
if c := rune(name[len(name)-1]); !isAlphaNumeric(c) {
return "", errors.Errorf("invalid name %q, last '%c' should be alphanumeric",
return "", fmt.Errorf("invalid name %q, last '%c' should be alphanumeric",
name, c)
}

Expand Down
23 changes: 11 additions & 12 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
package cdi

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"sync"

stderr "errors"

"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
"github.com/fsnotify/fsnotify"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

// Option is an option to change some aspect of default CDI behavior.
Expand Down Expand Up @@ -97,7 +96,7 @@ func (c *Cache) configure(options ...Option) error {

for _, o := range options {
if err = o(c); err != nil {
return errors.Wrapf(err, "failed to apply cache options")
return fmt.Errorf("failed to apply cache options: %w", err)
}
}

Expand Down Expand Up @@ -159,7 +158,7 @@ func (c *Cache) refresh() error {
return false
case devPrio == oldPrio:
devPath, oldPath := devSpec.GetPath(), oldSpec.GetPath()
collectError(errors.Errorf("conflicting device %q (specs %q, %q)",
collectError(fmt.Errorf("conflicting device %q (specs %q, %q)",
name, devPath, oldPath), devPath, oldPath)
conflicts[name] = struct{}{}
}
Expand All @@ -169,7 +168,7 @@ func (c *Cache) refresh() error {
_ = scanSpecDirs(c.specDirs, func(path string, priority int, spec *Spec, err error) error {
path = filepath.Clean(path)
if err != nil {
collectError(errors.Wrapf(err, "failed to load CDI Spec"), path)
collectError(fmt.Errorf("failed to load CDI Spec %w", err), path)
return nil
}

Expand Down Expand Up @@ -219,7 +218,7 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
var unresolved []string

if ociSpec == nil {
return devices, errors.Errorf("can't inject devices, nil OCI Spec")
return devices, fmt.Errorf("can't inject devices, nil OCI Spec")
}

c.Lock()
Expand All @@ -244,12 +243,12 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
}

if unresolved != nil {
return unresolved, errors.Errorf("unresolvable CDI devices %s",
return unresolved, fmt.Errorf("unresolvable CDI devices %s",
strings.Join(devices, ", "))
}

if err := edits.Apply(ociSpec); err != nil {
return nil, errors.Wrap(err, "failed to inject devices")
return nil, fmt.Errorf("failed to inject devices: %w", err)
}

return nil, nil
Expand Down Expand Up @@ -320,7 +319,7 @@ func (c *Cache) RemoveSpec(name string) error {
}

err = os.Remove(path)
if err != nil && stderr.Is(err, fs.ErrNotExist) {
if err != nil && errors.Is(err, fs.ErrNotExist) {
err = nil
}

Expand Down Expand Up @@ -485,7 +484,7 @@ func (w *watch) setup(dirs []string, dirErrors map[string]error) {
w.watcher, err = fsnotify.NewWatcher()
if err != nil {
for _, dir := range dirs {
dirErrors[dir] = errors.Wrap(err, "failed to create watcher")
dirErrors[dir] = fmt.Errorf("failed to create watcher: %w", err)
}
return
}
Expand Down Expand Up @@ -568,7 +567,7 @@ func (w *watch) update(dirErrors map[string]error, removed ...string) bool {
update = true
} else {
w.tracked[dir] = false
dirErrors[dir] = errors.Wrap(err, "failed to monitor for changes")
dirErrors[dir] = fmt.Errorf("failed to monitor for changes: %w", err)
}
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/cdi/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi/validate"
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -703,17 +702,17 @@ devices:
ociSpec := &oci.Spec{}
unresolved, err = cache.InjectDevices(ociSpec, inject)
if err != nil {
err = errors.Wrap(err, "device injection failed")
err = fmt.Errorf("device injection failed: %w", err)
return
}
if unresolved != nil {
err = errors.Errorf("unresolved devices %s", strings.Join(unresolved, ","))
err = fmt.Errorf("unresolved devices %s", strings.Join(unresolved, ","))
return
}

result := ociSpec.Linux.Devices[0].Path
if _, ok := expect[result]; !ok {
err = errors.Errorf("unexpected device path %s", result)
err = fmt.Errorf("unexpected device path %s", result)
return
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/cdi/container-edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package cdi

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/pkg/errors"

"github.com/container-orchestrated-devices/container-device-interface/specs-go"
oci "github.com/opencontainers/runtime-spec/specs-go"
ocigen "github.com/opencontainers/runtime-tools/generate"
Expand Down Expand Up @@ -140,7 +140,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
ensureOCIHooks(spec)
spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, h.ToOCI())
default:
return errors.Errorf("unknown hook name %q", h.HookName)
return fmt.Errorf("unknown hook name %q", h.HookName)
}
}

Expand All @@ -154,7 +154,7 @@ func (e *ContainerEdits) Validate() error {
}

if err := ValidateEnv(e.Env); err != nil {
return errors.Wrap(err, "invalid container edits")
return fmt.Errorf("invalid container edits: %w", err)
}
for _, d := range e.DeviceNodes {
if err := (&DeviceNode{d}).Validate(); err != nil {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (e *ContainerEdits) isEmpty() bool {
func ValidateEnv(env []string) error {
for _, v := range env {
if strings.IndexByte(v, byte('=')) <= 0 {
return errors.Errorf("invalid environment variable %q", v)
return fmt.Errorf("invalid environment variable %q", v)
}
}
return nil
Expand All @@ -234,11 +234,11 @@ func (d *DeviceNode) Validate() error {
return errors.New("invalid (empty) device path")
}
if _, ok := validTypes[d.Type]; !ok {
return errors.Errorf("device %q: invalid type %q", d.Path, d.Type)
return fmt.Errorf("device %q: invalid type %q", d.Path, d.Type)
}
for _, bit := range d.Permissions {
if bit != 'r' && bit != 'w' && bit != 'm' {
return errors.Errorf("device %q: invalid persmissions %q",
return fmt.Errorf("device %q: invalid persmissions %q",
d.Path, d.Permissions)
}
}
Expand All @@ -253,13 +253,13 @@ type Hook struct {
// Validate a hook.
func (h *Hook) Validate() error {
if _, ok := validHookNames[h.HookName]; !ok {
return errors.Errorf("invalid hook name %q", h.HookName)
return fmt.Errorf("invalid hook name %q", h.HookName)
}
if h.Path == "" {
return errors.Errorf("invalid hook %q with empty path", h.HookName)
return fmt.Errorf("invalid hook %q with empty path", h.HookName)
}
if err := ValidateEnv(h.Env); err != nil {
return errors.Wrapf(err, "invalid hook %q", h.HookName)
return fmt.Errorf("invalid hook %q: %w", h.HookName, err)
}
return nil
}
Expand Down
Loading

0 comments on commit e93a674

Please sign in to comment.