Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg,schema,internal: eliminate multierror. #196

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sanity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ on: [push, pull_request]
name: Sanity

env:
GO_VERSION: '1.19.x'
GO_VERSION: '1.20.x'

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion cmd/cdi/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tags.cncf.io/container-device-interface/cmd/cdi

go 1.19
go 1.20

require (
github.com/fsnotify/fsnotify v1.5.1
Expand Down
2 changes: 1 addition & 1 deletion cmd/validate/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tags.cncf.io/container-device-interface/cmd/validate

go 1.19
go 1.20

require tags.cncf.io/container-device-interface v0.0.0

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tags.cncf.io/container-device-interface

go 1.19
go 1.20

require (
github.com/fsnotify/fsnotify v1.5.1
Expand Down
82 changes: 0 additions & 82 deletions internal/multierror/multierror.go

This file was deleted.

38 changes: 0 additions & 38 deletions internal/multierror/multierror_test.go

This file was deleted.

11 changes: 5 additions & 6 deletions internal/validation/k8s/objectmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,27 @@ limitations under the License.
package k8s

import (
"errors"
"fmt"
"strings"

"tags.cncf.io/container-device-interface/internal/multierror"
)

// TotalAnnotationSizeLimitB defines the maximum size of all annotations in characters.
const TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB

// ValidateAnnotations validates that a set of annotations are correctly defined.
func ValidateAnnotations(annotations map[string]string, path string) error {
errors := multierror.New()
errs := []error{}
for k := range annotations {
// The rule is QualifiedName except that case doesn't matter, so convert to lowercase before checking.
for _, msg := range IsQualifiedName(strings.ToLower(k)) {
errors = multierror.Append(errors, fmt.Errorf("%v.%v is invalid: %v", path, k, msg))
errs = append(errs, fmt.Errorf("%v.%v is invalid: %v", path, k, msg))
}
}
if err := ValidateAnnotationsSize(annotations); err != nil {
errors = multierror.Append(errors, fmt.Errorf("%v is too long: %v", path, err))
errs = append(errs, fmt.Errorf("%v is too long: %v", path, err))
}
return errors
return errors.Join(errs...)
}

// ValidateAnnotationsSize validates that a set of annotations is not too large.
Expand Down
17 changes: 9 additions & 8 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/fsnotify/fsnotify"
oci "github.com/opencontainers/runtime-spec/specs-go"
"tags.cncf.io/container-device-interface/internal/multierror"
cdi "tags.cncf.io/container-device-interface/specs-go"
)

Expand Down Expand Up @@ -133,11 +132,11 @@ func (c *Cache) Refresh() error {
}

// collect and return cached errors, much like refresh() does it
var result error
for _, errors := range c.errors {
result = multierror.Append(result, errors...)
errs := []error{}
for _, specErrs := range c.errors {
errs = append(errs, errors.Join(specErrs...))
}
return result
return errors.Join(errs...)
}

// Refresh the Cache by rescanning CDI Spec directories and files.
Expand All @@ -147,12 +146,10 @@ func (c *Cache) refresh() error {
devices = map[string]*Device{}
conflicts = map[string]struct{}{}
specErrors = map[string][]error{}
result []error
)

// collect errors per spec file path and once globally
collectError := func(err error, paths ...string) {
result = append(result, err)
for _, path := range paths {
specErrors[path] = append(specErrors[path], err)
}
Expand Down Expand Up @@ -205,7 +202,11 @@ func (c *Cache) refresh() error {
c.devices = devices
c.errors = specErrors

return multierror.New(result...)
errs := []error{}
for _, specErrs := range specErrors {
errs = append(errs, errors.Join(specErrs...))
}
return errors.Join(errs...)
}

// RefreshIfRequired triggers a refresh if necessary.
Expand Down
13 changes: 9 additions & 4 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -30,7 +31,6 @@ import (
"sigs.k8s.io/yaml"

schema "github.com/xeipuuv/gojsonschema"
"tags.cncf.io/container-device-interface/internal/multierror"
"tags.cncf.io/container-device-interface/internal/validation"
)

Expand Down Expand Up @@ -333,11 +333,16 @@ func (e *Error) Error() string {
return ""
}

var multi error
errs := []error{}
for _, err := range e.Result.Errors() {
multi = multierror.Append(multi, fmt.Errorf("%v", err))
errs = append(errs, fmt.Errorf("%v", err))
}
return multi.Error()

if err := errors.Join(errs...); err != nil {
return fmt.Sprintf("%v", err)
}

return ""
}

var (
Expand Down