Add NVIDIA_CTK_CONFIG_FILE_PATH envvar

This change adds support for explicitly specifying the path
to the config file through an environment variable.
The NVIDIA_CTK_CONFIG_FILE_PATH variable is used for both the nvidia-container-runtime
and nvidia-container-runtime-hook.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-10-30 13:43:49 +01:00
parent 614e469dac
commit eb39b972a5
No known key found for this signature in database
2 changed files with 23 additions and 22 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"reflect" "reflect"
"strings" "strings"
@ -13,7 +12,6 @@ import (
) )
const ( const (
configPath = "/etc/nvidia-container-runtime/config.toml"
driverPath = "/run/nvidia/driver" driverPath = "/run/nvidia/driver"
) )
@ -25,29 +23,27 @@ type hookConfig struct {
// loadConfig loads the required paths for the hook config. // loadConfig loads the required paths for the hook config.
func loadConfig() (*config.Config, error) { func loadConfig() (*config.Config, error) {
var configPaths []string configFilePath, required := getConfigFilePath()
var required bool cfg, err := config.New(
if len(*configflag) != 0 { config.WithConfigFile(configFilePath),
configPaths = append(configPaths, *configflag) config.WithRequired(true),
required = true )
} else { if err == nil {
configPaths = append(configPaths, path.Join(driverPath, configPath), configPath) return cfg.Config()
} else if os.IsNotExist(err) && !required {
return config.GetDefault()
} }
return nil, fmt.Errorf("couldn't open required configuration file: %v", err)
}
for _, p := range configPaths { func getConfigFilePath() (string, bool) {
cfg, err := config.New( if configFromFlag := *configflag; configFromFlag != "" {
config.WithConfigFile(p), return configFromFlag, true
config.WithRequired(true),
)
if err == nil {
return cfg.Config()
} else if os.IsNotExist(err) && !required {
continue
}
return nil, fmt.Errorf("couldn't open required configuration file: %v", err)
} }
if configFromEnvvar := os.Getenv(config.FilePathOverrideEnvVar); configFromEnvvar != "" {
return config.GetDefault() return configFromEnvvar, true
}
return config.GetConfigFilePath(), false
} }
func getHookConfig() (*hookConfig, error) { func getHookConfig() (*hookConfig, error) {

View File

@ -31,6 +31,8 @@ import (
) )
const ( const (
FilePathOverrideEnvVar = "NVIDIA_CTK_CONFIG_FILE_PATH"
configOverride = "XDG_CONFIG_HOME" configOverride = "XDG_CONFIG_HOME"
configFilePath = "nvidia-container-runtime/config.toml" configFilePath = "nvidia-container-runtime/config.toml"
@ -74,6 +76,9 @@ type Config struct {
// GetConfigFilePath returns the path to the config file for the configured system // GetConfigFilePath returns the path to the config file for the configured system
func GetConfigFilePath() string { func GetConfigFilePath() string {
if configFilePathOverride := os.Getenv(FilePathOverrideEnvVar); configFilePathOverride != "" {
return configFilePathOverride
}
if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 { if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 {
return filepath.Join(XDGConfigDir, configFilePath) return filepath.Join(XDGConfigDir, configFilePath)
} }