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 <fshb1988@gmail.com>
This commit is contained in:
Abel Feng 2023-11-28 20:14:48 +08:00
parent 95d2a3b7c0
commit c0363754fb

View File

@ -263,14 +263,15 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
containerLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindContainer) 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 { 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, opts = append(opts,
containerd.WithSpec(spec, specOpts...), containerd.WithSpec(spec, specOpts...),
containerd.WithRuntime(sandboxInfo.Runtime.Name, sandboxInfo.Runtime.Options), containerd.WithRuntime(runtimeName, runtimeOption),
containerd.WithContainerLabels(containerLabels), containerd.WithContainerLabels(containerLabels),
containerd.WithContainerExtension(crilabels.ContainerMetadataExtension, &meta), containerd.WithContainerExtension(crilabels.ContainerMetadataExtension, &meta),
) )
@ -1055,3 +1056,16 @@ func (c *criService) linuxContainerMounts(sandboxID string, config *runtime.Cont
} }
return mounts 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
}