Skip to content

Commit e93a674

Browse files
authored
Merge pull request #99 from klihub/fixes/deps/github-pkg-errors
cmd, pkg, schema: stop using github.com/pkg/errors.
2 parents 80359a1 + dd57a82 commit e93a674

18 files changed

+109
-120
lines changed

cmd/cdi/cmd/cdi-api.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
2626
oci "github.com/opencontainers/runtime-spec/specs-go"
2727
gen "github.com/opencontainers/runtime-tools/generate"
28-
"github.com/pkg/errors"
2928
)
3029

3130
func cdiListVendors() {
@@ -149,8 +148,8 @@ func cdiInjectDevices(format string, ociSpec *oci.Spec, patterns []string) error
149148
for _, glob := range patterns {
150149
match, err := filepath.Match(glob, device)
151150
if err != nil {
152-
return errors.Wrapf(err, "failed to match pattern %q against %q",
153-
glob, device)
151+
return fmt.Errorf("failed to match pattern %q against %q: %w",
152+
glob, device, err)
154153
}
155154
if match {
156155
matches[device] = struct{}{}
@@ -171,7 +170,7 @@ func cdiInjectDevices(format string, ociSpec *oci.Spec, patterns []string) error
171170
}
172171
}
173172
if err != nil {
174-
return errors.Wrapf(err, "OCI device injection failed")
173+
return fmt.Errorf("OCI device injection failed: %w", err)
175174
}
176175

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

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

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

215214
format := chooseFormat(injectCfg.output, ociSpecFile)

cmd/cdi/cmd/inject.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"io/ioutil"
2222
"os"
2323

24-
"github.com/pkg/errors"
25-
2624
oci "github.com/opencontainers/runtime-spec/specs-go"
2725
"sigs.k8s.io/yaml"
2826

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

7674
if err != nil {
77-
return nil, errors.Wrapf(err, "failed to read OCI Spec (%q)", path)
75+
return nil, fmt.Errorf("failed to read OCI Spec (%q): %w", path, err)
7876
}
7977

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

8583
return spec, nil

cmd/cdi/cmd/monitor.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/fsnotify/fsnotify"
26-
"github.com/pkg/errors"
2726
"github.com/spf13/cobra"
2827

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

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

150149
if err = w.Add(dir); err != nil {
151-
return nil, errors.Wrapf(err, "failed to add %q to fsnotify watch", dir)
150+
return nil, fmt.Errorf("failed to add %q to fsnotify watch: %w", dir, err)
152151
}
153152
}
154153

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/opencontainers/runc v1.1.2
88
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb
99
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626
10-
github.com/pkg/errors v0.9.1
1110
github.com/spf13/cobra v1.6.0
1211
github.com/stretchr/testify v1.7.0
1312
github.com/xeipuuv/gojsonschema v1.2.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626/go.
4343
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
4444
github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU=
4545
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
46-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
47-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4846
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4947
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5048
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

pkg/cdi/annotations.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package cdi
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"strings"
21-
22-
"github.com/pkg/errors"
2323
)
2424

