Improve the implementation for UseLegacyConfig

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-11-29 16:26:51 +01:00
parent 56faf71991
commit f91791b4d1
No known key found for this signature in database
4 changed files with 53 additions and 42 deletions

View File

@ -70,18 +70,20 @@ func (c *ConfigV1) AddRuntime(name string, path string, setAsDefault bool) error
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "BinaryName"}, path) config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "BinaryName"}, path)
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "Runtime"}, path) config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "Runtime"}, path)
if setAsDefault && c.UseDefaultRuntimeName { if setAsDefault {
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime_name"}, name) if !c.UseLegacyConfig {
} else if setAsDefault { config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime_name"}, name)
// Note: This is deprecated in containerd 1.4.0 and will be removed in 1.5.0 } else {
if config.GetPath([]string{"plugins", "cri", "containerd", "default_runtime"}) == nil { // Note: This is deprecated in containerd 1.4.0 and will be removed in 1.5.0
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_type"}, c.RuntimeType) if config.GetPath([]string{"plugins", "cri", "containerd", "default_runtime"}) == nil {
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_root"}, "") config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_type"}, c.RuntimeType)
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_engine"}, "") config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_root"}, "")
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "privileged_without_host_devices"}, false) config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "runtime_engine"}, "")
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "privileged_without_host_devices"}, false)
}
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "BinaryName"}, path)
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "Runtime"}, path)
} }
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "BinaryName"}, path)
config.SetPath([]string{"plugins", "cri", "containerd", "default_runtime", "options", "Runtime"}, path)
} }
*c.Tree = config *c.Tree = config

View File

@ -27,10 +27,15 @@ import (
// Config represents the containerd config // Config represents the containerd config
type Config struct { type Config struct {
*toml.Tree *toml.Tree
Logger logger.Interface Logger logger.Interface
RuntimeType string RuntimeType string
UseDefaultRuntimeName bool ContainerAnnotations []string
ContainerAnnotations []string // UseLegacyConfig indicates whether a config file pre v1.3 should be generated.
// For version 1 config prior to containerd v1.4 the default runtime was
// specified in a containerd.runtimes.default_runtime section.
// This was deprecated in v1.4 in favour of containerd.default_runtime_name.
// Support for this section has been removed in v2.0.
UseLegacyConfig bool
} }
var _ engine.Interface = (*Config)(nil) var _ engine.Interface = (*Config)(nil)
@ -73,14 +78,14 @@ func New(opts ...Option) (engine.Interface, error) {
} }
cfg := &Config{ cfg := &Config{
Tree: tomlConfig, Tree: tomlConfig,
Logger: b.logger, Logger: b.logger,
RuntimeType: b.runtimeType, RuntimeType: b.runtimeType,
UseDefaultRuntimeName: b.useLegacyConfig, ContainerAnnotations: b.containerAnnotations,
ContainerAnnotations: b.containerAnnotations, UseLegacyConfig: b.useLegacyConfig,
} }
version, err := cfg.parseVersion(b.useLegacyConfig) version, err := cfg.parseVersion()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse config version: %v", err) return nil, fmt.Errorf("failed to parse config version: %v", err)
} }
@ -95,9 +100,10 @@ func New(opts ...Option) (engine.Interface, error) {
} }
// parseVersion returns the version of the config // parseVersion returns the version of the config
func (c *Config) parseVersion(useLegacyConfig bool) (int, error) { func (c *Config) parseVersion() (int, error) {
defaultVersion := 2 defaultVersion := 2
if useLegacyConfig { // For legacy configs, we default to v1 configs.
if c.UseLegacyConfig {
defaultVersion = 1 defaultVersion = 1
} }

View File

@ -92,10 +92,10 @@ func TestUpdateV1ConfigDefaultRuntime(t *testing.T) {
require.NoError(t, err, "%d: %v", i, tc) require.NoError(t, err, "%d: %v", i, tc)
v1 := &containerd.ConfigV1{ v1 := &containerd.ConfigV1{
Logger: logger, Logger: logger,
Tree: cfg, Tree: cfg,
UseDefaultRuntimeName: !tc.legacyConfig, UseLegacyConfig: tc.legacyConfig,
RuntimeType: runtimeType, RuntimeType: runtimeType,
} }
err = o.UpdateConfig(v1) err = o.UpdateConfig(v1)
@ -238,11 +238,11 @@ func TestUpdateV1Config(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
v1 := &containerd.ConfigV1{ v1 := &containerd.ConfigV1{
Logger: logger, Logger: logger,
Tree: cfg, Tree: cfg,
UseDefaultRuntimeName: true, UseLegacyConfig: true,
RuntimeType: runtimeType, RuntimeType: runtimeType,
ContainerAnnotations: []string{"cdi.k8s.io/*"}, ContainerAnnotations: []string{"cdi.k8s.io/*"},
} }
err = o.UpdateConfig(v1) err = o.UpdateConfig(v1)
@ -397,11 +397,11 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
v1 := &containerd.ConfigV1{ v1 := &containerd.ConfigV1{
Logger: logger, Logger: logger,
Tree: cfg, Tree: cfg,
UseDefaultRuntimeName: true, UseLegacyConfig: true,
RuntimeType: runtimeType, RuntimeType: runtimeType,
ContainerAnnotations: []string{"cdi.k8s.io/*"}, ContainerAnnotations: []string{"cdi.k8s.io/*"},
} }
err = o.UpdateConfig(v1) err = o.UpdateConfig(v1)
@ -476,9 +476,9 @@ func TestRevertV1Config(t *testing.T) {
require.NoError(t, err, "%d: %v", i, tc) require.NoError(t, err, "%d: %v", i, tc)
v1 := &containerd.ConfigV1{ v1 := &containerd.ConfigV1{
Tree: cfg, Tree: cfg,
UseDefaultRuntimeName: true, UseLegacyConfig: true,
RuntimeType: runtimeType, RuntimeType: runtimeType,
} }
err = o.RevertConfig(v1) err = o.RevertConfig(v1)

View File

@ -52,8 +52,11 @@ type Options struct {
func Flags(opts *Options) []cli.Flag { func Flags(opts *Options) []cli.Flag {
flags := []cli.Flag{ flags := []cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Name: "use-legacy-config", Name: "use-legacy-config",
Usage: "Specify whether a legacy (pre v1.3) config should be used", Usage: "Specify whether a legacy (pre v1.3) config should be used. " +
"This ensures that a version 1 container config is created by default and that the " +
"containerd.runtimes.default_runtime config section is used to define the default " +
"runtime instead of container.default_runtime_name.",
Destination: &opts.useLegacyConfig, Destination: &opts.useLegacyConfig,
EnvVars: []string{"CONTAINERD_USE_LEGACY_CONFIG"}, EnvVars: []string{"CONTAINERD_USE_LEGACY_CONFIG"},
}, },