From 31de84f547be058ec9415fd6bd0c208fe2b55992 Mon Sep 17 00:00:00 2001 From: aiordache Date: Tue, 13 Apr 2021 11:31:39 +0200 Subject: [PATCH] Set image digest as service image to trigger recreation after build Signed-off-by: aiordache --- cli/cmd/compose/images.go | 11 ++++-- local/compose/build.go | 35 +++++++++++++++--- local/compose/images.go | 74 ++++++++++++++++++++------------------- 3 files changed, 78 insertions(+), 42 deletions(-) diff --git a/cli/cmd/compose/images.go b/cli/cmd/compose/images.go index edcf8b07e..bb53f8716 100644 --- a/cli/cmd/compose/images.go +++ b/cli/cmd/compose/images.go @@ -99,8 +99,15 @@ func runImages(ctx context.Context, opts imageOptions, services []string) error for _, img := range images { id := stringid.TruncateID(img.ID) size := units.HumanSizeWithPrecision(float64(img.Size), 3) - - _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, img.Repository, img.Tag, id, size) + repo := img.Repository + if repo == "" { + repo = "" + } + tag := img.Tag + if tag == "" { + tag = "" + } + _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size) } }, "Container", "Repository", "Tag", "Image Id", "Size") diff --git a/local/compose/build.go b/local/compose/build.go index 35a113f48..545f4697d 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -134,12 +134,39 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. } err := s.build(ctx, project, opts, observedState, mode) - if err == nil { - if len(imagesToBuild) > 0 { - utils.DisplayScanSuggestMsg() + if err != nil { + return err + } + if len(imagesToBuild) > 0 { + utils.DisplayScanSuggestMsg() + } + + return s.updateServiceImages(ctx, project) +} + +func (s *composeService) updateServiceImages(ctx context.Context, project *types.Project) error { + imageNames := []string{} + for _, s := range project.Services { + imgName := s.Image + if imgName == "" { + imgName = getImageName(s, project.Name) + } + if !utils.StringContains(imageNames, imgName) { + imageNames = append(imageNames, imgName) } } - return err + images, err := s.getImages(ctx, imageNames) + if err != nil { + return err + } + for i, s := range project.Services { + img, ok := images[getImageName(s, project.Name)] + if !ok { + return fmt.Errorf("failed to retrieve image for service %s", s.Name) + } + project.Services[i].Image = img.ID + } + return nil } func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) { diff --git a/local/compose/images.go b/local/compose/images.go index c6f33a9d9..aff1252b6 100644 --- a/local/compose/images.go +++ b/local/compose/images.go @@ -57,25 +57,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options imageIDs = append(imageIDs, c.ImageID) } } - - images := map[string]moby.ImageInspect{} - l := sync.Mutex{} - eg, ctx := errgroup.WithContext(ctx) - for _, img := range imageIDs { - img := img - eg.Go(func() error { - inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img) - if err != nil { - return err - } - l.Lock() - images[img] = inspect - l.Unlock() - return nil - }) - } - err = eg.Wait() - + images, err := s.getImages(ctx, imageIDs) if err != nil { return nil, err } @@ -85,24 +67,44 @@ func (s *composeService) Images(ctx context.Context, projectName string, options if !ok { return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container)) } - if len(img.RepoTags) == 0 { - return nil, fmt.Errorf("no image tag found for %s", img.ID) - } - tag := "" - repository := "" - repotag := strings.Split(img.RepoTags[0], ":") - repository = repotag[0] - if len(repotag) > 1 { - tag = repotag[1] - } - summary[i] = compose.ImageSummary{ - ID: img.ID, - ContainerName: getCanonicalContainerName(container), - Repository: repository, - Tag: tag, - Size: img.Size, - } + summary[i] = img + summary[i].ContainerName = getCanonicalContainerName(container) } return summary, nil } + +func (s *composeService) getImages(ctx context.Context, images []string) (map[string]compose.ImageSummary, error) { + summary := map[string]compose.ImageSummary{} + l := sync.Mutex{} + eg, ctx := errgroup.WithContext(ctx) + for _, img := range images { + img := img + eg.Go(func() error { + inspect, _, err := s.apiClient.ImageInspectWithRaw(ctx, img) + if err != nil { + return err + } + tag := "" + repository := "" + if len(inspect.RepoTags) > 0 { + + repotag := strings.Split(inspect.RepoTags[0], ":") + repository = repotag[0] + if len(repotag) > 1 { + tag = repotag[1] + } + } + l.Lock() + summary[img] = compose.ImageSummary{ + ID: inspect.ID, + Repository: repository, + Tag: tag, + Size: inspect.Size, + } + l.Unlock() + return nil + }) + } + return summary, eg.Wait() +}