mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-22 08:18:32 +00:00
Add support for specifying devices in annotations
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
404e266222
commit
8817dee66c
@ -37,6 +37,24 @@ type cdiModifier struct {
|
|||||||
// CDI specifications available on the system. The NVIDIA_VISIBLE_DEVICES enviroment variable is
|
// CDI specifications available on the system. The NVIDIA_VISIBLE_DEVICES enviroment variable is
|
||||||
// used to select the devices to include.
|
// used to select the devices to include.
|
||||||
func NewCDIModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) {
|
func NewCDIModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) {
|
||||||
|
devices, err := getDevicesFromSpec(ociSpec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get required devices from OCI specification: %v", err)
|
||||||
|
}
|
||||||
|
if len(devices) == 0 {
|
||||||
|
logger.Debugf("No devices requested; no modification required.")
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
m := cdiModifier{
|
||||||
|
logger: logger,
|
||||||
|
devices: devices,
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDevicesFromSpec(ociSpec oci.Spec) ([]string, error) {
|
||||||
rawSpec, err := ociSpec.Load()
|
rawSpec, err := ociSpec.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load OCI spec: %v", err)
|
return nil, fmt.Errorf("failed to load OCI spec: %v", err)
|
||||||
@ -47,26 +65,27 @@ func NewCDIModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
devices := image.DevicesFromEnvvars(visibleDevicesEnvvar)
|
envDevices := image.DevicesFromEnvvars(visibleDevicesEnvvar)
|
||||||
if len(devices) == 0 {
|
|
||||||
logger.Debugf("No modification required; no devices requested")
|
_, annotationDevices, err := cdi.ParseAnnotations(rawSpec.Annotations)
|
||||||
return nil, nil
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse container annotations: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var qualifiedDevices []string
|
uniqueDevices := make(map[string]struct{})
|
||||||
for _, name := range devices {
|
for _, name := range append(envDevices, annotationDevices...) {
|
||||||
if !cdi.IsQualifiedName(name) {
|
if !cdi.IsQualifiedName(name) {
|
||||||
name = cdi.QualifiedName("nvidia.com", "gpu", name)
|
name = cdi.QualifiedName("nvidia.com", "gpu", name)
|
||||||
}
|
}
|
||||||
qualifiedDevices = append(qualifiedDevices, name)
|
uniqueDevices[name] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
m := cdiModifier{
|
var devices []string
|
||||||
logger: logger,
|
for name := range uniqueDevices {
|
||||||
devices: qualifiedDevices,
|
devices = append(devices, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return devices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modify loads the CDI registry and injects the specified CDI devices into the OCI runtime specification.
|
// Modify loads the CDI registry and injects the specified CDI devices into the OCI runtime specification.
|
||||||
@ -91,6 +110,7 @@ func (m cdiModifier) Modify(spec *specs.Spec) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.logger.Debugf("Injecting devices using CDI: %v", devices)
|
||||||
_, err := registry.InjectDevices(spec, devices...)
|
_, err := registry.InjectDevices(spec, devices...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to inject CDI devices: %v", err)
|
return fmt.Errorf("failed to inject CDI devices: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user