From fe0cb22026386fd391b17e26d7d6789f280f7aa7 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Tue, 23 Jul 2019 19:10:36 -0700 Subject: [PATCH] Do not cache image handler. Signed-off-by: Lantao Liu --- pkg/server/container_create.go | 6 +++++- pkg/server/helpers.go | 10 ++++++++++ pkg/server/sandbox_run.go | 6 +++++- pkg/store/image/image.go | 3 --- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/pkg/server/container_create.go b/pkg/server/container_create.go index f83f72482..315a95fdd 100644 --- a/pkg/server/container_create.go +++ b/pkg/server/container_create.go @@ -114,6 +114,10 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta if err != nil { 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. 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 // points corresponding to spec.Mounts) before making the // rootfs readonly (requested by spec.Root.Readonly). - customopts.WithNewSnapshot(id, image.Image), + customopts.WithNewSnapshot(id, containerdImage), } if len(volumeMounts) > 0 { diff --git a/pkg/server/helpers.go b/pkg/server/helpers.go index ec1fae171..bf7d2bc74 100644 --- a/pkg/server/helpers.go +++ b/pkg/server/helpers.go @@ -25,6 +25,7 @@ import ( "strings" "github.com/BurntSushi/toml" + "github.com/containerd/containerd" "github.com/containerd/containerd/containers" "github.com/containerd/containerd/runtime/linux/runctypes" 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) } +// 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. // If user is numeric, it will be treated as uid; or else, it is treated as user name. func getUserFromImage(user string) (*int64, string) { diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index 1fc8dd666..0b0a19ad1 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -98,6 +98,10 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox if err != nil { 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()) if err != nil { @@ -187,7 +191,7 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox } opts := []containerd.NewContainerOpts{ containerd.WithSnapshotter(c.config.ContainerdConfig.Snapshotter), - customopts.WithNewSnapshot(id, image.Image), + customopts.WithNewSnapshot(id, containerdImage), containerd.WithSpec(spec, specOpts...), containerd.WithContainerLabels(sandboxLabels), containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata), diff --git a/pkg/store/image/image.go b/pkg/store/image/image.go index f05609082..a956c13a5 100644 --- a/pkg/store/image/image.go +++ b/pkg/store/image/image.go @@ -47,8 +47,6 @@ type Image struct { Size int64 // ImageSpec is the oci image structure which describes basic information about the image. ImageSpec imagespec.Image - // Containerd image reference - Image containerd.Image } // Store stores all images. @@ -152,7 +150,6 @@ func getImage(ctx context.Context, i containerd.Image) (*Image, error) { ChainID: chainID.String(), Size: size, ImageSpec: ociimage, - Image: i, }, nil }