From 25b052cbcabcc032fa511af3c814474c3291bb8c Mon Sep 17 00:00:00 2001 From: Kirtana Ashok Date: Thu, 7 Dec 2023 12:23:00 -0800 Subject: [PATCH] Move GenerateRuntimeOptions() to pkg/cri/config Signed-off-by: Kirtana Ashok --- pkg/cri/config/config.go | 42 +++++++++++++++++++++++ pkg/cri/server/helpers.go | 46 -------------------------- pkg/cri/server/helpers_test.go | 2 +- pkg/cri/server/runtime_config_linux.go | 3 +- pkg/cri/server/sandbox_run.go | 2 +- plugins/types.go | 3 ++ 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/pkg/cri/config/config.go b/pkg/cri/config/config.go index fb526ecd9..a9446e370 100644 --- a/pkg/cri/config/config.go +++ b/pkg/cri/config/config.go @@ -24,10 +24,15 @@ import ( "time" "github.com/containerd/log" + "github.com/pelletier/go-toml/v2" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" + runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" "github.com/containerd/containerd/v2/pkg/cri/annotations" "github.com/containerd/containerd/v2/pkg/deprecation" + runtimeoptions "github.com/containerd/containerd/v2/pkg/runtimeoptions/v1" + "github.com/containerd/containerd/v2/plugins" + runcoptions "github.com/containerd/containerd/v2/runtime/v2/runc/options" ) const ( @@ -532,3 +537,40 @@ func hostAccessingSandbox(config *runtime.PodSandboxConfig) bool { return false } + +// GenerateRuntimeOptions generates runtime options from cri plugin config. +func GenerateRuntimeOptions(r Runtime) (interface{}, error) { + if r.Options == nil { + return nil, nil + } + + b, err := toml.Marshal(r.Options) + if err != nil { + return nil, fmt.Errorf("failed to marshal TOML blob for runtime %q: %w", r.Type, err) + } + + options := getRuntimeOptionsType(r.Type) + if err := toml.Unmarshal(b, options); err != nil { + return nil, err + } + + // For generic configuration, if no config path specified (preserving old behavior), pass + // the whole TOML configuration section to the runtime. + if runtimeOpts, ok := options.(*runtimeoptions.Options); ok && runtimeOpts.ConfigPath == "" { + runtimeOpts.ConfigBody = b + } + + return options, nil +} + +// getRuntimeOptionsType gets empty runtime options by the runtime type name. +func getRuntimeOptionsType(t string) interface{} { + switch t { + case plugins.RuntimeRuncV2: + return &runcoptions.Options{} + case plugins.RuntimeRunhcsV1: + return &runhcsoptions.Options{} + default: + return &runtimeoptions.Options{} + } +} diff --git a/pkg/cri/server/helpers.go b/pkg/cri/server/helpers.go index b17fcd2eb..17f5e1c66 100644 --- a/pkg/cri/server/helpers.go +++ b/pkg/cri/server/helpers.go @@ -27,23 +27,17 @@ import ( "strings" "time" - runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" "github.com/containerd/typeurl/v2" runtimespec "github.com/opencontainers/runtime-spec/specs-go" - "github.com/pelletier/go-toml/v2" runtime "k8s.io/cri-api/pkg/apis/runtime/v1" containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/containers" "github.com/containerd/containerd/v2/errdefs" clabels "github.com/containerd/containerd/v2/labels" - criconfig "github.com/containerd/containerd/v2/pkg/cri/config" crilabels "github.com/containerd/containerd/v2/pkg/cri/labels" containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" - runtimeoptions "github.com/containerd/containerd/v2/pkg/runtimeoptions/v1" - "github.com/containerd/containerd/v2/plugins" - runcoptions "github.com/containerd/containerd/v2/runtime/v2/runc/options" "github.com/containerd/log" ) @@ -75,9 +69,6 @@ const ( // defaultIfName is the default network interface for the pods defaultIfName = "eth0" - // runtimeRunhcsV1 is the runtime type for runhcs. - runtimeRunhcsV1 = "io.containerd.runhcs.v1" - // devShm is the default path of /dev/shm. devShm = "/dev/shm" // etcHosts is the default path of /etc/hosts file. @@ -250,43 +241,6 @@ func buildLabels(configLabels, imageConfigLabels map[string]string, containerTyp return labels } -// generateRuntimeOptions generates runtime options from cri plugin config. -func generateRuntimeOptions(r criconfig.Runtime) (interface{}, error) { - if r.Options == nil { - return nil, nil - } - - b, err := toml.Marshal(r.Options) - if err != nil { - return nil, fmt.Errorf("failed to marshal TOML blob for runtime %q: %w", r.Type, err) - } - - options := getRuntimeOptionsType(r.Type) - if err := toml.Unmarshal(b, options); err != nil { - return nil, err - } - - // For generic configuration, if no config path specified (preserving old behavior), pass - // the whole TOML configuration section to the runtime. - if runtimeOpts, ok := options.(*runtimeoptions.Options); ok && runtimeOpts.ConfigPath == "" { - runtimeOpts.ConfigBody = b - } - - return options, nil -} - -// getRuntimeOptionsType gets empty runtime options by the runtime type name. -func getRuntimeOptionsType(t string) interface{} { - switch t { - case plugins.RuntimeRuncV2: - return &runcoptions.Options{} - case runtimeRunhcsV1: - return &runhcsoptions.Options{} - default: - return &runtimeoptions.Options{} - } -} - // getRuntimeOptions get runtime options from container metadata. func getRuntimeOptions(c containers.Container) (interface{}, error) { from := c.Runtime.Options diff --git a/pkg/cri/server/helpers_test.go b/pkg/cri/server/helpers_test.go index bebd47550..4c78b5d62 100644 --- a/pkg/cri/server/helpers_test.go +++ b/pkg/cri/server/helpers_test.go @@ -175,7 +175,7 @@ systemd_cgroup = true } { test := test t.Run(test.desc, func(t *testing.T) { - opts, err := generateRuntimeOptions(test.r) + opts, err := criconfig.GenerateRuntimeOptions(test.r) assert.NoError(t, err) assert.Equal(t, test.expectedOptions, opts) }) diff --git a/pkg/cri/server/runtime_config_linux.go b/pkg/cri/server/runtime_config_linux.go index f5b678daa..71802570f 100644 --- a/pkg/cri/server/runtime_config_linux.go +++ b/pkg/cri/server/runtime_config_linux.go @@ -20,6 +20,7 @@ import ( "context" "sort" + criconfig "github.com/containerd/containerd/v2/pkg/cri/config" "github.com/containerd/containerd/v2/pkg/systemd" runcoptions "github.com/containerd/containerd/v2/runtime/v2/runc/options" "github.com/containerd/log" @@ -48,7 +49,7 @@ func (c *criService) getCgroupDriver(ctx context.Context) runtime.CgroupDriver { }) for _, handler := range handlerNames { - opts, err := generateRuntimeOptions(c.config.ContainerdConfig.Runtimes[handler]) + opts, err := criconfig.GenerateRuntimeOptions(c.config.ContainerdConfig.Runtimes[handler]) if err != nil { log.G(ctx).Debugf("failed to parse runtime handler options for %q", handler) continue diff --git a/pkg/cri/server/sandbox_run.go b/pkg/cri/server/sandbox_run.go index fcb9bf858..6a0b21fa3 100644 --- a/pkg/cri/server/sandbox_run.go +++ b/pkg/cri/server/sandbox_run.go @@ -96,7 +96,7 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox runtimeStart := time.Now() // Retrieve runtime options - runtimeOpts, err := generateRuntimeOptions(ociRuntime) + runtimeOpts, err := criconfig.GenerateRuntimeOptions(ociRuntime) if err != nil { return nil, fmt.Errorf("failed to generate sandbox runtime options: %w", err) } diff --git a/plugins/types.go b/plugins/types.go index dfe9b9666..740e2d253 100644 --- a/plugins/types.go +++ b/plugins/types.go @@ -75,6 +75,9 @@ const ( // RuntimeRuncV2 is the runc runtime that supports multiple containers per shim RuntimeRuncV2 = "io.containerd.runc.v2" + // RuntimeRunhcsV1 is the runtime type for runhcs. + RuntimeRunhcsV1 = "io.containerd.runhcs.v1" + DeprecationsPlugin = "deprecations" )