Skip to content

Commit d59fc65

Browse files
committed
pkg/cdi: improve CDI device validation, injection.
Accept all OCI supported device types in CDI Spec files. Address a few bugs and omissions in InjectDevices making it more complete / better usable in runtimes: - generate cgroup device access rules - remove conflicting devices/mounts prior to injection - fill in missing device type, major/minor number from host - use container uid/gid if they are unspecified in CDI Spec - sort mounts after injection Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 44b2a85 commit d59fc65

File tree

7 files changed

+650
-32
lines changed

7 files changed

+650
-32
lines changed

pkg/cdi/cache.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ func (c *Cache) Refresh() error {
153153
// returns any unresolvable devices and an error if injection fails for
154154
// any of the devices.
155155
func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, error) {
156-
var (
157-
unresolved []string
158-
specs = map[*Spec]struct{}{}
159-
)
156+
var unresolved []string
160157

161158
if ociSpec == nil {
162159
return devices, errors.Errorf("can't inject devices, nil OCI Spec")
@@ -165,6 +162,9 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
165162
c.Lock()
166163
defer c.Unlock()
167164

165+
edits := &ContainerEdits{}
166+
specs := map[*Spec]struct{}{}
167+
168168
for _, device := range devices {
169169
d := c.devices[device]
170170
if d == nil {
@@ -173,16 +173,20 @@ func (c *Cache) InjectDevices(ociSpec *oci.Spec, devices ...string) ([]string, e
173173
}
174174
if _, ok := specs[d.GetSpec()]; !ok {
175175
specs[d.GetSpec()] = struct{}{}
176-
d.GetSpec().ApplyEdits(ociSpec)
176+
edits.Append(d.GetSpec().edits())
177177
}
178-
d.ApplyEdits(ociSpec)
178+
edits.Append(d.edits())
179179
}
180180

181181
if unresolved != nil {
182182
return unresolved, errors.Errorf("unresolvable CDI devices %s",
183183
strings.Join(devices, ", "))
184184
}
185185

186+
if err := edits.Apply(ociSpec); err != nil {
187+
return nil, errors.Wrap(err, "failed to inject devices")
188+
}
189+
186190
return nil, nil
187191
}
188192

0 commit comments

Comments
 (0)