mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Use toml representation to get defaults
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -496,7 +496,7 @@ func TestGetNvidiaConfig(t *testing.T) {
|
||||
getConfig := func() {
|
||||
hookConfig := tc.hookConfig
|
||||
if hookConfig == nil {
|
||||
defaultConfig := getDefaultHookConfig()
|
||||
defaultConfig, _ := getDefaultHookConfig()
|
||||
hookConfig = &defaultConfig
|
||||
}
|
||||
config = getNvidiaConfig(hookConfig, tc.env, nil, tc.privileged)
|
||||
@@ -708,7 +708,7 @@ func TestDeviceListSourcePriority(t *testing.T) {
|
||||
env := map[string]string{
|
||||
envNVVisibleDevices: tc.envvarDevices,
|
||||
}
|
||||
hookConfig := getDefaultHookConfig()
|
||||
hookConfig, _ := getDefaultHookConfig()
|
||||
hookConfig.AcceptEnvvarUnprivileged = tc.acceptUnprivileged
|
||||
hookConfig.AcceptDeviceListAsVolumeMounts = tc.acceptMounts
|
||||
devices = getDevices(&hookConfig, env, tc.mountDevices, tc.privileged)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
@@ -48,8 +49,18 @@ type HookConfig struct {
|
||||
NVIDIAContainerRuntimeHook config.RuntimeHookConfig `toml:"nvidia-container-runtime-hook"`
|
||||
}
|
||||
|
||||
func getDefaultHookConfig() HookConfig {
|
||||
return HookConfig{
|
||||
func getDefaultHookConfig() (HookConfig, error) {
|
||||
rtConfig, err := config.GetDefaultRuntimeConfig()
|
||||
if err != nil {
|
||||
return HookConfig{}, err
|
||||
}
|
||||
|
||||
rtHookConfig, err := config.GetDefaultRuntimeHookConfig()
|
||||
if err != nil {
|
||||
return HookConfig{}, err
|
||||
}
|
||||
|
||||
c := HookConfig{
|
||||
DisableRequire: false,
|
||||
SwarmResource: nil,
|
||||
AcceptEnvvarUnprivileged: true,
|
||||
@@ -67,28 +78,37 @@ func getDefaultHookConfig() HookConfig {
|
||||
User: nil,
|
||||
Ldconfig: nil,
|
||||
},
|
||||
NVIDIAContainerRuntime: *config.GetDefaultRuntimeConfig(),
|
||||
NVIDIAContainerRuntimeHook: *config.GetDefaultRuntimeHookConfig(),
|
||||
NVIDIAContainerRuntime: *rtConfig,
|
||||
NVIDIAContainerRuntimeHook: *rtHookConfig,
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func getHookConfig() (config HookConfig) {
|
||||
func getHookConfig() (*HookConfig, error) {
|
||||
var err error
|
||||
var config HookConfig
|
||||
|
||||
if len(*configflag) > 0 {
|
||||
config = getDefaultHookConfig()
|
||||
config, err = getDefaultHookConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't get default configuration: %v", err)
|
||||
}
|
||||
_, err = toml.DecodeFile(*configflag, &config)
|
||||
if err != nil {
|
||||
log.Panicln("couldn't open configuration file:", err)
|
||||
return nil, fmt.Errorf("couldn't open configuration file: %v", err)
|
||||
}
|
||||
} else {
|
||||
for _, p := range defaultPaths {
|
||||
config = getDefaultHookConfig()
|
||||
config, err = getDefaultHookConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't get default configuration: %v", err)
|
||||
}
|
||||
_, err = toml.DecodeFile(p, &config)
|
||||
if err == nil {
|
||||
break
|
||||
} else if !os.IsNotExist(err) {
|
||||
log.Panicln("couldn't open default configuration file:", err)
|
||||
return nil, fmt.Errorf("couldn't open default configuration file: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,7 +122,7 @@ func getHookConfig() (config HookConfig) {
|
||||
log.Panicf("Invalid value for config option '%v'; %v (supported: %v)\n", configName, config.SupportedDriverCapabilities, allDriverCapabilities)
|
||||
}
|
||||
|
||||
return config
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// getConfigOption returns the toml config option associated with the
|
||||
|
||||
@@ -89,7 +89,8 @@ func TestGetHookConfig(t *testing.T) {
|
||||
|
||||
var config HookConfig
|
||||
getHookConfig := func() {
|
||||
config = getHookConfig()
|
||||
c, _ := getHookConfig()
|
||||
config = *c
|
||||
}
|
||||
|
||||
if tc.expectedPanic {
|
||||
|
||||
@@ -71,14 +71,17 @@ func doPrestart() {
|
||||
defer exit()
|
||||
log.SetFlags(0)
|
||||
|
||||
hook := getHookConfig()
|
||||
hook, err := getHookConfig()
|
||||
if err != nil || hook == nil {
|
||||
log.Panicln("error getting hook config:", err)
|
||||
}
|
||||
cli := hook.NvidiaContainerCLI
|
||||
|
||||
if !hook.NVIDIAContainerRuntimeHook.SkipModeDetection && info.ResolveAutoMode(&logInterceptor{}, hook.NVIDIAContainerRuntime.Mode) != "legacy" {
|
||||
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 (e.g. specify the --runtime=nvidia flag) instead.")
|
||||
}
|
||||
|
||||
container := getContainerConfig(hook)
|
||||
container := getContainerConfig(*hook)
|
||||
nvidia := container.Nvidia
|
||||
if nvidia == nil {
|
||||
// Not a GPU container, nothing to do.
|
||||
|
||||
Reference in New Issue
Block a user