Skip to content

Commit eb39b97

Browse files
committed
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>
1 parent 614e469 commit eb39b97

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

cmd/nvidia-container-runtime-hook/hook_config.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"log"
66
"os"
7-
"path"
87
"reflect"
98
"strings"
109

@@ -13,7 +12,6 @@ import (
1312
)
1413

1514
const (
16-
configPath = "/etc/nvidia-container-runtime/config.toml"
1715
driverPath = "/run/nvidia/driver"
1816
)
1917

@@ -25,29 +23,27 @@ type hookConfig struct {
2523

2624
// loadConfig loads the required paths for the hook config.
2725
func loadConfig() (*config.Config, error) {
28-
var configPaths []string
29-
var required bool
30-
if len(*configflag) != 0 {
31-
configPaths = append(configPaths, *configflag)
32-
required = true
33-
} else {
34-
configPaths = append(configPaths, path.Join(driverPath, configPath), configPath)
26+
configFilePath, required := getConfigFilePath()
27+
cfg, err := config.New(
28+
config.WithConfigFile(configFilePath),
29+
config.WithRequired(true),
30+
)
31+
if err == nil {
32+
return cfg.Config()
33+
} else if os.IsNotExist(err) && !required {
34+
return config.GetDefault()
3535
}
36+
return nil, fmt.Errorf("couldn't open required configuration file: %v", err)
37+
}
3638

37-
for _, p := range configPaths {
38-
cfg, err := config.New(
39-
config.WithConfigFile(p),
40-
config.WithRequired(true),
41-
)
42-
if err == nil {
43-
return cfg.Config()
44-
} else if os.IsNotExist(err) && !required {
45-
continue
46-
}
47-
return nil, fmt.Errorf("couldn't open required configuration file: %v", err)
39+
func getConfigFilePath() (string, bool) {
40+
if configFromFlag := *configflag; configFromFlag != "" {
41+
return configFromFlag, true
4842
}
49-
50-
return config.GetDefault()
43+
if configFromEnvvar := os.Getenv(config.FilePathOverrideEnvVar); configFromEnvvar != "" {
44+
return configFromEnvvar, true
45+
}
46+
return config.GetConfigFilePath(), false
5147
}
5248

5349
func getHookConfig() (*hookConfig, error) {

internal/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
)
3232

3333
const (
34+
FilePathOverrideEnvVar = "NVIDIA_CTK_CONFIG_FILE_PATH"
35+
3436
configOverride = "XDG_CONFIG_HOME"
3537
configFilePath = "nvidia-container-runtime/config.toml"
3638

@@ -74,6 +76,9 @@ type Config struct {
7476

7577
// GetConfigFilePath returns the path to the config file for the configured system
7678
func GetConfigFilePath() string {
79+
if configFilePathOverride := os.Getenv(FilePathOverrideEnvVar); configFilePathOverride != "" {
80+
return configFilePathOverride
81+
}
7782
if XDGConfigDir := os.Getenv(configOverride); len(XDGConfigDir) != 0 {
7883
return filepath.Join(XDGConfigDir, configFilePath)
7984
}

0 commit comments

Comments
 (0)