2525
const (
@@ -34,14 +34,14 @@ const (
3434
func UpdateAnnotations(annotations map[string]string, plugin string, deviceID string, devices []string) (map[string]string, error) {
3535
key, err := AnnotationKey(plugin, deviceID)
3636
if err != nil {
37-
return annotations, errors.Wrap(err, "CDI annotation failed")
37+
return annotations, fmt.Errorf("CDI annotation failed: %w", err)
3838
}
3939
if _, ok := annotations[key]; ok {
40-
return annotations, errors.Errorf("CDI annotation failed, key %q used", key)
40+
return annotations, fmt.Errorf("CDI annotation failed, key %q used", key)
4141
}
4242
value, err := AnnotationValue(devices)
4343
if err != nil {
44-
return annotations, errors.Wrap(err, "CDI annotation failed")
44+
return annotations, fmt.Errorf("CDI annotation failed: %w", err)
4545
}
4646

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

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

104104
if c := rune(name[0]); !isAlphaNumeric(c) {
105-
return "", errors.Errorf("invalid name %q, first '%c' should be alphanumeric",
105+
return "", fmt.Errorf("invalid name %q, first '%c' should be alphanumeric",
106106
name, c)
107107
}
108108
if len(name) > 2 {
@@ -111,13 +111,13 @@ func AnnotationKey(pluginName, deviceID string) (string, error) {
111111
case isAlphaNumeric(c):
112112
case c == '_' || c == '-' || c == '.':
113113
default:
114-
return "", errors.Errorf("invalid name %q, invalid charcter '%c'",
114+
return "", fmt.Errorf("invalid name %q, invalid charcter '%c'",
115115
name, c)
116116
}
117117
}
118118
}
119119
if c := rune(name[len(name)-1]); !isAlphaNumeric(c) {
120-
return "", errors.Errorf("invalid name %q, last '%c' should be alphanumeric",
120+
return "", fmt.Errorf("invalid name %q, last '%c' should be alphanumeric",
121121
name, c)
122122
}
123123

pkg/cdi/cache.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717
package cdi
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"io/fs"
2123
"os"
2224
"path/filepath"
2325
"sort"
2426
"strings"
2527
"sync"
2628

27-
stderr "errors"
28-
2929
"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
3030
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3131
"github.com/fsnotify/fsnotify"
3232
oci "github.com/opencontainers/runtime-spec/specs-go"
33-
"github.com/pkg/errors"
3433
)
3534

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

9897
for _, o := range options {
9998
if err = o(c); err != nil {
100-
return errors.Wrapf(err, "failed to apply cache options")
99+
return fmt.Errorf("failed to apply cache options: %w", err)
101100
}
102101
}
103102

@@ -159,7 +158,7 @@ func (c *Cache) refresh() error {
159158
return false
160159
case devPrio == oldPrio:
161160
devPath, oldPath := devSpec.GetPath(), oldSpec.GetPath()
162-
collectError(errors.Errorf("conflicting device %q (specs %q, %q)",
161+
collectError(fmt.Errorf("conflicting device %q (specs %q, %q)",
163162
name, devPath, oldPath), devPath, oldPath)
164163
conflicts[name] = struct{}{}
165164
}
@@ -169,7 +168,7 @@ func (c *Cache) refresh() error {
169168
_ = scanSpecDirs(c.specDirs, func(path string, priority int, spec *Spec, err error) error {
170169
path = filepath.Clean(path)
171170
if err != nil {
172-
collectError(errors.Wrapf(err, "failed to load CDI Spec"), path)
171+
collectError(fmt.Errorf("failed to load CDI Spec %w", err), path)
173172
return nil
174173
}
175174

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

221220
if ociSpec == nil {
222-
return devices, errors.Errorf("can't inject devices, nil OCI Spec")
221+
return devices, fmt.Errorf("can't inject devices, nil OCI Spec")
223222
}
224223

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

246245
if unresolved != nil {
247-
return unresolved, errors.Errorf("unresolvable CDI devices %s",
246+
return unresolved, fmt.Errorf("unresolvable CDI devices %s",
248247
strings.Join(devices, ", "))
249248
}
250249

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

255254
return nil, nil
@@ -320,7 +319,7 @@ func (c *Cache) RemoveSpec(name string) error {
320319
}
321320

322321
err = os.Remove(path)
323-
if err != nil && stderr.Is(err, fs.ErrNotExist) {
322+
if err != nil && errors.Is(err, fs.ErrNotExist) {
324323
err = nil
325324
}
326325

@@ -485,7 +484,7 @@ func (w *watch) setup(dirs []string, dirErrors map[string]error) {
485484
w.watcher, err = fsnotify.NewWatcher()
486485
if err != nil {
487486
for _, dir := range dirs {
488-
dirErrors[dir] = errors.Wrap(err, "failed to create watcher")
487+
dirErrors[dir] = fmt.Errorf("failed to create watcher: %w", err)
489488
}
490489
return
491490
}
@@ -568,7 +567,7 @@ func (w *watch) update(dirErrors map[string]error, removed ...string) bool {
568567
update = true
569568
} else {
570569
w.tracked[dir] = false
571-
dirErrors[dir] = errors.Wrap(err, "failed to monitor for changes")
570+
dirErrors[dir] = fmt.Errorf("failed to monitor for changes: %w", err)
572571
}
573572
}
574573

pkg/cdi/cache_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi/validate"
3030
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3131
oci "github.com/opencontainers/runtime-spec/specs-go"
32-
"github.com/pkg/errors"
3332
"github.com/stretchr/testify/require"
3433
"sigs.k8s.io/yaml"
3534
)
@@ -703,17 +702,17 @@ devices:
703702
ociSpec := &oci.Spec{}
704703
unresolved, err = cache.InjectDevices(ociSpec, inject)
705704
if err != nil {
706-
err = errors.Wrap(err, "device injection failed")
705+
err = fmt.Errorf("device injection failed: %w", err)
707706
return
708707
}
709708
if unresolved != nil {
710-
err = errors.Errorf("unresolved devices %s", strings.Join(unresolved, ","))
709+
err = fmt.Errorf("unresolved devices %s", strings.Join(unresolved, ","))
711710
return
712711
}
713712

714713
result := ociSpec.Linux.Devices[0].Path
715714
if _, ok := expect[result]; !ok {
716-
err = errors.Errorf("unexpected device path %s", result)
715+
err = fmt.Errorf("unexpected device path %s", result)
717716
return
718717
}
719718

pkg/cdi/container-edits.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
package cdi
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"os"
2123
"path/filepath"
2224
"sort"
2325
"strings"
2426

25-
"github.com/pkg/errors"
26-
2727
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
2828
oci "github.com/opencontainers/runtime-spec/specs-go"
2929
ocigen "github.com/opencontainers/runtime-tools/generate"
@@ -140,7 +140,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
140140
ensureOCIHooks(spec)
141141
spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, h.ToOCI())
142142
default:
143-
return errors.Errorf("unknown hook name %q", h.HookName)
143+
return fmt.Errorf("unknown hook name %q", h.HookName)
144144
}
145145
}
146146

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

156156
if err := ValidateEnv(e.Env); err != nil {
157-
return errors.Wrap(err, "invalid container edits")
157+
return fmt.Errorf("invalid container edits: %w", err)
158158
}
159159
for _, d := range e.DeviceNodes {
160160
if err := (&DeviceNode{d}).Validate(); err != nil {
@@ -209,7 +209,7 @@ func (e *ContainerEdits) isEmpty() bool {
209209
func ValidateEnv(env []string) error {
210210
for _, v := range env {
211211
if strings.IndexByte(v, byte('=')) <= 0 {
212-
return errors.Errorf("invalid environment variable %q", v)
212+
return fmt.Errorf("invalid environment variable %q", v)
213213
}
214214
}
215215
return nil
@@ -234,11 +234,11 @@ func (d *DeviceNode) Validate() error {
234234
return errors.New("invalid (empty) device path")
235235
}
236236
if _, ok := validTypes[d.Type]; !ok {
237-
return errors.Errorf("device %q: invalid type %q", d.Path, d.Type)
237+
return fmt.Errorf("device %q: invalid type %q", d.Path, d.Type)
238238
}
239239
for _, bit := range d.Permissions {
240240
if bit != 'r' && bit != 'w' && bit != 'm' {
241-
return errors.Errorf("device %q: invalid persmissions %q",
241+
return fmt.Errorf("device %q: invalid persmissions %q",
242242
d.Path, d.Permissions)
243243
}
244244
}
@@ -253,13 +253,13 @@ type Hook struct {
253253
// Validate a hook.
254254
func (h *Hook) Validate() error {
255255
if _, ok := validHookNames[h.HookName]; !ok {
256-
return errors.Errorf("invalid hook name %q", h.HookName)
256+
return fmt.Errorf("invalid hook name %q", h.HookName)
257257
}
258258
if h.Path == "" {
259-
return errors.Errorf("invalid hook %q with empty path", h.HookName)
259+
return fmt.Errorf("invalid hook %q with empty path", h.HookName)
260260
}
261261
if err := ValidateEnv(h.Env); err != nil {
262-
return errors.Wrapf(err, "invalid hook %q", h.HookName)
262+
return fmt.Errorf("invalid hook %q: %w", h.HookName, err)
263263
}
264264
return nil
265265
}

0 commit comments

Comments
 (0)