mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Fix generation of default config
This change ensures that the nvidia-ctk config default command generates a config file that is compatible with the official documentation to, for example, disable cgroups in the NVIDIA Container CLI. This requires that whitespace around comments is stripped before outputing the contets. This also adds an option to load a config and modify it in-place instead. This can be triggered as a post-install step, for example. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -74,13 +74,22 @@ func GetConfig() (*Config, error) {
|
||||
|
||||
configFilePath := path.Join(configDir, configFilePath)
|
||||
|
||||
return Load(configFilePath)
|
||||
}
|
||||
|
||||
// Load loads the config from the specified file path.
|
||||
func Load(configFilePath string) (*Config, error) {
|
||||
if configFilePath == "" {
|
||||
return getDefault()
|
||||
}
|
||||
|
||||
tomlFile, err := os.Open(configFilePath)
|
||||
if err != nil {
|
||||
return getDefaultConfig()
|
||||
return getDefault()
|
||||
}
|
||||
defer tomlFile.Close()
|
||||
|
||||
cfg, err := loadConfigFrom(tomlFile)
|
||||
cfg, err := LoadFrom(tomlFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config values: %v", err)
|
||||
}
|
||||
@@ -88,21 +97,28 @@ func GetConfig() (*Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// loadRuntimeConfigFrom reads the config from the specified Reader
|
||||
func loadConfigFrom(reader io.Reader) (*Config, error) {
|
||||
toml, err := toml.LoadReader(reader)
|
||||
// LoadFrom reads the config from the specified Reader
|
||||
func LoadFrom(reader io.Reader) (*Config, error) {
|
||||
var tree *toml.Tree
|
||||
if reader != nil {
|
||||
toml, err := toml.LoadReader(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tree = toml
|
||||
}
|
||||
|
||||
return getFromTree(tree)
|
||||
}
|
||||
|
||||
// getFromTree reads the nvidia container runtime config from the specified toml Tree.
|
||||
func getFromTree(toml *toml.Tree) (*Config, error) {
|
||||
cfg, err := getDefault()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getConfigFrom(toml)
|
||||
}
|
||||
|
||||
// getConfigFrom reads the nvidia container runtime config from the specified toml Tree.
|
||||
func getConfigFrom(toml *toml.Tree) (*Config, error) {
|
||||
cfg, err := getDefaultConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if toml == nil {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
if err := toml.Unmarshal(cfg); err != nil {
|
||||
@@ -112,9 +128,9 @@ func getConfigFrom(toml *toml.Tree) (*Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// getDefaultConfig defines the default values for the config
|
||||
func getDefaultConfig() (*Config, error) {
|
||||
tomlConfig, err := GetDefaultConfigToml()
|
||||
// getDefault defines the default values for the config
|
||||
func getDefault() (*Config, error) {
|
||||
tomlConfig, err := GetDefaultToml()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -149,8 +165,8 @@ func getDefaultConfig() (*Config, error) {
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
// GetDefaultConfigToml returns the default config as a toml Tree.
|
||||
func GetDefaultConfigToml() (*toml.Tree, error) {
|
||||
// GetDefaultToml returns the default config as a toml Tree.
|
||||
func GetDefaultToml() (*toml.Tree, error) {
|
||||
tree, err := toml.TreeFromMap(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -192,7 +192,7 @@ func TestGetConfig(t *testing.T) {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
reader := strings.NewReader(strings.Join(tc.contents, "\n"))
|
||||
|
||||
cfg, err := loadConfigFrom(reader)
|
||||
cfg, err := LoadFrom(reader)
|
||||
if tc.expectedError != nil {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
|
||||
@@ -27,7 +27,7 @@ type RuntimeHookConfig struct {
|
||||
|
||||
// GetDefaultRuntimeHookConfig defines the default values for the config
|
||||
func GetDefaultRuntimeHookConfig() (*RuntimeHookConfig, error) {
|
||||
cfg, err := getDefaultConfig()
|
||||
cfg, err := getDefault()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ type csvModeConfig struct {
|
||||
|
||||
// GetDefaultRuntimeConfig defines the default values for the config
|
||||
func GetDefaultRuntimeConfig() (*RuntimeConfig, error) {
|
||||
cfg, err := getDefaultConfig()
|
||||
cfg, err := getDefault()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user