Do not cache image handler.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2019-07-23 19:10:36 -07:00
parent 64bf4bebf3
commit fe0cb22026
4 changed files with 20 additions and 5 deletions

View File

@ -114,6 +114,10 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to resolve image %q", config.GetImage().GetImage()) return nil, errors.Wrapf(err, "failed to resolve image %q", config.GetImage().GetImage())
} }
containerdImage, err := c.toContainerdImage(ctx, image)
if err != nil {
return nil, errors.Wrapf(err, "failed to get image from containerd %q", image.ID)
}
// Run container using the same runtime with sandbox. // Run container using the same runtime with sandbox.
sandboxInfo, err := sandbox.Container.Info(ctx) sandboxInfo, err := sandbox.Container.Info(ctx)
@ -179,7 +183,7 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
// the runtime (runc) a chance to modify (e.g. to create mount // the runtime (runc) a chance to modify (e.g. to create mount
// points corresponding to spec.Mounts) before making the // points corresponding to spec.Mounts) before making the
// rootfs readonly (requested by spec.Root.Readonly). // rootfs readonly (requested by spec.Root.Readonly).
customopts.WithNewSnapshot(id, image.Image), customopts.WithNewSnapshot(id, containerdImage),
} }
if len(volumeMounts) > 0 { if len(volumeMounts) > 0 {

View File

@ -25,6 +25,7 @@ import (
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/containerd/containerd"
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/runtime/linux/runctypes" "github.com/containerd/containerd/runtime/linux/runctypes"
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options" runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
@ -254,6 +255,15 @@ func (c *criService) localResolve(refOrID string) (imagestore.Image, error) {
return c.imageStore.Get(imageID) return c.imageStore.Get(imageID)
} }
// toContainerdImage converts an image object in image store to containerd image handler.
func (c *criService) toContainerdImage(ctx context.Context, image imagestore.Image) (containerd.Image, error) {
// image should always have at least one reference.
if len(image.References) == 0 {
return nil, errors.Errorf("invalid image with no reference %q", image.ID)
}
return c.client.GetImage(ctx, image.References[0])
}
// getUserFromImage gets uid or user name of the image user. // getUserFromImage gets uid or user name of the image user.
// If user is numeric, it will be treated as uid; or else, it is treated as user name. // If user is numeric, it will be treated as uid; or else, it is treated as user name.
func getUserFromImage(user string) (*int64, string) { func getUserFromImage(user string) (*int64, string) {

View File

@ -98,6 +98,10 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to get sandbox image %q", c.config.SandboxImage) return nil, errors.Wrapf(err, "failed to get sandbox image %q", c.config.SandboxImage)
} }
containerdImage, err := c.toContainerdImage(ctx, *image)
if err != nil {
return nil, errors.Wrapf(err, "failed to get image from containerd %q", image.ID)
}
ociRuntime, err := c.getSandboxRuntime(config, r.GetRuntimeHandler()) ociRuntime, err := c.getSandboxRuntime(config, r.GetRuntimeHandler())
if err != nil { if err != nil {
@ -187,7 +191,7 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
} }
opts := []containerd.NewContainerOpts{ opts := []containerd.NewContainerOpts{
containerd.WithSnapshotter(c.config.ContainerdConfig.Snapshotter), containerd.WithSnapshotter(c.config.ContainerdConfig.Snapshotter),
customopts.WithNewSnapshot(id, image.Image), customopts.WithNewSnapshot(id, containerdImage),
containerd.WithSpec(spec, specOpts...), containerd.WithSpec(spec, specOpts...),
containerd.WithContainerLabels(sandboxLabels), containerd.WithContainerLabels(sandboxLabels),
containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata), containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata),

View File

@ -47,8 +47,6 @@ type Image struct {
Size int64 Size int64
// ImageSpec is the oci image structure which describes basic information about the image. // ImageSpec is the oci image structure which describes basic information about the image.
ImageSpec imagespec.Image ImageSpec imagespec.Image
// Containerd image reference
Image containerd.Image
} }
// Store stores all images. // Store stores all images.
@ -152,7 +150,6 @@ func getImage(ctx context.Context, i containerd.Image) (*Image, error) {
ChainID: chainID.String(), ChainID: chainID.String(),
Size: size, Size: size,
ImageSpec: ociimage, ImageSpec: ociimage,
Image: i,
}, nil }, nil
} }