Repository: moby/moby
File: daemon/devices_nvidia_linux.go (Go)

Code before:
	for _, deviceID := range getRequestedDevicesIDs(dev.req) {
		cdiDeviceIDs = append(cdiDeviceIDs, i.normalizeDeviceID(deviceID))
	}
	if len(cdiDeviceIDs) == 0 {
		return nil
	}

	// If the cdi device driver is not available then we return an error.
	cdiDeviceDriver := deviceDrivers["cdi"]
	if cdiDeviceDriver == nil {
		return fmt.Errorf("no CDI device driver registered: %w", incompatibleDeviceRequest{dev.req.Driver, dev.req.Capabilities})
	}

	// We construct a device instance using the CDI device IDs and forward this
	// to the cdiDeviceDriver.
	cdiRequest := dev.req
	cdiRequest.Count = 0
	cdiRequest.Options = nil
	cdiRequest.DeviceIDs = cdiDeviceIDs

	cdiDeviceInstance := deviceInstance{
		req:          cdiRequest,
		selectedCaps: nil,
	}

	return cdiDeviceDriver.updateSpec(s, &cdiDeviceInstance)
}

// normalizeDeviceID ensures that the specified deviceID is a fully-qualified
// CDI device name.
// If the deviceID is already a fully-qualified CDI device name it is returned
// as-is, otherwise, the defailt CDI device kind (vendor/class) is used to
// construct a fully qualified CDI device name.
func (i *cdiDeviceInjector) normalizeDeviceID(deviceID string) string {
	// TODO: We should ideally use the parser from the tags.cncf.io/cdi packages.
	parts := strings.SplitN(deviceID, "=", 2)
	if len(parts) == 2 {
		return deviceID
	}

	return i.defaultCDIDeviceKind + "=" + deviceID
}


Review comment (suggestion):
Looking at the `container.DeviceRequest` struct, I _think_ the only thing we're preserving from the `dev.req` is the `Driver` field and the `Capabilities`; Wondering if it'd be more clear to construct a fresh request here (but not sure if that's correct; just from glancing over);

```go
	return cdiDeviceDriver.updateSpec(s, &deviceInstance{
		req: container.DeviceRequest{
			Driver:       dev.req.Driver,
			DeviceIDs:    cdiDeviceIDs,
			Capabilities: dev.req.Capabilities,
		},
		selectedCaps: nil,
	})
```

I don't think there's a need to de-reference the Capabilities? If there is, then perhaps a `slices.Clone` (but not sure that really helps as it's a slice of slices);

```go
	return cdiDeviceDriver.updateSpec(s, &deviceInstance{
		req: container.DeviceRequest{
			Driver:       dev.req.Driver,
			DeviceIDs:    cdiDeviceIDs,
			Capabilities: slices.Clone(dev.req.Capabilities),
		},
		selectedCaps: nil,
	})
```


Code after:
	}

	var cdiDeviceIDs []string
	for _, deviceID := range deviceIDs {
		cdiDeviceIDs = append(cdiDeviceIDs, i.normalizeDeviceID(deviceID))
	}

	// We construct a device instance using the CDI device IDs and forward this
	// to the cdiDeviceDriver.
	return cdiDeviceDriver.updateSpec(s, &deviceInstance{
		req: container.DeviceRequest{
			Driver:       dev.req.Driver,
			DeviceIDs:    cdiDeviceIDs,
			Capabilities: dev.req.Capabilities,
		},
		selectedCaps: nil,
	})
}

// normalizeDeviceID ensures that the specified deviceID is a fully-qualified
// CDI device name.
// If the deviceID is already a fully-qualified CDI device name it is returned
// as-is, otherwise, the default CDI device kind (vendor/class) is used to
// construct a fully qualified CDI device name.
func (i *cdiDeviceInjector) normalizeDeviceID(deviceID string) string {
	// if deviceID is of the form vendor.com/class=name, we return it as-is.
	// TODO: We should ideally use the parser from the tags.cncf.io/cdi packages.
	if _, _, ok := strings.Cut(deviceID, "="); ok {
		return deviceID
	}

	return i.defaultCDIDeviceKind + "=" + deviceID
}