Use CUDA.DevicesFromEnvvar to check if modifications are required

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-06-29 17:33:57 +02:00
parent 925c348565
commit 4e08ec2405
3 changed files with 30 additions and 27 deletions

View File

@ -50,10 +50,13 @@ func NewCSVModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec)
return nil, fmt.Errorf("failed to load OCI spec: %v", err) return nil, fmt.Errorf("failed to load OCI spec: %v", err)
} }
// We check whether a modification is required and return a nil modifier if this is not the case. image, err := image.NewCUDAImageFromSpec(rawSpec)
visibleDevices, exists := ociSpec.LookupEnv(visibleDevicesEnvvar) if err != nil {
if !exists || visibleDevices == "" || visibleDevices == visibleDevicesVoid { return nil, err
logger.Infof("No modification required: %v=%v (exists=%v)", visibleDevicesEnvvar, visibleDevices, exists) }
if devices := image.DevicesFromEnvvars(visibleDevicesEnvvar); len(devices) == 0 {
logger.Infof("No modification required; no devices requested")
return nil, nil return nil, nil
} }
logger.Infof("Constructing modifier from config: %+v", *cfg) logger.Infof("Constructing modifier from config: %+v", *cfg)
@ -63,14 +66,7 @@ func NewCSVModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec)
NVIDIAContainerToolkitCLIExecutablePath: cfg.NVIDIACTKConfig.Path, NVIDIAContainerToolkitCLIExecutablePath: cfg.NVIDIACTKConfig.Path,
} }
// TODO: Once the devices have been encapsulated in the CUDA image, this can be moved to before the if err := checkRequirements(logger, image); err != nil {
// visible devices are checked.
image, err := image.NewCUDAImageFromSpec(rawSpec)
if err != nil {
return nil, err
}
if err := checkRequirements(logger, &image); err != nil {
return nil, fmt.Errorf("requirements not met: %v", err) return nil, fmt.Errorf("requirements not met: %v", err)
} }
@ -79,8 +75,7 @@ func NewCSVModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec)
return nil, fmt.Errorf("failed to get list of CSV files: %v", err) return nil, fmt.Errorf("failed to get list of CSV files: %v", err)
} }
nvidiaRequireJetpack, _ := ociSpec.LookupEnv(nvidiaRequireJetpackEnvvar) if nvidiaRequireJetpack, _ := image[nvidiaRequireJetpackEnvvar]; nvidiaRequireJetpack != "csv-mounts=all" {
if nvidiaRequireJetpack != "csv-mounts=all" {
csvFiles = csv.BaseFilesOnly(csvFiles) csvFiles = csv.BaseFilesOnly(csvFiles)
} }
@ -114,7 +109,7 @@ func NewCSVModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec)
return modifiers, nil return modifiers, nil
} }
func checkRequirements(logger *logrus.Logger, image *image.CUDA) error { func checkRequirements(logger *logrus.Logger, image image.CUDA) error {
if image.HasDisableRequire() { if image.HasDisableRequire() {
// TODO: We could print the real value here instead // TODO: We could print the real value here instead
logger.Debugf("NVIDIA_DISABLE_REQUIRE=%v; skipping requirement checks", true) logger.Debugf("NVIDIA_DISABLE_REQUIRE=%v; skipping requirement checks", true)

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -32,19 +33,22 @@ const (
// NewGDSModifier creates the modifiers for GDS devices. // NewGDSModifier creates the modifiers for GDS devices.
// If the spec does not contain the NVIDIA_GDS=enabled environment variable no changes are made. // If the spec does not contain the NVIDIA_GDS=enabled environment variable no changes are made.
func NewGDSModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) { func NewGDSModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) {
_, 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)
} }
// We check whether a modification is required and return a nil modifier if this is not the case. image, err := image.NewCUDAImageFromSpec(rawSpec)
visibleDevices, exists := ociSpec.LookupEnv(visibleDevicesEnvvar) if err != nil {
if !exists || visibleDevices == "" || visibleDevices == visibleDevicesVoid { return nil, err
logger.Infof("No modification required: %v=%v (exists=%v)", visibleDevicesEnvvar, visibleDevices, exists) }
if devices := image.DevicesFromEnvvars(visibleDevicesEnvvar); len(devices) == 0 {
logger.Infof("No modification required; no devices requested")
return nil, nil return nil, nil
} }
if gds, _ := ociSpec.LookupEnv(nvidiaGDSEnvvar); gds != "enabled" { if gds, _ := image[nvidiaGDSEnvvar]; gds != "enabled" {
return nil, nil return nil, nil
} }

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -32,19 +33,22 @@ const (
// NewMOFEDModifier creates the modifiers for MOFED devices. // NewMOFEDModifier creates the modifiers for MOFED devices.
// If the spec does not contain the NVIDIA_MOFED=enabled environment variable no changes are made. // If the spec does not contain the NVIDIA_MOFED=enabled environment variable no changes are made.
func NewMOFEDModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) { func NewMOFEDModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) {
_, 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)
} }
// We check whether a modification is required and return a nil modifier if this is not the case. image, err := image.NewCUDAImageFromSpec(rawSpec)
visibleDevices, exists := ociSpec.LookupEnv(visibleDevicesEnvvar) if err != nil {
if !exists || visibleDevices == "" || visibleDevices == visibleDevicesVoid { return nil, err
logger.Infof("No modification required: %v=%v (exists=%v)", visibleDevicesEnvvar, visibleDevices, exists) }
if devices := image.DevicesFromEnvvars(visibleDevicesEnvvar); len(devices) == 0 {
logger.Infof("No modification required; no devices requested")
return nil, nil return nil, nil
} }
if mofed, _ := ociSpec.LookupEnv(nvidiaMOFEDEnvvar); mofed != "enabled" { if mofed, _ := image[nvidiaMOFEDEnvvar]; mofed != "enabled" {
return nil, nil return nil, nil
} }