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

View File

@ -17,7 +17,6 @@
package modifier package modifier
import ( import (
"fmt"
"path/filepath" "path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
@ -47,10 +46,19 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error {
return nil return nil
} }
var newPrestart []specs.Hook
for _, hook := range spec.Hooks.Prestart { for _, hook := range spec.Hooks.Prestart {
if isNVIDIAContainerRuntimeHook(&hook) { 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 return nil

View File

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

View File

@ -17,6 +17,7 @@ import (
var ( var (
debugflag = flag.Bool("debug", false, "enable debug output") 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") configflag = flag.String("config", "", "configuration file")
defaultPATH = []string{"/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"} defaultPATH = []string{"/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"}
@ -85,6 +86,10 @@ func doPrestart() {
hook := getHookConfig() hook := getHookConfig()
cli := hook.NvidiaContainerCLI 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) container := getContainerConfig(hook)
nvidia := container.Nvidia nvidia := container.Nvidia
if nvidia == nil { if nvidia == nil {

View File

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

View File

@ -29,7 +29,7 @@ type RuntimeConfig struct {
// getRuntimeConfigFrom reads the nvidia container runtime config from the specified toml Tree. // getRuntimeConfigFrom reads the nvidia container runtime config from the specified toml Tree.
func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig { func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig {
cfg := getDefaultRuntimeConfig() cfg := GetDefaultRuntimeConfig()
if toml == nil { if toml == nil {
return cfg return cfg
@ -42,8 +42,8 @@ func getRuntimeConfigFrom(toml *toml.Tree) *RuntimeConfig {
return cfg return cfg
} }
// getDefaultRuntimeConfig defines the default values for the config // GetDefaultRuntimeConfig defines the default values for the config
func getDefaultRuntimeConfig() *RuntimeConfig { func GetDefaultRuntimeConfig() *RuntimeConfig {
c := RuntimeConfig{ c := RuntimeConfig{
DebugFilePath: "/dev/null", DebugFilePath: "/dev/null",
Experimental: false, 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) d.logger.Debugf("Using NVIDIA Container Runtime Hook path %v", hookPath)
args := []string{hookPath, "prestart"} args := []string{hookPath, "--force", "prestart"}
h := Hook{ h := Hook{
Lifecycle: cdi.PrestartHook, Lifecycle: cdi.PrestartHook,
Path: hookPath, Path: hookPath,