diff --git a/pkg/config/engine/containerd/config.go b/pkg/config/engine/containerd/config.go index 8b76edbb..52a336ba 100644 --- a/pkg/config/engine/containerd/config.go +++ b/pkg/config/engine/containerd/config.go @@ -34,36 +34,36 @@ func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error { runtimeNamesForConfig := engine.GetLowLevelRuntimes(c) for _, r := range runtimeNamesForConfig { - options := config.GetSubtreeByPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", r}) + options := config.GetSubtreeByPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", r}) if options == nil { continue } c.Logger.Debugf("using options from runtime %v: %v", r, options) - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}, options.Copy()) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}, options.Copy()) break } - if config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}) == nil { + if config.GetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}) == nil { c.Logger.Warningf("could not infer options from runtimes %v; using defaults", runtimeNamesForConfig) - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "runtime_type"}, c.RuntimeType) - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "runtime_root"}, "") - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "runtime_engine"}, "") - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "privileged_without_host_devices"}, false) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_type"}, c.RuntimeType) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_root"}, "") + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "runtime_engine"}, "") + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "privileged_without_host_devices"}, false) } if len(c.ContainerAnnotations) > 0 { - annotations, err := c.getRuntimeAnnotations([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"}) + annotations, err := c.getRuntimeAnnotations([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "container_annotations"}) if err != nil { return err } annotations = append(c.ContainerAnnotations, annotations...) - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"}, annotations) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "container_annotations"}, annotations) } - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "options", "BinaryName"}, path) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "options", "BinaryName"}, path) if setAsDefault { - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}, name) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}, name) } *c.Tree = config @@ -99,13 +99,13 @@ func (c *Config) getRuntimeAnnotations(path []string) ([]string, error) { // Set sets the specified containerd option. func (c *Config) Set(key string, value interface{}) { config := *c.Tree - config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", key}, value) + config.SetPath([]string{"plugins", c.CRIRuntimePluginName, key}, value) *c.Tree = config } // DefaultRuntime returns the default runtime for the cri-o config func (c Config) DefaultRuntime() string { - if runtime, ok := c.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}).(string); ok { + if runtime, ok := c.GetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}).(string); ok { return runtime } return "" @@ -119,14 +119,14 @@ func (c *Config) RemoveRuntime(name string) error { config := *c.Tree - config.DeletePath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}) - if runtime, ok := config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}).(string); ok { + config.DeletePath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}) + if runtime, ok := config.GetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}).(string); ok { if runtime == name { - config.DeletePath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}) + config.DeletePath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}) } } - runtimePath := []string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name} + runtimePath := []string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name} for i := 0; i < len(runtimePath); i++ { if runtimes, ok := config.GetPath(runtimePath[:len(runtimePath)-i]).(*toml.Tree); ok { if len(runtimes.Keys()) == 0 { diff --git a/pkg/config/engine/containerd/config_v2_test.go b/pkg/config/engine/containerd/config_v2_test.go index 80c2675f..836d2b1d 100644 --- a/pkg/config/engine/containerd/config_v2_test.go +++ b/pkg/config/engine/containerd/config_v2_test.go @@ -203,15 +203,15 @@ func TestAddRuntime(t *testing.T) { expectedConfig: ` version = 3 [plugins] - [plugins."io.containerd.grpc.v1.cri"] - [plugins."io.containerd.grpc.v1.cri".containerd] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test] + [plugins."io.containerd.cri.v1.runtime"] + [plugins."io.containerd.cri.v1.runtime".containerd] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.test] privileged_without_host_devices = false runtime_engine = "" runtime_root = "" runtime_type = "io.containerd.runc.v2" - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test.options] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.test.options] BinaryName = "/usr/bin/test" `, expectedError: nil, @@ -221,38 +221,38 @@ func TestAddRuntime(t *testing.T) { config: ` version = 3 [plugins] - [plugins."io.containerd.grpc.v1.cri"] - [plugins."io.containerd.grpc.v1.cri".containerd] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] + [plugins."io.containerd.cri.v1.runtime"] + [plugins."io.containerd.cri.v1.runtime".containerd] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.runc] privileged_without_host_devices = true runtime_engine = "engine" runtime_root = "root" runtime_type = "type" - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.runc.options] BinaryName = "/usr/bin/runc" SystemdCgroup = true `, expectedConfig: ` version = 3 [plugins] - [plugins."io.containerd.grpc.v1.cri"] - [plugins."io.containerd.grpc.v1.cri".containerd] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes] - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] + [plugins."io.containerd.cri.v1.runtime"] + [plugins."io.containerd.cri.v1.runtime".containerd] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.runc] privileged_without_host_devices = true runtime_engine = "engine" runtime_root = "root" runtime_type = "type" - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.runc.options] BinaryName = "/usr/bin/runc" SystemdCgroup = true - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.test] privileged_without_host_devices = true runtime_engine = "engine" runtime_root = "root" runtime_type = "type" - [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test.options] + [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.test.options] BinaryName = "/usr/bin/test" SystemdCgroup = true `, diff --git a/pkg/config/engine/containerd/containerd.go b/pkg/config/engine/containerd/containerd.go index 3e171018..767f0588 100644 --- a/pkg/config/engine/containerd/containerd.go +++ b/pkg/config/engine/containerd/containerd.go @@ -42,6 +42,10 @@ type Config struct { // 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 + // CRIRuntimePluginName represents the fully qualified name of the containerd plugin + // for the CRI runtime service. The name of this plugin was changed in v3 of the + // containerd configuration file. + CRIRuntimePluginName string } var _ engine.Interface = (*Config)(nil) @@ -101,7 +105,11 @@ func New(opts ...Option) (engine.Interface, error) { switch configVersion { case 1: return (*ConfigV1)(cfg), nil - case 2, 3: + case 2: + cfg.CRIRuntimePluginName = "io.containerd.grpc.v1.cri" + return cfg, nil + case 3: + cfg.CRIRuntimePluginName = "io.containerd.cri.v1.runtime" return cfg, nil } return nil, fmt.Errorf("unsupported config version: %v", configVersion) @@ -133,7 +141,7 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) { if c == nil || c.Tree == nil { return nil, fmt.Errorf("config is nil") } - runtimeData := c.GetSubtreeByPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}) + runtimeData := c.GetSubtreeByPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}) return &containerdCfgRuntime{ tree: runtimeData, }, nil