From c0363754fb47fd50a9346f1377b43aee7122fad5 Mon Sep 17 00:00:00 2001 From: Abel Feng Date: Tue, 28 Nov 2023 20:14:48 +0800 Subject: [PATCH] sandbox: get runtime info from sandbox or container For backward compatibility, we should get runtimeInfo from sandbox in db, or get it from the sandbox container in db. Note that this is a temporary solution and we will remove the Container field in Sandbox in cri cache, and replace it with a SandboxInsantance of type containerd.Sandbox interface. Signed-off-by: Abel Feng --- pkg/cri/server/container_create.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/cri/server/container_create.go b/pkg/cri/server/container_create.go index 95571fc95..ebc71e8c5 100644 --- a/pkg/cri/server/container_create.go +++ b/pkg/cri/server/container_create.go @@ -263,14 +263,15 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta containerLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindContainer) - sandboxInfo, err := c.client.SandboxStore().Get(ctx, sandboxID) + // TODO the sandbox in the cache should hold this info + runtimeName, runtimeOption, err := c.runtimeInfo(ctx, sandboxID) if err != nil { - return nil, fmt.Errorf("unable to get sandbox %q metdata: %w", sandboxID, err) + return nil, fmt.Errorf("unable to get sandbox %q runtime info: %w", sandboxID, err) } opts = append(opts, containerd.WithSpec(spec, specOpts...), - containerd.WithRuntime(sandboxInfo.Runtime.Name, sandboxInfo.Runtime.Options), + containerd.WithRuntime(runtimeName, runtimeOption), containerd.WithContainerLabels(containerLabels), containerd.WithContainerExtension(crilabels.ContainerMetadataExtension, &meta), ) @@ -1055,3 +1056,16 @@ func (c *criService) linuxContainerMounts(sandboxID string, config *runtime.Cont } return mounts } + +func (c *criService) runtimeInfo(ctx context.Context, id string) (string, typeurl.Any, error) { + sandboxInfo, err := c.client.SandboxStore().Get(ctx, id) + if err == nil { + return sandboxInfo.Runtime.Name, sandboxInfo.Runtime.Options, nil + } + sandboxContainer, legacyErr := c.client.ContainerService().Get(ctx, id) + if legacyErr == nil { + return sandboxContainer.Runtime.Name, sandboxContainer.Runtime.Options, nil + } + + return "", nil, err +}