@@ -26,6 +26,8 @@ import (
2626
2727 oci "github.com/opencontainers/runtime-spec/specs-go"
2828 ocigen "github.com/opencontainers/runtime-tools/generate"
29+
30+ "tags.cncf.io/container-device-interface/pkg/cdi/producer/validator"
2931 cdi "tags.cncf.io/container-device-interface/specs-go"
3032)
3133
@@ -44,18 +46,6 @@ const (
4446 PoststopHook = "poststop"
4547)
4648
47- var (
48- // Names of recognized hooks.
49- validHookNames = map [string ]struct {}{
50- PrestartHook : {},
51- CreateRuntimeHook : {},
52- CreateContainerHook : {},
53- StartContainerHook : {},
54- PoststartHook : {},
55- PoststopHook : {},
56- }
57- )
58-
5949// ContainerEdits represent updates to be applied to an OCI Spec.
6050// These updates can be specific to a CDI device, or they can be
6151// specific to a CDI Spec. In the former case these edits should
@@ -167,32 +157,7 @@ func (e *ContainerEdits) Validate() error {
167157 if e == nil || e .ContainerEdits == nil {
168158 return nil
169159 }
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
160+ return validator .Default .ValidateAny (e .ContainerEdits )
196161}
197162
198163// Append other edits into this one. If called with a nil receiver,
@@ -220,71 +185,14 @@ func (e *ContainerEdits) Append(o *ContainerEdits) *ContainerEdits {
220185 return e
221186}
222187
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-
260188// DeviceNode is a CDI Spec DeviceNode wrapper, used for validating DeviceNodes.
261189type DeviceNode struct {
262190 * cdi.DeviceNode
263191}
264192
265193// Validate a CDI Spec DeviceNode.
266194func (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
195+ return validator .Default .ValidateAny (d .DeviceNode )
288196}
289197
290198// Hook is a CDI Spec Hook wrapper, used for validating hooks.
@@ -294,16 +202,7 @@ type Hook struct {
294202
295203// Validate a hook.
296204func (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
205+ return validator .Default .ValidateAny (h .Hook )
307206}
308207
309208// Mount is a CDI Mount wrapper, used for validating mounts.
@@ -313,13 +212,7 @@ type Mount struct {
313212
314213// Validate a mount.
315214func (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
215+ return validator .Default .ValidateAny (m .Mount )
323216}
324217
325218// IntelRdt is a CDI IntelRdt wrapper.
@@ -337,11 +230,7 @@ func ValidateIntelRdt(i *cdi.IntelRdt) error {
337230
338231// Validate validates the IntelRdt configuration.
339232func (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
233+ return validator .Default .ValidateAny (i .IntelRdt )
345234}
346235
347236// Ensure OCI Spec hooks are not nil so we can add hooks.
0 commit comments