Use toml unmarshal to read runtime config

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-05-09 13:56:26 +02:00
parent ae57a2fc93
commit ba0e606df2
2 changed files with 30 additions and 25 deletions

View File

@ -80,22 +80,26 @@ func loadConfigFrom(reader io.Reader) (*Config, error) {
return nil, err return nil, err
} }
return getConfigFrom(toml), nil return getConfigFrom(toml)
} }
// getConfigFrom reads the nvidia container runtime config from the specified toml Tree. // getConfigFrom reads the nvidia container runtime config from the specified toml Tree.
func getConfigFrom(toml *toml.Tree) *Config { func getConfigFrom(toml *toml.Tree) (*Config, error) {
cfg := getDefaultConfig() cfg := getDefaultConfig()
if toml == nil { if toml == nil {
return cfg return cfg, nil
} }
cfg.NVIDIAContainerCLIConfig = *getContainerCLIConfigFrom(toml) cfg.NVIDIAContainerCLIConfig = *getContainerCLIConfigFrom(toml)
cfg.NVIDIACTKConfig = *getCTKConfigFrom(toml) cfg.NVIDIACTKConfig = *getCTKConfigFrom(toml)
cfg.NVIDIAContainerRuntimeConfig = *getRuntimeConfigFrom(toml) runtimeConfig, err := getRuntimeConfigFrom(toml)
if err != nil {
return nil, fmt.Errorf("failed to load nvidia-container-runtime config: %v", err)
}
cfg.NVIDIAContainerRuntimeConfig = *runtimeConfig
return cfg return cfg, nil
} }
// getDefaultConfig defines the default values for the config // getDefaultConfig defines the default values for the config

View File

@ -17,6 +17,8 @@
package config package config
import ( import (
"fmt"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -28,38 +30,37 @@ const (
// RuntimeConfig stores the config options for the NVIDIA Container Runtime // RuntimeConfig stores the config options for the NVIDIA Container Runtime
type RuntimeConfig struct { type RuntimeConfig struct {
DebugFilePath string DebugFilePath string `toml:"debug"`
Experimental bool Experimental bool `toml:"experimental"`
DiscoverMode string DiscoverMode string `toml:"discover-mode"`
// LogLevel defines the logging level for the application // LogLevel defines the logging level for the application
LogLevel string LogLevel string `toml:"log-level"`
// Runtimes defines the candidates for the low-level runtime // Runtimes defines the candidates for the low-level runtime
Runtimes []string Runtimes []string `toml:"runtimes"`
}
// dummy allows us to unmarshal only a RuntimeConfig from a *toml.Tree
type dummy struct {
Runtime RuntimeConfig `toml:"nvidia-container-runtime"`
} }
// 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, error) {
cfg := GetDefaultRuntimeConfig() cfg := GetDefaultRuntimeConfig()
if toml == nil { if toml == nil {
return cfg return cfg, nil
} }
cfg.DebugFilePath = toml.GetDefault("nvidia-container-runtime.debug", cfg.DebugFilePath).(string) d := dummy{
cfg.Experimental = toml.GetDefault("nvidia-container-runtime.experimental", cfg.Experimental).(bool) Runtime: *cfg,
cfg.DiscoverMode = toml.GetDefault("nvidia-container-runtime.discover-mode", cfg.DiscoverMode).(string)
cfg.LogLevel = toml.GetDefault("nvidia-container-runtime.log-level", cfg.LogLevel).(string)
configRuntimes := toml.Get("nvidia-container-runtime.runtimes")
if configRuntimes != nil {
var runtimes []string
for _, r := range configRuntimes.([]interface{}) {
runtimes = append(runtimes, r.(string))
}
cfg.Runtimes = runtimes
} }
return cfg if err := toml.Unmarshal(&d); err != nil {
return nil, fmt.Errorf("failed to unmarshal runtime config: %v", err)
}
return &d.Runtime, nil
} }
// GetDefaultRuntimeConfig defines the default values for the config // GetDefaultRuntimeConfig defines the default values for the config