Skip to content

Commit 4dd97f6

Browse files
committed
Include annotation validation when loading spec
Signed-off-by: Evan Lezar <[email protected]>
1 parent f162724 commit 4dd97f6

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

pkg/cdi/annotations.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"strings"
23-
24-
"github.com/container-orchestrated-devices/container-device-interface/internal/multierror"
25-
"k8s.io/apimachinery/pkg/api/validation"
26-
"k8s.io/apimachinery/pkg/api/validation/field"
27-
"k8s.io/apimachinery/pkg/util/validation/field"
2823
)
2924

3025
const (
@@ -142,16 +137,3 @@ func AnnotationValue(devices []string) (string, error) {
142137

143138
return value, nil
144139
}
145-
146-
// ValidateSpecAnnotations checks whether spec annotations are valid.
147-
func ValidateSpecAnnotations(name string, annotations map[string]string) error {
148-
fldErrorList := validation.ValidateAnnotations(annotations, field.NewPath(name, "annotations"))
149-
if len(fldErrorList) > 0 {
150-
err := multierror.New()
151-
for _, e := range fldErrorList {
152-
err = multierror.Append(err, e)
153-
}
154-
return err
155-
}
156-
return nil
157-
}

pkg/cdi/device.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cdi
1919
import (
2020
"fmt"
2121

22+
"github.com/container-orchestrated-devices/container-device-interface/internal/validation"
2223
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
2324
oci "github.com/opencontainers/runtime-spec/specs-go"
2425
)
@@ -68,6 +69,13 @@ func (d *Device) validate() error {
6869
if err := ValidateDeviceName(d.Name); err != nil {
6970
return err
7071
}
72+
name := d.Name
73+
if d.spec != nil {
74+
name = d.GetQualifiedName()
75+
}
76+
if err := validation.ValidateSpecAnnotations(name, d.Annotations); err != nil {
77+
return err
78+
}
7179
edits := d.edits()
7280
if edits.isEmpty() {
7381
return fmt.Errorf("invalid device, empty device edits")

pkg/cdi/spec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
oci "github.com/opencontainers/runtime-spec/specs-go"
2929
"sigs.k8s.io/yaml"
3030

31+
"github.com/container-orchestrated-devices/container-device-interface/internal/validation"
3132
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3233
)
3334

@@ -219,6 +220,9 @@ func (s *Spec) validate() (map[string]*Device, error) {
219220
if err := ValidateClassName(s.class); err != nil {
220221
return nil, err
221222
}
223+
if err := validation.ValidateSpecAnnotations(s.Kind, s.Annotations); err != nil {
224+
return nil, err
225+
}
222226
if err := s.edits().Validate(); err != nil {
223227
return nil, err
224228
}

0 commit comments

Comments
 (0)