Merge branch 'detect-gpus-flag' into 'master'

Detect use of --gpus flag in experimental mode

See merge request nvidia/container-toolkit/container-toolkit!125
This commit is contained in:
Evan Lezar 2022-04-08 11:18:11 +00:00
commit 5211960fc3
7 changed files with 31 additions and 17 deletions

View File

@ -231,7 +231,7 @@ func TestExperimentalModifier(t *testing.T) {
},
},
{
description: "modification fails for existing nvidia-container-runtime-hook",
description: "modification removes existing nvidia-container-runtime-hook",
spec: &specs.Spec{
Hooks: &specs.Hooks{
Prestart: []specs.Hook{
@ -254,20 +254,19 @@ func TestExperimentalModifier(t *testing.T) {
return hooks, nil
},
},
expectedError: fmt.Errorf("nvidia-container-runtime-hook already exists"),
expectedSpec: &specs.Spec{
Hooks: &specs.Hooks{
Prestart: []specs.Hook{
{
Path: "/path/to/nvidia-container-runtime-hook",
Args: []string{"/path/to/nvidia-container-runtime-hook", "prestart"},
Path: "/hook/b",
Args: []string{"/hook/b", "argb"},
},
},
},
},
},
{
description: "modification fails for existing nvidia-container-toolkit",
description: "modification removes existing nvidia-container-toolkit",
spec: &specs.Spec{
Hooks: &specs.Hooks{
Prestart: []specs.Hook{
@ -290,13 +289,12 @@ func TestExperimentalModifier(t *testing.T) {
return hooks, nil
},
},
expectedError: fmt.Errorf("nvidia-container-toolkit already exists"),
expectedSpec: &specs.Spec{
Hooks: &specs.Hooks{
Prestart: []specs.Hook{
{
Path: "/path/to/nvidia-container-toolkit",
Args: []string{"/path/to/nvidia-container-toolkit", "prestart"},
Path: "/hook/b",
Args: []string{"/hook/b", "argb"},
},
},
},

View File

@ -17,7 +17,6 @@
package modifier
import (
"fmt"
"path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
@ -47,10 +46,19 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error {
return nil
}
var newPrestart []specs.Hook
for _, hook := range spec.Hooks.Prestart {
if isNVIDIAContainerRuntimeHook(&hook) {
return fmt.Errorf("spec already contains required 'prestart' hook")
m.logger.Debugf("Removing hook %v", hook)
continue
}
newPrestart = append(newPrestart, hook)
}
if len(newPrestart) != len(spec.Hooks.Prestart) {
m.logger.Debugf("Updating 'prestart' hooks to %v", newPrestart)
spec.Hooks.Prestart = newPrestart
}
return nil

View File

@ -7,6 +7,7 @@ import (
"reflect"
"github.com/BurntSushi/toml"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
)
const (
@ -41,10 +42,11 @@ type HookConfig struct {
AcceptDeviceListAsVolumeMounts bool `toml:"accept-nvidia-visible-devices-as-volume-mounts"`
SupportedDriverCapabilities DriverCapabilities `toml:"supported-driver-capabilities"`
NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"`
NvidiaContainerCLI CLIConfig `toml:"nvidia-container-cli"`
NVIDIAContainerRuntime config.RuntimeConfig `toml:"nvidia-container-runtime"`
}
func getDefaultHookConfig() (config HookConfig) {
func getDefaultHookConfig() HookConfig {
return HookConfig{
DisableRequire: false,
SwarmResource: nil,
@ -63,6 +65,7 @@ func getDefaultHookConfig() (config HookConfig) {
User: nil,
Ldconfig: nil,
},
NVIDIAContainerRuntime: *config.GetDefaultRuntimeConfig(),
}
}

View File

@ -17,6 +17,7 @@ import (
var (
debugflag = flag.Bool("debug", false, "enable debug output")
forceflag = flag.Bool("force", false, "force execution of prestart hook in experimental mode")
configflag = flag.String("config", "", "configuration file")
defaultPATH = []string{"/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"}
@ -85,6 +86,10 @@ func doPrestart() {
hook := getHookConfig()
cli := hook.NvidiaContainerCLI
if hook.NVIDIAContainerRuntime.Experimental && !*forceflag {
log.Panicln("invoking the NVIDIA Container Runtime Hook directly (e.g. specifying the docker --gpus flag) is not supported. Please use the NVIDIA Container Runtime instead.")
}
container := getContainerConfig(hook)
nvidia := container.Nvidia
if nvidia == nil {

View File

@ -103,7 +103,7 @@ func getDefaultConfig() *Config {
c := Config{
NVIDIAContainerCLIConfig: *getDefaultContainerCLIConfig(),
NVIDIACTKConfig: *getDefaultCTKConfig(),
NVIDIAContainerRuntimeConfig: *getDefaultRuntimeConfig(),
NVIDIAContainerRuntimeConfig: *GetDefaultRuntimeConfig(),
}
return &c

View File

@ -29,7 +29,7 @@ type RuntimeConfig struct {
// getRuntimeConfigFrom reads the nvidia container runtime config from the specified toml Tree.
func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig {
cfg := getDefaultRuntimeConfig()
cfg := GetDefaultRuntimeConfig()
if toml == nil {
return cfg
@ -42,8 +42,8 @@ func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig {
return cfg
}
// getDefaultRuntimeConfig defines the default values for the config
func getDefaultRuntimeConfig() *RuntimeConfig {
// GetDefaultRuntimeConfig defines the default values for the config
func GetDefaultRuntimeConfig() *RuntimeConfig {
c := RuntimeConfig{
DebugFilePath: "/dev/null",
Experimental: false,

View File

@ -59,7 +59,7 @@ func (d legacy) Hooks() ([]Hook, error) {
}
d.logger.Debugf("Using NVIDIA Container Runtime Hook path %v", hookPath)
args := []string{hookPath, "prestart"}
args := []string{hookPath, "--force", "prestart"}
h := Hook{
Lifecycle: cdi.PrestartHook,
Path: hookPath,