Browse code

daemon/devices: Turn RegisterGPUDeviceDrivers into func

Change GPU device driver registration from init-time function assignment
to direct function exports.

This removes the init() function and global function variable pattern in
favor of a more explicit approach.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2026/03/05 02:22:18
Showing 3 changed files
... ...
@@ -10,13 +10,10 @@ import (
10 10
 	"github.com/moby/moby/v2/daemon/config"
11 11
 	"github.com/moby/moby/v2/daemon/internal/capabilities"
12 12
 	"github.com/opencontainers/runtime-spec/specs-go"
13
-	"tags.cncf.io/container-device-interface/pkg/cdi"
14 13
 )
15 14
 
16 15
 var deviceDrivers = map[string]*deviceDriver{}
17 16
 
18
-var RegisterGPUDeviceDrivers = func(_ *cdi.Cache) {}
19
-
20 17
 type deviceListing struct {
21 18
 	Devices  []system.DeviceInfo
22 19
 	Warnings []string
... ...
@@ -2,14 +2,10 @@ package daemon
2 2
 
3 3
 import "tags.cncf.io/container-device-interface/pkg/cdi"
4 4
 
5
-func init() {
6
-	RegisterGPUDeviceDrivers = registerGPUDeviceDrivers
7
-}
8
-
9
-// registerGPUDeviceDrivers registers GPU device drivers.
5
+// RegisterGPUDeviceDrivers registers GPU device drivers.
10 6
 // If the cdiCache is provided, it is used to detect presence of CDI specs for AMD GPUs.
11 7
 // For NVIDIA GPUs, presence of CDI specs is detected by checking for the nvidia-cdi-hook binary.
12
-func registerGPUDeviceDrivers(cdiCache *cdi.Cache) {
8
+func RegisterGPUDeviceDrivers(cdiCache *cdi.Cache) {
13 9
 	// Register NVIDIA device drivers.
14 10
 	if nvidiaDrivers := getNVIDIADeviceDrivers(); len(nvidiaDrivers) > 0 {
15 11
 		for name, driver := range nvidiaDrivers {
16 12
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+//go:build !linux
1
+
2
+package daemon
3
+
4
+import "tags.cncf.io/container-device-interface/pkg/cdi"
5
+
6
+// RegisterGPUDeviceDrivers is a no-op on non-Linux platforms.
7
+func RegisterGPUDeviceDrivers(_ *cdi.Cache) {}