From f1791c95f60159d14e6c546ba36d51840613b737 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Thu, 8 Aug 2024 16:27:07 +0200 Subject: [PATCH] Refactor Toml config handling Signed-off-by: Evan Lezar --- .../engine/containerd/config_v1_test.go | 11 +- pkg/config/engine/containerd/config_v2.go | 14 --- .../engine/containerd/config_v2_test.go | 11 +- pkg/config/engine/containerd/containerd.go | 61 +++++++++- pkg/config/engine/containerd/option.go | 94 ++------------ pkg/config/engine/crio/crio.go | 39 +++--- pkg/config/engine/crio/crio_test.go | 11 +- pkg/config/engine/crio/option.go | 58 ++------- pkg/config/engine/docker/docker.go | 3 +- pkg/config/source.go | 115 ++++++++++++++++++ pkg/config/{engine/config.go => toml.go} | 67 +++++++++- tools/container/containerd/config_v1_test.go | 23 ++-- tools/container/containerd/config_v2_test.go | 25 ++-- tools/container/containerd/containerd.go | 2 + 14 files changed, 315 insertions(+), 219 deletions(-) create mode 100644 pkg/config/source.go rename pkg/config/{engine/config.go => toml.go} (50%) diff --git a/pkg/config/engine/containerd/config_v1_test.go b/pkg/config/engine/containerd/config_v1_test.go index 774500d51..f1563116d 100644 --- a/pkg/config/engine/containerd/config_v1_test.go +++ b/pkg/config/engine/containerd/config_v1_test.go @@ -19,9 +19,10 @@ package containerd 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" ) func TestAddRuntimeV1(t *testing.T) { @@ -226,20 +227,20 @@ func TestAddRuntimeV1(t *testing.T) { for _, tc := range testCases { t.Run(tc.description, func(t *testing.T) { - config, err := toml.Load(tc.config) + cfg, err := config.Load(tc.config) require.NoError(t, err) - expectedConfig, err := toml.Load(tc.expectedConfig) + expectedConfig, err := config.Load(tc.expectedConfig) require.NoError(t, err) c := &ConfigV1{ Logger: logger, - Tree: config, + Toml: cfg, } err = c.AddRuntime("test", "/usr/bin/test", tc.setAsDefault, tc.configOverrides...) require.NoError(t, err) - require.EqualValues(t, expectedConfig.String(), config.String()) + require.EqualValues(t, expectedConfig.String(), cfg.String()) }) } } diff --git a/pkg/config/engine/containerd/config_v2.go b/pkg/config/engine/containerd/config_v2.go index ee9d33f79..0cadf79ce 100644 --- a/pkg/config/engine/containerd/config_v2.go +++ b/pkg/config/engine/containerd/config_v2.go @@ -20,8 +20,6 @@ import ( "fmt" "github.com/pelletier/go-toml" - - "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" ) // AddRuntime adds a runtime to the containerd config @@ -151,15 +149,3 @@ func (c *Config) RemoveRuntime(name string) error { *c.Tree = config return nil } - -// 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 -} diff --git a/pkg/config/engine/containerd/config_v2_test.go b/pkg/config/engine/containerd/config_v2_test.go index 598222f8c..50e043169 100644 --- a/pkg/config/engine/containerd/config_v2_test.go +++ b/pkg/config/engine/containerd/config_v2_test.go @@ -19,9 +19,10 @@ package containerd 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" ) func TestAddRuntime(t *testing.T) { @@ -224,20 +225,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 := config.Load(tc.config) require.NoError(t, err) - expectedConfig, err := toml.Load(tc.expectedConfig) + expectedConfig, err := config.Load(tc.expectedConfig) require.NoError(t, err) c := &Config{ Logger: logger, - Tree: config, + Toml: cfg, } err = c.AddRuntime("test", "/usr/bin/test", tc.setAsDefault, tc.configOverrides...) require.NoError(t, err) - require.EqualValues(t, expectedConfig.String(), config.String()) + require.EqualValues(t, expectedConfig.String(), cfg.String()) }) } } diff --git a/pkg/config/engine/containerd/containerd.go b/pkg/config/engine/containerd/containerd.go index 3bdc27ae5..f7b102a69 100644 --- a/pkg/config/engine/containerd/containerd.go +++ b/pkg/config/engine/containerd/containerd.go @@ -17,15 +17,16 @@ package containerd import ( - "github.com/pelletier/go-toml" + "fmt" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" ) // Config represents the containerd config type Config struct { - *toml.Tree + *config.Toml Logger logger.Interface RuntimeType string UseDefaultRuntimeName bool @@ -36,14 +37,64 @@ var _ engine.Interface = (*Config)(nil) // New creates a containerd config with the specified options func New(opts ...Option) (engine.Interface, error) { - b := &builder{} + b := &builder{ + runtimeType: defaultRuntimeType, + } for _, opt := range opts { opt(b) } - if b.logger == nil { b.logger = logger.New() } + if b.reference == nil { + b.reference = config.FromFile(b.path) + } + + tomlConfig, err := b.reference.Load() + if err != nil { + return nil, fmt.Errorf("failed to load config: %v", err) + } + + cfg := &Config{ + Toml: tomlConfig, + Logger: b.logger, + RuntimeType: b.runtimeType, + UseDefaultRuntimeName: b.useLegacyConfig, + ContainerAnnotations: b.containerAnnotations, + } - return b.build() + version, err := cfg.parseVersion(b.useLegacyConfig) + if err != nil { + return nil, fmt.Errorf("failed to parse config version: %v", err) + } + switch version { + case 1: + return (*ConfigV1)(cfg), nil + case 2: + return cfg, nil + } + + return nil, fmt.Errorf("unsupported config version: %v", version) +} + +// parseVersion returns the version of the config +func (c *Config) parseVersion(useLegacyConfig bool) (int, error) { + defaultVersion := 2 + if useLegacyConfig { + defaultVersion = 1 + } + + switch v := c.Get("version").(type) { + case nil: + switch len(c.Keys()) { + case 0: // No config exists, or the config file is empty, use version inferred from containerd + return defaultVersion, nil + default: // A config file exists, has content, and no version is set + return 1, nil + } + case int64: + return int(v), nil + default: + return -1, fmt.Errorf("unsupported type for version field: %v", v) + } } diff --git a/pkg/config/engine/containerd/option.go b/pkg/config/engine/containerd/option.go index ae1d99443..5ae010c5a 100644 --- a/pkg/config/engine/containerd/option.go +++ b/pkg/config/engine/containerd/option.go @@ -17,13 +17,8 @@ package containerd import ( - "fmt" - "os" - - "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" ) const ( @@ -32,6 +27,7 @@ const ( type builder struct { logger logger.Interface + reference config.Loader path string runtimeType string useLegacyConfig bool @@ -55,6 +51,13 @@ func WithPath(path string) Option { } } +// WithReference sets the TOML source for the config. +func WithReference(reference config.Loader) Option { + return func(b *builder) { + b.reference = reference + } +} + // WithRuntimeType sets the runtime type for the config builder func WithRuntimeType(runtimeType string) Option { return func(b *builder) { @@ -75,82 +78,3 @@ func WithContainerAnnotations(containerAnnotations ...string) Option { b.containerAnnotations = containerAnnotations } } - -func (b *builder) build() (engine.Interface, error) { - if b.path == "" { - return nil, fmt.Errorf("config path is empty") - } - - if b.runtimeType == "" { - b.runtimeType = defaultRuntimeType - } - - config, err := b.loadConfig(b.path) - if err != nil { - return nil, fmt.Errorf("failed to load config: %v", err) - } - config.Logger = b.logger - config.RuntimeType = b.runtimeType - config.UseDefaultRuntimeName = !b.useLegacyConfig - config.ContainerAnnotations = b.containerAnnotations - - version, err := config.parseVersion(b.useLegacyConfig) - if err != nil { - return nil, fmt.Errorf("failed to parse config version: %v", err) - } - switch version { - case 1: - return (*ConfigV1)(config), nil - case 2: - return config, nil - } - - return nil, fmt.Errorf("unsupported config version: %v", version) -} - -// loadConfig loads the containerd config from disk -func (b *builder) loadConfig(config string) (*Config, error) { - 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) - } - - tomlConfig, err := toml.LoadFile(config) - if err != nil { - return nil, err - } - - cfg := Config{ - Tree: tomlConfig, - } - return &cfg, nil -} - -// parseVersion returns the version of the config -func (c *Config) parseVersion(useLegacyConfig bool) (int, error) { - defaultVersion := 2 - if useLegacyConfig { - defaultVersion = 1 - } - - switch v := c.Get("version").(type) { - case nil: - switch len(c.Keys()) { - case 0: // No config exists, or the config file is empty, use version inferred from containerd - return defaultVersion, nil - default: // A config file exists, has content, and no version is set - return 1, nil - } - case int64: - return int(v), nil - default: - return -1, fmt.Errorf("unsupported type for version field: %v", v) - } -} diff --git a/pkg/config/engine/crio/crio.go b/pkg/config/engine/crio/crio.go index 47320d15b..5de8b142d 100644 --- a/pkg/config/engine/crio/crio.go +++ b/pkg/config/engine/crio/crio.go @@ -22,12 +22,13 @@ import ( "github.com/pelletier/go-toml" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" ) // Config represents the cri-o config type Config struct { - *toml.Tree + *config.Toml Logger logger.Interface } @@ -39,8 +40,23 @@ func New(opts ...Option) (engine.Interface, error) { for _, opt := range opts { opt(b) } + if b.logger == nil { + b.logger = logger.New() + } + if b.reference == nil { + b.reference = config.FromFile(b.path) + } + + tomlConfig, err := b.reference.Load() + if err != nil { + return nil, err + } - return b.build() + cfg := Config{ + Toml: tomlConfig, + Logger: b.logger, + } + return &cfg, nil } // AddRuntime adds a new runtime to the crio config @@ -115,22 +131,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 -} diff --git a/pkg/config/engine/crio/crio_test.go b/pkg/config/engine/crio/crio_test.go index a43d3a945..9e3620d14 100644 --- a/pkg/config/engine/crio/crio_test.go +++ b/pkg/config/engine/crio/crio_test.go @@ -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" ) func TestAddRuntime(t *testing.T) { @@ -127,20 +128,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 := config.Load(tc.config) require.NoError(t, err) - expectedConfig, err := toml.Load(tc.expectedConfig) + expectedConfig, err := config.Load(tc.expectedConfig) require.NoError(t, err) c := &Config{ Logger: logger, - Tree: config, + Toml: cfg, } err = c.AddRuntime("test", "/usr/bin/test", tc.setAsDefault, tc.configOverrides...) require.NoError(t, err) - require.EqualValues(t, expectedConfig.String(), config.String()) + require.EqualValues(t, expectedConfig.String(), cfg.String()) }) } } diff --git a/pkg/config/engine/crio/option.go b/pkg/config/engine/crio/option.go index 9f4d10fc0..1ed4ec8b8 100644 --- a/pkg/config/engine/crio/option.go +++ b/pkg/config/engine/crio/option.go @@ -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" ) type builder struct { - logger logger.Interface - path string + logger logger.Interface + reference config.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() - } - 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, +// WithReference sets the TOML source for the config. +func WithReference(reference config.Loader) Option { + return func(b *builder) { + b.reference = reference } - return &c, nil } diff --git a/pkg/config/engine/docker/docker.go b/pkg/config/engine/docker/docker.go index e6edbe818..8755cbd65 100644 --- a/pkg/config/engine/docker/docker.go +++ b/pkg/config/engine/docker/docker.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" ) @@ -128,6 +129,6 @@ func (c Config) Save(path string) (int64, error) { return 0, fmt.Errorf("unable to convert to JSON: %v", err) } - n, err := engine.Config(path).Write(output) + n, err := config.Raw(path).Write(output) return int64(n), err } diff --git a/pkg/config/source.go b/pkg/config/source.go new file mode 100644 index 000000000..b07a2fd3b --- /dev/null +++ b/pkg/config/source.go @@ -0,0 +1,115 @@ +/** +# Copyright 2024 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package config + +import ( + "bytes" + "fmt" + "os" + "os/exec" + + "github.com/pelletier/go-toml" +) + +const ( + Empty = empty("") +) + +// Loader represents a source for a toml config. +type Loader interface { + Load() (*Toml, error) +} + +type empty string + +var _ Loader = (*empty)(nil) + +// Load is a no-op for an empty source. +func (e empty) Load() (*Toml, error) { + tomlTree, err := toml.TreeFromMap(nil) + if err != nil { + return nil, err + } + + t := Toml{ + Tree: tomlTree, + } + return &t, nil +} + +type tomlFile string + +var _ Loader = (*tomlFile)(nil) + +// FromFile creates a TOML source from the specified file. +// If an empty string is passed an empty toml config is used. +func FromFile(path string) Loader { + if path == "" { + return Empty + } + return tomlFile(path) +} + +// Load loads the contents of the specified TOML file as a map. +func (f tomlFile) Load() (*Toml, error) { + info, err := os.Stat(string(f)) + if os.IsExist(err) && info.IsDir() { + return nil, fmt.Errorf("config file %s is a directory", string(f)) + } + + if os.IsNotExist(err) { + return Empty.Load() + } + + return LoadFile(string(f)) +} + +type tomlFromCommandOutput struct { + command string + args []string +} + +var _ Loader = (*tomlFromCommandOutput)(nil) + +// Load runs the specified command and returns the TOML output as a map. +func (c *tomlFromCommandOutput) Load() (*Toml, error) { + //nolint:gosec // Subprocess launched with a potential tainted input or cmd arguments + cmd := exec.Command(c.command, c.args...) + + var outb bytes.Buffer + var errb bytes.Buffer + + cmd.Stdout = &outb + cmd.Stderr = &errb + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("failed to run command %v %v: %w", c.command, c.args, err) + } + + return LoadBytes(outb.Bytes()) +} + +type tomlFromCRI struct { + socket string +} + +var _ Loader = (*tomlFromCRI)(nil) + +// Load queries the specified CRI socket to get the runtime information. +// See for example: https://github.com/elezar/cri-tools/blob/cd486308b37ca13c5b5293b234876fd03d6f069a/cmd/crictl/info.go#L70 +func (c *tomlFromCRI) Load() (*Toml, error) { + return nil, fmt.Errorf("not implemented for socket %v", c.socket) +} diff --git a/pkg/config/engine/config.go b/pkg/config/toml.go similarity index 50% rename from pkg/config/engine/config.go rename to pkg/config/toml.go index f82a59f2b..ab8207881 100644 --- a/pkg/config/engine/config.go +++ b/pkg/config/toml.go @@ -1,5 +1,5 @@ /** -# Copyright (c) NVIDIA CORPORATION. All rights reserved. +# Copyright 2024 NVIDIA CORPORATION # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,19 +14,76 @@ # limitations under the License. **/ -package engine +package config import ( "fmt" "os" "path/filepath" + + "github.com/pelletier/go-toml" ) -// Config represents a runtime config -type Config string +type Tree *toml.Tree + +// Toml represents a generic toml config with a logger. +type Toml struct { + *toml.Tree +} + +func Load(content string) (*Toml, error) { + return new(func() (*toml.Tree, error) { + return toml.Load(content) + }) +} + +func LoadBytes(b []byte) (*Toml, error) { + return new(func() (*toml.Tree, error) { + return toml.LoadBytes(b) + }) +} + +func LoadFile(path string) (*Toml, error) { + return new(func() (*toml.Tree, error) { + return toml.LoadFile(path) + }) +} + +func LoadMap(m map[string]interface{}) (*Toml, error) { + return new(func() (*toml.Tree, error) { + return toml.TreeFromMap(m) + }) +} + +func new(construct func() (*toml.Tree, error)) (*Toml, error) { + tomlTree, err := construct() + if err != nil { + return nil, err + } + t := &Toml{ + Tree: tomlTree, + } + + return t, nil +} + +// Save writes the config to the specified path +func (c Toml) 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 := Raw(path).Write(output) + return int64(n), err +} + +// Raw represents a raw config file +type Raw string // Write writes the specified contents to a config file. -func (c Config) Write(output []byte) (int, error) { +func (c Raw) Write(output []byte) (int, error) { path := string(c) if path == "" { n, err := os.Stdout.Write(output) diff --git a/tools/container/containerd/config_v1_test.go b/tools/container/containerd/config_v1_test.go index 49a4bc2fe..87e1cef8a 100644 --- a/tools/container/containerd/config_v1_test.go +++ b/tools/container/containerd/config_v1_test.go @@ -24,6 +24,7 @@ import ( testlog "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/require" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd" "github.com/NVIDIA/nvidia-container-toolkit/tools/container" ) @@ -92,12 +93,12 @@ func TestUpdateV1ConfigDefaultRuntime(t *testing.T) { useLegacyConfig: tc.legacyConfig, } - config, err := toml.TreeFromMap(map[string]interface{}{}) + cfg, err := config.Empty.Load() require.NoError(t, err, "%d: %v", i, tc) v1 := &containerd.ConfigV1{ Logger: logger, - Tree: config, + Toml: cfg, UseDefaultRuntimeName: !tc.legacyConfig, RuntimeType: runtimeType, } @@ -240,12 +241,12 @@ func TestUpdateV1Config(t *testing.T) { }, } - config, err := toml.TreeFromMap(map[string]interface{}{}) + cfg, err := config.Empty.Load() require.NoError(t, err) v1 := &containerd.ConfigV1{ Logger: logger, - Tree: config, + Toml: cfg, UseDefaultRuntimeName: true, RuntimeType: runtimeType, ContainerAnnotations: []string{"cdi.k8s.io/*"}, @@ -257,7 +258,7 @@ func TestUpdateV1Config(t *testing.T) { expected, err := toml.TreeFromMap(tc.expectedConfig) require.NoError(t, err) - require.Equal(t, expected.String(), config.String()) + require.Equal(t, expected.String(), cfg.String()) }) } } @@ -401,12 +402,12 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) { }, } - config, err := toml.TreeFromMap(runcConfigMapV1("/runc-binary")) + cfg, err := config.LoadMap(runcConfigMapV1("/runc-binary")) require.NoError(t, err) v1 := &containerd.ConfigV1{ Logger: logger, - Tree: config, + Toml: cfg, UseDefaultRuntimeName: true, RuntimeType: runtimeType, ContainerAnnotations: []string{"cdi.k8s.io/*"}, @@ -418,7 +419,7 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) { expected, err := toml.TreeFromMap(tc.expectedConfig) require.NoError(t, err) - require.Equal(t, expected.String(), config.String()) + require.Equal(t, expected.String(), cfg.String()) }) } } @@ -479,14 +480,14 @@ func TestRevertV1Config(t *testing.T) { }, } - config, err := toml.TreeFromMap(tc.config) + cfg, err := config.LoadMap(tc.config) require.NoError(t, err, "%d: %v", i, tc) expected, err := toml.TreeFromMap(tc.expected) require.NoError(t, err, "%d: %v", i, tc) v1 := &containerd.ConfigV1{ - Tree: config, + Toml: cfg, UseDefaultRuntimeName: true, RuntimeType: runtimeType, } @@ -494,7 +495,7 @@ func TestRevertV1Config(t *testing.T) { err = o.RevertConfig(v1) require.NoError(t, err, "%d: %v", i, tc) - configContents, _ := toml.Marshal(config) + configContents, _ := toml.Marshal(cfg) expectedContents, _ := toml.Marshal(expected) require.Equal(t, string(expectedContents), string(configContents), "%d: %v", i, tc) diff --git a/tools/container/containerd/config_v2_test.go b/tools/container/containerd/config_v2_test.go index ce4a58bef..5c4fdb72e 100644 --- a/tools/container/containerd/config_v2_test.go +++ b/tools/container/containerd/config_v2_test.go @@ -24,6 +24,7 @@ import ( testlog "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/require" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd" "github.com/NVIDIA/nvidia-container-toolkit/tools/container" ) @@ -74,19 +75,19 @@ func TestUpdateV2ConfigDefaultRuntime(t *testing.T) { }, } - config, err := toml.TreeFromMap(map[string]interface{}{}) + cfg, err := config.LoadMap(map[string]interface{}{}) require.NoError(t, err) v2 := &containerd.Config{ Logger: logger, - Tree: config, + Toml: cfg, RuntimeType: runtimeType, } err = o.UpdateConfig(v2) require.NoError(t, err) - defaultRuntimeName := config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}) + defaultRuntimeName := cfg.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}) require.EqualValues(t, tc.expectedDefaultRuntimeName, defaultRuntimeName) }) } @@ -200,12 +201,12 @@ func TestUpdateV2Config(t *testing.T) { runtimeType: runtimeType, } - config, err := toml.TreeFromMap(map[string]interface{}{}) + cfg, err := config.LoadMap(map[string]interface{}{}) require.NoError(t, err) v2 := &containerd.Config{ Logger: logger, - Tree: config, + Toml: cfg, RuntimeType: o.runtimeType, ContainerAnnotations: []string{"cdi.k8s.io/*"}, } @@ -216,7 +217,7 @@ func TestUpdateV2Config(t *testing.T) { expected, err := toml.TreeFromMap(tc.expectedConfig) require.NoError(t, err) - require.Equal(t, expected.String(), config.String()) + require.Equal(t, expected.String(), cfg.String()) }) } @@ -355,12 +356,12 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) { }, } - config, err := toml.TreeFromMap(runcConfigMapV2("/runc-binary")) + cfg, err := config.LoadMap(runcConfigMapV2("/runc-binary")) require.NoError(t, err) v2 := &containerd.Config{ Logger: logger, - Tree: config, + Toml: cfg, RuntimeType: runtimeType, ContainerAnnotations: []string{"cdi.k8s.io/*"}, } @@ -371,7 +372,7 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) { expected, err := toml.TreeFromMap(tc.expectedConfig) require.NoError(t, err) - require.Equal(t, expected.String(), config.String()) + require.Equal(t, expected.String(), cfg.String()) }) } } @@ -427,21 +428,21 @@ func TestRevertV2Config(t *testing.T) { }, } - config, err := toml.TreeFromMap(tc.config) + cfg, err := config.LoadMap(tc.config) require.NoError(t, err) expected, err := toml.TreeFromMap(tc.expected) require.NoError(t, err) v2 := &containerd.Config{ - Tree: config, + Toml: cfg, RuntimeType: runtimeType, } err = o.RevertConfig(v2) require.NoError(t, err) - configContents, _ := toml.Marshal(config) + configContents, _ := toml.Marshal(cfg) expectedContents, _ := toml.Marshal(expected) require.Equal(t, string(expectedContents), string(configContents)) diff --git a/tools/container/containerd/containerd.go b/tools/container/containerd/containerd.go index b5dff882d..84c3f7342 100644 --- a/tools/container/containerd/containerd.go +++ b/tools/container/containerd/containerd.go @@ -25,6 +25,7 @@ import ( cli "github.com/urfave/cli/v2" "github.com/NVIDIA/nvidia-container-toolkit/internal/info" + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd" "github.com/NVIDIA/nvidia-container-toolkit/tools/container" ) @@ -189,6 +190,7 @@ func Setup(c *cli.Context, o *options) error { log.Infof("Starting 'setup' for %v", c.App.Name) cfg, err := containerd.New( + containerd.WithReference(config.FromFile(o.Config)), containerd.WithPath(o.Config), containerd.WithRuntimeType(o.runtimeType), containerd.WithUseLegacyConfig(o.useLegacyConfig),