From 83ad09b1791879564014fc2538f9ea02eef97bec Mon Sep 17 00:00:00 2001 From: Christopher Desiniotis Date: Fri, 1 Dec 2023 15:59:34 -0800 Subject: [PATCH] Refactor the engine.Interface such that the Set() API does not return an extraneous error Signed-off-by: Christopher Desiniotis --- cmd/nvidia-ctk/runtime/configure/configure.go | 8 +++++--- pkg/config/engine/api.go | 2 +- pkg/config/engine/containerd/config_v1.go | 3 +-- pkg/config/engine/containerd/config_v2.go | 3 +-- pkg/config/engine/containerd/containerd.go | 2 ++ pkg/config/engine/crio/crio.go | 6 +++--- pkg/config/engine/docker/docker.go | 5 +++-- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cmd/nvidia-ctk/runtime/configure/configure.go b/cmd/nvidia-ctk/runtime/configure/configure.go index 145fef0e..a3da3e76 100644 --- a/cmd/nvidia-ctk/runtime/configure/configure.go +++ b/cmd/nvidia-ctk/runtime/configure/configure.go @@ -308,9 +308,11 @@ func enableCDI(config *config, cfg engine.Interface) error { } switch config.runtime { case "containerd": - return cfg.Set("enable_cdi", true) + cfg.Set("enable_cdi", true) case "docker": - return cfg.Set("experimental", true) + cfg.Set("experimental", true) + default: + return fmt.Errorf("enabling CDI in %s is not supported", config.runtime) } - return fmt.Errorf("enabling CDI in %s is not supported", config.runtime) + return nil } diff --git a/pkg/config/engine/api.go b/pkg/config/engine/api.go index 3e6d1035..b074dadf 100644 --- a/pkg/config/engine/api.go +++ b/pkg/config/engine/api.go @@ -20,7 +20,7 @@ package engine type Interface interface { DefaultRuntime() string AddRuntime(string, string, bool) error - Set(string, interface{}) error + Set(string, interface{}) RemoveRuntime(string) error Save(string) (int64, error) } diff --git a/pkg/config/engine/containerd/config_v1.go b/pkg/config/engine/containerd/config_v1.go index 04a11cd3..e9afda95 100644 --- a/pkg/config/engine/containerd/config_v1.go +++ b/pkg/config/engine/containerd/config_v1.go @@ -135,11 +135,10 @@ func (c *ConfigV1) RemoveRuntime(name string) error { } // SetOption sets the specified containerd option. -func (c *ConfigV1) Set(key string, value interface{}) error { +func (c *ConfigV1) Set(key string, value interface{}) { config := *c.Tree config.SetPath([]string{"plugins", "cri", "containerd", key}, value) *c.Tree = config - return nil } // Save wrotes the config to a file diff --git a/pkg/config/engine/containerd/config_v2.go b/pkg/config/engine/containerd/config_v2.go index 18dc6b18..52ea54e2 100644 --- a/pkg/config/engine/containerd/config_v2.go +++ b/pkg/config/engine/containerd/config_v2.go @@ -91,11 +91,10 @@ func (c *Config) getRuntimeAnnotations(path []string) ([]string, error) { } // Set sets the specified containerd option. -func (c *Config) Set(key string, value interface{}) error { +func (c *Config) Set(key string, value interface{}) { config := *c.Tree config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", key}, value) *c.Tree = config - return nil } // DefaultRuntime returns the default runtime for the cri-o config diff --git a/pkg/config/engine/containerd/containerd.go b/pkg/config/engine/containerd/containerd.go index b2ea62fa..26ae7746 100644 --- a/pkg/config/engine/containerd/containerd.go +++ b/pkg/config/engine/containerd/containerd.go @@ -31,6 +31,8 @@ type Config struct { ContainerAnnotations []string } +var _ engine.Interface = (*Config)(nil) + // New creates a containerd config with the specified options func New(opts ...Option) (engine.Interface, error) { b := &builder{} diff --git a/pkg/config/engine/crio/crio.go b/pkg/config/engine/crio/crio.go index 0cc4a251..1bbec60e 100644 --- a/pkg/config/engine/crio/crio.go +++ b/pkg/config/engine/crio/crio.go @@ -27,6 +27,8 @@ import ( // Config represents the cri-o config type Config toml.Tree +var _ engine.Interface = (*Config)(nil) + // New creates a cri-o config with the specified options func New(opts ...Option) (engine.Interface, error) { b := &builder{} @@ -100,9 +102,7 @@ func (c *Config) RemoveRuntime(name string) error { } // Set is not supported for cri-o configs. -func (c *Config) Set(key string, value interface{}) error { - return fmt.Errorf("Set is not supported for crio configs") -} +func (c *Config) Set(key string, value interface{}) {} // Save writes the config to the specified path func (c Config) Save(path string) (int64, error) { diff --git a/pkg/config/engine/docker/docker.go b/pkg/config/engine/docker/docker.go index a6828528..831360f6 100644 --- a/pkg/config/engine/docker/docker.go +++ b/pkg/config/engine/docker/docker.go @@ -32,6 +32,8 @@ const ( // TODO: This should not be public, but we need to access it from the tests in tools/container/docker type Config map[string]interface{} +var _ engine.Interface = (*Config)(nil) + // New creates a docker config with the specified options func New(opts ...Option) (engine.Interface, error) { b := &builder{} @@ -115,9 +117,8 @@ func (c *Config) RemoveRuntime(name string) error { } // Set sets the specified docker option -func (c *Config) Set(key string, value interface{}) error { +func (c *Config) Set(key string, value interface{}) { (*c)[key] = value - return nil } // Save writes the config to the specified path