@@ -26,6 +26,7 @@ import (
26
26
27
27
oci "github.com/opencontainers/runtime-spec/specs-go"
28
28
ocigen "github.com/opencontainers/runtime-tools/generate"
29
+ "tags.cncf.io/container-device-interface/pkg/cdi/producer/validator"
29
30
cdi "tags.cncf.io/container-device-interface/specs-go"
30
31
)
31
32
@@ -167,32 +168,7 @@ func (e *ContainerEdits) Validate() error {
167
168
if e == nil || e .ContainerEdits == nil {
168
169
return nil
169
170
}
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 validator .Default .ValidateAny (e .ContainerEdits )
196
172
}
197
173
198
174
// Append other edits into this one. If called with a nil receiver,
@@ -220,71 +196,14 @@ func (e *ContainerEdits) Append(o *ContainerEdits) *ContainerEdits {
220
196
return e
221
197
}
222
198
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
-
260
199
// DeviceNode is a CDI Spec DeviceNode wrapper, used for validating DeviceNodes.
261
200
type DeviceNode struct {
262
201
* cdi.DeviceNode
263
202
}
264
203
265
204
// Validate a CDI Spec DeviceNode.
266
205
func (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 validator .Default .ValidateAny (d .DeviceNode )
288
207
}
289
208
290
209
// Hook is a CDI Spec Hook wrapper, used for validating hooks.
@@ -294,16 +213,7 @@ type Hook struct {
294
213
295
214
// Validate a hook.
296
215
func (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 validator .Default .ValidateAny (h .Hook )
307
217
}
308
218
309
219
// Mount is a CDI Mount wrapper, used for validating mounts.
@@ -313,13 +223,7 @@ type Mount struct {
313
223
314
224
// Validate a mount.
315
225
func (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 validator .Default .ValidateAny (m .Mount )
323
227
}
324
228
325
229
// IntelRdt is a CDI IntelRdt wrapper.
@@ -337,11 +241,7 @@ func ValidateIntelRdt(i *cdi.IntelRdt) error {
337
241
338
242
// Validate validates the IntelRdt configuration.
339
243
func (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 validator .Default .ValidateAny (i .IntelRdt )
345
245
}
346
246
347
247
// Ensure OCI Spec hooks are not nil so we can add hooks.
0 commit comments