mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Refactor Toml config handling
This change refactors the toml config file handlig for runtimes such as containerd or crio. A toml.Loader is introduced that encapsulates loading the required file. This can be extended to allow other mechanisms for loading loading the current config. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -19,10 +19,9 @@ package crio
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
|
||||
)
|
||||
|
||||
// Config represents the cri-o config
|
||||
@@ -39,8 +38,23 @@ func New(opts ...Option) (engine.Interface, error) {
|
||||
for _, opt := range opts {
|
||||
opt(b)
|
||||
}
|
||||
if b.logger == nil {
|
||||
b.logger = logger.New()
|
||||
}
|
||||
if b.configSource == nil {
|
||||
b.configSource = toml.FromFile(b.path)
|
||||
}
|
||||
|
||||
return b.build()
|
||||
tomlConfig, err := b.configSource.Load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Tree: tomlConfig,
|
||||
Logger: b.logger,
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// AddRuntime adds a new runtime to the crio config
|
||||
@@ -115,22 +129,3 @@ func (c *Config) RemoveRuntime(name string) error {
|
||||
*c.Tree = config
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set sets the specified cri-o option.
|
||||
func (c *Config) Set(key string, value interface{}) {
|
||||
config := *c.Tree
|
||||
config.Set(key, value)
|
||||
*c.Tree = config
|
||||
}
|
||||
|
||||
// Save writes the config to the specified path
|
||||
func (c Config) Save(path string) (int64, error) {
|
||||
config := c.Tree
|
||||
output, err := config.Marshal()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
||||
}
|
||||
|
||||
n, err := engine.Config(path).Write(output)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
@@ -19,9 +19,10 @@ package crio
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
testlog "github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
|
||||
)
|
||||
|
||||
func TestAddRuntime(t *testing.T) {
|
||||
@@ -126,20 +127,20 @@ func TestAddRuntime(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
config, err := toml.Load(tc.config)
|
||||
cfg, err := toml.Load(tc.config)
|
||||
require.NoError(t, err)
|
||||
expectedConfig, err := toml.Load(tc.expectedConfig)
|
||||
require.NoError(t, err)
|
||||
|
||||
c := &Config{
|
||||
Logger: logger,
|
||||
Tree: config,
|
||||
Tree: cfg,
|
||||
}
|
||||
|
||||
err = c.AddRuntime("test", "/usr/bin/test", tc.setAsDefault)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.EqualValues(t, expectedConfig.String(), config.String())
|
||||
require.EqualValues(t, expectedConfig.String(), cfg.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,14 @@
|
||||
package crio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
|
||||
)
|
||||
|
||||
type builder struct {
|
||||
logger logger.Interface
|
||||
path string
|
||||
logger logger.Interface
|
||||
configSource toml.Loader
|
||||
path string
|
||||
}
|
||||
|
||||
// Option defines a function that can be used to configure the config builder
|
||||
@@ -47,48 +44,9 @@ func WithPath(path string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *builder) build() (*Config, error) {
|
||||
if b.logger == nil {
|
||||
b.logger = logger.New()
|
||||
// WithConfigSource sets the TOML source for the config.
|
||||
func WithConfigSource(configSource toml.Loader) Option {
|
||||
return func(b *builder) {
|
||||
b.configSource = configSource
|
||||
}
|
||||
if b.path == "" {
|
||||
empty := toml.Tree{}
|
||||
c := Config{
|
||||
Tree: &empty,
|
||||
Logger: b.logger,
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
return b.loadConfig(b.path)
|
||||
}
|
||||
|
||||
// loadConfig loads the cri-o config from disk
|
||||
func (b *builder) loadConfig(config string) (*Config, error) {
|
||||
b.logger.Infof("Loading config: %v", config)
|
||||
|
||||
info, err := os.Stat(config)
|
||||
if os.IsExist(err) && info.IsDir() {
|
||||
return nil, fmt.Errorf("config file is a directory")
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
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(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.logger.Infof("Successfully loaded config")
|
||||
|
||||
c := Config{
|
||||
Tree: cfg,
|
||||
Logger: b.logger,
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user