@@ -26,6 +26,7 @@ import (
2626
2727 oci "github.com/opencontainers/runtime-spec/specs-go"
2828 ocigen "github.com/opencontainers/runtime-tools/generate"
29+ "tags.cncf.io/container-device-interface/pkg/cdi/producer"
2930 cdi "tags.cncf.io/container-device-interface/specs-go"
3031)
3132
@@ -167,32 +168,7 @@ func (e *ContainerEdits) Validate() error {
167168 if e == nil || e .ContainerEdits == nil {
168169 return nil
169170 }
170-
171- if err := ValidateEnv (e .Env ); err != nil {
172- return fmt .Errorf ("invalid container edits: %w" , err )
173- }
174- for _ , d := range e .DeviceNodes {
175- if err := (& DeviceNode {d }).Validate (); err != nil {
176- return err
177- }
178- }
179- for _ , h := range e .Hooks {
180- if err := (& Hook {h }).Validate (); err != nil {
181- return err
182- }
183- }
184- for _ , m := range e .Mounts {
185- if err := (& Mount {m }).Validate (); err != nil {
186- return err
187- }
188- }
189- if e .IntelRdt != nil {
190- if err := (& IntelRdt {e .IntelRdt }).Validate (); err != nil {
191- return err
192- }
193- }
194-
195- return nil
171+ return producer .DefaultValidator .Validate (e .ContainerEdits )
196172}
197173
198174// Append other edits into this one. If called with a nil receiver,
@@ -220,71 +196,14 @@ func (e *ContainerEdits) Append(o *ContainerEdits) *ContainerEdits {
220196 return e
221197}
222198
223- // isEmpty returns true if these edits are empty. This is valid in a
224- // global Spec context but invalid in a Device context.
225- func (e * ContainerEdits ) isEmpty () bool {
226- if e == nil {
227- return false
228- }
229- if len (e .Env ) > 0 {
230- return false
231- }
232- if len (e .DeviceNodes ) > 0 {
233- return false
234- }
235- if len (e .Hooks ) > 0 {
236- return false
237- }
238- if len (e .Mounts ) > 0 {
239- return false
240- }
241- if len (e .AdditionalGIDs ) > 0 {
242- return false
243- }
244- if e .IntelRdt != nil {
245- return false
246- }
247- return true
248- }
249-
250- // ValidateEnv validates the given environment variables.
251- func ValidateEnv (env []string ) error {
252- for _ , v := range env {
253- if strings .IndexByte (v , byte ('=' )) <= 0 {
254- return fmt .Errorf ("invalid environment variable %q" , v )
255- }
256- }
257- return nil
258- }
259-
260199// DeviceNode is a CDI Spec DeviceNode wrapper, used for validating DeviceNodes.
261200type DeviceNode struct {
262201 * cdi.DeviceNode
263202}
264203
265204// Validate a CDI Spec DeviceNode.
266205func (d * DeviceNode ) Validate () error {
267- validTypes := map [string ]struct {}{
268- "" : {},
269- "b" : {},
270- "c" : {},
271- "u" : {},
272- "p" : {},
273- }
274-
275- if d .Path == "" {
276- return errors .New ("invalid (empty) device path" )
277- }
278- if _ , ok := validTypes [d .Type ]; ! ok {
279- return fmt .Errorf ("device %q: invalid type %q" , d .Path , d .Type )
280- }
281- for _ , bit := range d .Permissions {
282- if bit != 'r' && bit != 'w' && bit != 'm' {
283- return fmt .Errorf ("device %q: invalid permissions %q" ,
284- d .Path , d .Permissions )
285- }
286- }
287- return nil
206+ return producer .DefaultValidator .Validate (d .DeviceNode )
288207}
289208
290209// Hook is a CDI Spec Hook wrapper, used for validating hooks.
@@ -294,16 +213,7 @@ type Hook struct {
294213
295214// Validate a hook.
296215func (h * Hook ) Validate () error {
297- if _ , ok := validHookNames [h .HookName ]; ! ok {
298- return fmt .Errorf ("invalid hook name %q" , h .HookName )
299- }
300- if h .Path == "" {
301- return fmt .Errorf ("invalid hook %q with empty path" , h .HookName )
302- }
303- if err := ValidateEnv (h .Env ); err != nil {
304- return fmt .Errorf ("invalid hook %q: %w" , h .HookName , err )
305- }
306- return nil
216+ return producer .DefaultValidator .Validate (h .Hook )
307217}
308218
309219// Mount is a CDI Mount wrapper, used for validating mounts.
@@ -313,13 +223,7 @@ type Mount struct {
313223
314224// Validate a mount.
315225func (m * Mount ) Validate () error {
316- if m .HostPath == "" {
317- return errors .New ("invalid mount, empty host path" )
318- }
319- if m .ContainerPath == "" {
320- return errors .New ("invalid mount, empty container path" )
321- }
322- return nil
226+ return producer .DefaultValidator .Validate (m .Mount )
323227}
324228
325229// IntelRdt is a CDI IntelRdt wrapper.
@@ -337,11 +241,7 @@ func ValidateIntelRdt(i *cdi.IntelRdt) error {
337241
338242// Validate validates the IntelRdt configuration.
339243func (i * IntelRdt ) Validate () error {
340- // ClosID must be a valid Linux filename
341- if len (i .ClosID ) >= 4096 || i .ClosID == "." || i .ClosID == ".." || strings .ContainsAny (i .ClosID , "/\n " ) {
342- return errors .New ("invalid ClosID" )
343- }
344- return nil
244+ return producer .DefaultValidator .Validate (i .IntelRdt )
345245}
346246
347247// Ensure OCI Spec hooks are not nil so we can add hooks.
0 commit comments