Skip to content

Commit 4a121aa

Browse files
Merge pull request #9295 from klihub/fixes/release-1.33/delay-cdi-device-injection
[release-1.33] server,factory/container: delay CDI device injection later.
2 parents 5b19bdb + db21d46 commit 4a121aa

File tree

7 files changed

+29
-6
lines changed

7 files changed

+29
-6
lines changed

internal/factory/container/container.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ type Container interface {
111111
// SpecAddDevices adds devices from the server config, and container CRI config
112112
SpecAddDevices([]device.Device, []device.Device, bool, bool) error
113113

114+
// SpecInjectCDIDevices injects any requested CDI devices to the container's Spec.
115+
SpecInjectCDIDevices() error
116+
114117
// AddUnifiedResourcesFromAnnotations adds the cgroup-v2 resources specified in the io.kubernetes.cri-o.UnifiedCgroup annotation
115118
AddUnifiedResourcesFromAnnotations(annotationsMap map[string]string) error
116119

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package container
22

33
import (
4+
"fmt"
5+
"runtime"
6+
47
devicecfg "github.com/cri-o/cri-o/internal/config/device"
58
)
69

710
func (c *container) SpecAddDevices(configuredDevices, annotationDevices []devicecfg.Device, privilegedWithoutHostDevices, enableDeviceOwnershipFromSecurityContext bool) error {
811
return nil
912
}
13+
14+
func (c *container) SpecInjectCDIDevices() error {
15+
if len(c.Config().CDIDevices) > 0 {
16+
return fmt.Errorf("(*container).SpecInjectCDIDevices not supported on %s", runtime.GOOS)
17+
}
18+
return nil
19+
}

internal/factory/container/device_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ func (c *container) SpecAddDevices(configuredDevices, annotationDevices []device
4949
return err
5050
}
5151

52-
// Finally, inject CDI devices
53-
return c.specInjectCDIDevices()
52+
return nil
5453
}
5554

5655
func (c *container) specAddHostDevicesIfPrivileged(privilegedWithoutHostDevices bool) error {
@@ -185,7 +184,7 @@ func (c *container) specAddContainerConfigDevices(enableDeviceOwnershipFromSecur
185184
return nil
186185
}
187186

188-
func (c *container) specInjectCDIDevices() error {
187+
func (c *container) SpecInjectCDIDevices() error {
189188
var (
190189
cdiDevices = c.Config().CDIDevices
191190
fromCRI = map[string]struct{}{}

internal/factory/container/device_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ var _ = t.Describe("Container", func() {
186186
}
187187
})
188188

189-
t.Describe("SpecAdd(CDI)Devices", func() {
189+
t.Describe("SpecInjectCDIDevices", func() {
190190
writeCDISpecFiles := func(content []string) error {
191191
if len(content) == 0 {
192192
return nil
@@ -421,7 +421,7 @@ containerEdits:
421421
Expect(writeCDISpecFiles(test.cdiSpecFiles)).To(Succeed())
422422

423423
// When
424-
err := sut.SpecAddDevices(nil, nil, false, false)
424+
err := sut.SpecInjectCDIDevices()
425425

426426
// Then
427427
Expect(err != nil).To(Equal(test.expectError))

internal/factory/container/device_unsupported.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ import (
1212
func (c *container) SpecAddDevices(configuredDevices, annotationDevices []devicecfg.Device, privilegedWithoutHostDevices, enableDeviceOwnershipFromSecurityContext bool) error {
1313
return fmt.Errorf("(*container).SpecAddDevices not supported on %s", runtime.GOOS)
1414
}
15+
16+
func (c *container) SpecInjectCDIDevices() error {
17+
if len(c.Config().CDIDevices) > 0 {
18+
return fmt.Errorf("(*container).SpecInjectCDIDevices not supported on %s", runtime.GOOS)
19+
}
20+
return nil
21+
}

server/container_create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,10 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr container.Conta
11991199
}
12001200
}
12011201

1202+
if err := ctr.SpecInjectCDIDevices(); err != nil {
1203+
return nil, err
1204+
}
1205+
12021206
// Set up pids limit if pids cgroup is mounted
12031207
if node.CgroupHasPid() {
12041208
specgen.SetLinuxResourcesPidsLimit(s.config.PidsLimit)

test/cdi.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function annotate_ctr_with_unknown_cdidev {
111111
}
112112

113113
function prepare_ctr_with_cdidev {
114-
jq ".CDI_Devices |= . + [ { \"Name\": \"vendor0.com/device=loop8\" }, { \"Name\": \"vendor0.com/device=loop9\" } ]" \
114+
jq ".CDI_Devices |= . + [ { \"Name\": \"vendor0.com/device=loop8\" }, { \"Name\": \"vendor0.com/device=loop9\" } ] | .envs |= . + [ { \"key\": \"VENDOR0\", \"value\": \"unset\" }, { \"key\": \"LOOP8\", \"value\": \"unset\" } ]" \
115115
"$TESTDATA/container_sleep.json" > "$ctr_config"
116116
}
117117

0 commit comments

Comments
 (0)