Skip to content

Commit 651c13c

Browse files
committed
Factor out adding device nodes to OCI spec
This change moves adding device nodes to the OCI spec to a new function so as to make future extensions easier to implement. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 35765bd commit 651c13c

1 file changed

Lines changed: 37 additions & 26 deletions

File tree

pkg/cdi/container-edits.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,10 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
8888
for _, d := range e.DeviceNodes {
8989
dn := DeviceNode{d}
9090

91-
err := dn.fillMissingInfo()
91+
err := dn.addToGenerator(&specgen, spec)
9292
if err != nil {
9393
return err
9494
}
95-
dev := dn.toOCI()
96-
if dev.UID == nil && spec.Process != nil {
97-
if uid := spec.Process.User.UID; uid > 0 {
98-
dev.UID = &uid
99-
}
100-
}
101-
if dev.GID == nil && spec.Process != nil {
102-
if gid := spec.Process.User.GID; gid > 0 {
103-
dev.GID = &gid
104-
}
105-
}
106-
107-
specgen.RemoveDevice(dev.Path)
108-
specgen.AddDevice(dev)
109-
110-
if dev.Type == "b" || dev.Type == "c" {
111-
access := d.Permissions
112-
switch access {
113-
case "":
114-
access = "rwm"
115-
case NoPermissions:
116-
access = ""
117-
}
118-
specgen.AddLinuxResourcesDevice(true, dev.Type, &dev.Major, &dev.Minor, access)
119-
}
12095
}
12196

12297
if len(e.NetDevices) > 0 {
@@ -378,6 +353,42 @@ func (d *DeviceNode) Validate() error {
378353
return nil
379354
}
380355

356+
func (d *DeviceNode) addToGenerator(specgen *ocigen.Generator, spec *oci.Spec) error {
357+
err := d.fillMissingInfo()
358+
if err != nil {
359+
return err
360+
}
361+
dev := d.toOCI()
362+
if dev.UID == nil && spec.Process != nil {
363+
if uid := spec.Process.User.UID; uid > 0 {
364+
dev.UID = &uid
365+
}
366+
}
367+
if dev.GID == nil && spec.Process != nil {
368+
if gid := spec.Process.User.GID; gid > 0 {
369+
dev.GID = &gid
370+
}
371+
}
372+
373+
specgen.RemoveDevice(dev.Path)
374+
specgen.AddDevice(dev)
375+
376+
if dev.Type == "b" || dev.Type == "c" {
377+
access := d.Permissions
378+
switch access {
379+
case "":
380+
access = "rwm"
381+
case NoPermissions:
382+
access = ""
383+
}
384+
specgen.AddLinuxResourcesDevice(true, dev.Type, &dev.Major, &dev.Minor, access)
385+
}
386+
return nil
387+
}
388+
389+
}
390+
return nil
391+
}
381392
// Hook is a CDI Spec Hook wrapper, used for validating hooks.
382393
type Hook struct {
383394
*cdi.Hook

0 commit comments

Comments
 (0)