Add engine.Config to encapsulate writing

This change adds an engine.Config type to encapsulate the writing
of config files for container engines.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar
2023-07-03 15:14:22 +02:00
parent bbe9742c46
commit baf94181aa
8 changed files with 82 additions and 103 deletions

View File

@@ -18,7 +18,6 @@ package crio
import (
"fmt"
"os"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
"github.com/pelletier/go-toml"
@@ -103,34 +102,11 @@ func (c *Config) RemoveRuntime(name string) error {
// Save writes the config to the specified path
func (c Config) Save(path string) (int64, error) {
config := (toml.Tree)(c)
output, err := config.ToTomlString()
output, err := config.Marshal()
if err != nil {
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
}
if path == "" {
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
return int64(len(output)), nil
}
if len(output) == 0 {
err := os.Remove(path)
if err != nil {
return 0, fmt.Errorf("unable to remove empty file: %v", err)
}
return 0, nil
}
f, err := os.Create(path)
if err != nil {
return 0, fmt.Errorf("unable to open '%v' for writing: %v", path, err)
}
defer f.Close()
n, err := f.WriteString(output)
if err != nil {
return 0, fmt.Errorf("unable to write output: %v", err)
}
n, err := engine.Config(path).Write(output)
return int64(n), err
}

View File

@@ -67,13 +67,14 @@ func (b *builder) loadConfig(config string) (*Config, error) {
return nil, fmt.Errorf("config file is a directory")
}
configFile := config
if os.IsNotExist(err) {
configFile = "/dev/null"
b.logger.Infof("Config file does not exist, creating new one")
b.logger.Infof("Config file does not exist; using empty config")
config = "/dev/null"
} else {
b.logger.Infof("Loading config from %v", config)
}
cfg, err := toml.LoadFile(configFile)
cfg, err := toml.LoadFile(config)
if err != nil {
return nil, err
}