From 686b7ef7bf5b60c50b45307d548518c9709da87e Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Thu, 30 Sep 2021 16:42:45 +0200 Subject: pkg/specgen: cache image in generator To prevent expensive redundant lookups and inspects on the same image, cache the image in the generator. Note that once a given image has been inspected, subsequent calls will use the libimage-internal cache. [NO TESTS NEEDED] since it is no functional change. Signed-off-by: Valentin Rothberg --- pkg/specgen/generate/container.go | 42 +++++++++++++++++++++++++------- pkg/specgen/generate/container_create.go | 19 ++++++--------- 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'pkg/specgen/generate') diff --git a/pkg/specgen/generate/container.go b/pkg/specgen/generate/container.go index 71b882510..f4b2eeb25 100644 --- a/pkg/specgen/generate/container.go +++ b/pkg/specgen/generate/container.go @@ -18,19 +18,43 @@ import ( "golang.org/x/sys/unix" ) +func getImageFromSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerator) (*libimage.Image, string, *libimage.ImageData, error) { + if s.Image == "" || s.Rootfs != "" { + return nil, "", nil, nil + } + + // Image may already have been set in the generator. + image, resolvedName := s.GetImage() + if image != nil { + inspectData, err := image.Inspect(ctx, false) + if err != nil { + return nil, "", nil, err + } + return image, resolvedName, inspectData, nil + } + + // Need to look up image. + image, resolvedName, err := r.LibimageRuntime().LookupImage(s.Image, nil) + if err != nil { + return nil, "", nil, err + } + s.SetImage(image, resolvedName) + inspectData, err := image.Inspect(ctx, false) + if err != nil { + return nil, "", nil, err + } + return image, resolvedName, inspectData, err +} + // Fill any missing parts of the spec generator (e.g. from the image). // Returns a set of warnings or any fatal error that occurred. func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerator) ([]string, error) { // Only add image configuration if we have an image - var newImage *libimage.Image - var inspectData *libimage.ImageData - var err error - if s.Image != "" { - newImage, _, err = r.LibimageRuntime().LookupImage(s.Image, nil) - if err != nil { - return nil, err - } - + newImage, _, inspectData, err := getImageFromSpec(ctx, r, s) + if err != nil { + return nil, err + } + if inspectData != nil { inspectData, err = newImage.Inspect(ctx, false) if err != nil { return nil, err diff --git a/pkg/specgen/generate/container_create.go b/pkg/specgen/generate/container_create.go index 6100e7a5b..d0db9f35d 100644 --- a/pkg/specgen/generate/container_create.go +++ b/pkg/specgen/generate/container_create.go @@ -102,20 +102,15 @@ func MakeContainer(ctx context.Context, rt *libpod.Runtime, s *specgen.SpecGener options = append(options, libpod.WithCreateCommand(s.ContainerCreateCommand)) } - var newImage *libimage.Image - var imageData *libimage.ImageData if s.Rootfs != "" { options = append(options, libpod.WithRootFS(s.Rootfs, s.RootfsOverlay)) - } else { - var resolvedImageName string - newImage, resolvedImageName, err = rt.LibimageRuntime().LookupImage(s.Image, nil) - if err != nil { - return nil, nil, nil, err - } - imageData, err = newImage.Inspect(ctx, false) - if err != nil { - return nil, nil, nil, err - } + } + + newImage, resolvedImageName, imageData, err := getImageFromSpec(ctx, rt, s) + if err != nil { + return nil, nil, nil, err + } + if newImage != nil { // If the input name changed, we could properly resolve the // image. Otherwise, it must have been an ID where we're // defaulting to the first name or an empty one if no names are -- cgit v1.2.3-54-g00ecf