Split loading config from reader and getting config from toml.Tree

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-03-29 11:52:38 +02:00
parent 239b6d3739
commit 33d9c1dd57
2 changed files with 14 additions and 5 deletions

View File

@ -56,7 +56,7 @@ func GetRuntimeConfig() (*RuntimeConfig, error) {
} }
defer tomlFile.Close() defer tomlFile.Close()
cfg, err := getRuntimeConfigFrom(tomlFile) cfg, err := loadRuntimeConfigFrom(tomlFile)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read config values: %v", err) return nil, fmt.Errorf("failed to read config values: %v", err)
} }
@ -64,20 +64,29 @@ func GetRuntimeConfig() (*RuntimeConfig, error) {
return cfg, nil return cfg, nil
} }
// getRuntimeConfigFrom reads the config from the specified Reader // loadRuntimeConfigFrom reads the config from the specified Reader
func getRuntimeConfigFrom(reader io.Reader) (*RuntimeConfig, error) { func loadRuntimeConfigFrom(reader io.Reader) (*RuntimeConfig, error) {
toml, err := toml.LoadReader(reader) toml, err := toml.LoadReader(reader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return getRuntimeConfigFrom(toml), nil
}
// 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
}
cfg.DebugFilePath = toml.GetDefault("nvidia-container-runtime.debug", cfg.DebugFilePath).(string) cfg.DebugFilePath = toml.GetDefault("nvidia-container-runtime.debug", cfg.DebugFilePath).(string)
cfg.Experimental = toml.GetDefault("nvidia-container-runtime.experimental", cfg.Experimental).(bool) cfg.Experimental = toml.GetDefault("nvidia-container-runtime.experimental", cfg.Experimental).(bool)
cfg.DiscoverMode = toml.GetDefault("nvidia-container-runtime.discover-mode", cfg.DiscoverMode).(string) cfg.DiscoverMode = toml.GetDefault("nvidia-container-runtime.discover-mode", cfg.DiscoverMode).(string)
return cfg, nil return cfg
} }
// getDefaultRuntimeConfig defines the default values for the config // getDefaultRuntimeConfig defines the default values for the config

View File

@ -95,7 +95,7 @@ func TestGerRuntimeConfig(t *testing.T) {
t.Run(tc.description, func(t *testing.T) { t.Run(tc.description, func(t *testing.T) {
reader := strings.NewReader(strings.Join(tc.contents, "\n")) reader := strings.NewReader(strings.Join(tc.contents, "\n"))
cfg, err := getRuntimeConfigFrom(reader) cfg, err := loadRuntimeConfigFrom(reader)
if tc.expectedError != nil { if tc.expectedError != nil {
require.Error(t, err) require.Error(t, err)
} else { } else {