Skip to content

Commit

Permalink
Include annotation validation when loading spec
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Feb 9, 2023
1 parent c3a2d1b commit 16d2508
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
18 changes: 0 additions & 18 deletions pkg/cdi/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (
"errors"
"fmt"
"strings"

"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
"k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/api/validation/field"
"k8s.io/apimachinery/pkg/util/validation/field"
)

const (
Expand Down Expand Up @@ -142,16 +137,3 @@ func AnnotationValue(devices []string) (string, error) {

return value, nil
}

// ValidateSpecAnnotations checks whether spec annotations are valid.
func ValidateSpecAnnotations(name string, annotations map[string]string) error {
fldErrorList := validation.ValidateAnnotations(annotations, field.NewPath(name, "annotations"))
if len(fldErrorList) > 0 {
err := multierror.New()
for _, e := range fldErrorList {
err = multierror.Append(err, e)
}
return err
}
return nil
}
8 changes: 8 additions & 0 deletions pkg/cdi/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cdi
import (
"fmt"

"github.com/container-orchestrated-devices/container-device-interface/internal/validation"
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
oci "github.com/opencontainers/runtime-spec/specs-go"
)
Expand Down Expand Up @@ -68,6 +69,13 @@ func (d *Device) validate() error {
if err := ValidateDeviceName(d.Name); err != nil {
return err
}
name := d.Name
if d.spec != nil {
name = d.GetQualifiedName()
}
if err := validation.ValidateSpecAnnotations(name, d.Annotations); err != nil {
return err
}
edits := d.edits()
if edits.isEmpty() {
return fmt.Errorf("invalid device, empty device edits")
Expand Down
4 changes: 4 additions & 0 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
oci "github.com/opencontainers/runtime-spec/specs-go"
"sigs.k8s.io/yaml"

"github.com/container-orchestrated-devices/container-device-interface/internal/validation"
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
)

Expand Down Expand Up @@ -219,6 +220,9 @@ func (s *Spec) validate() (map[string]*Device, error) {
if err := ValidateClassName(s.class); err != nil {
return nil, err
}
if err := validation.ValidateSpecAnnotations(s.Kind, s.Annotations); err != nil {
return nil, err
}
if err := s.edits().Validate(); err != nil {
return nil, err
}
Expand Down

0 comments on commit 16d2508

Please sign in to comment.