Browse Source

Set image digest as service image to trigger recreation after build

Signed-off-by: aiordache <[email protected]>
aiordache 4 years ago
parent
commit
31de84f547
3 changed files with 76 additions and 40 deletions
  1. 9 2
      cli/cmd/compose/images.go
  2. 31 4
      local/compose/build.go
  3. 36 34
      local/compose/images.go

+ 9 - 2
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 = "<none>"
+				}
+				tag := img.Tag
+				if tag == "" {
+					tag = "<none>"
+				}
+				_, _ = 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")

+ 31 - 4
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) {

+ 36 - 34
local/compose/images.go

@@ -57,52 +57,54 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
 			imageIDs = append(imageIDs, c.ImageID)
 		}
 	}
+	images, err := s.getImages(ctx, imageIDs)
+	if err != nil {
+		return nil, err
+	}
+	summary := make([]compose.ImageSummary, len(containers))
+	for i, container := range containers {
+		img, ok := images[container.ImageID]
+		if !ok {
+			return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(container))
+		}
+
+		summary[i] = img
+		summary[i].ContainerName = getCanonicalContainerName(container)
+	}
+	return summary, nil
+}
 
-	images := map[string]moby.ImageInspect{}
+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 imageIDs {
+	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()
-			images[img] = inspect
+			summary[img] = compose.ImageSummary{
+				ID:         inspect.ID,
+				Repository: repository,
+				Tag:        tag,
+				Size:       inspect.Size,
+			}
 			l.Unlock()
 			return nil
 		})
 	}
-	err = eg.Wait()
-
-	if err != nil {
-		return nil, err
-	}
-	summary := make([]compose.ImageSummary, len(containers))
-	for i, container := range containers {
-		img, ok := images[container.ImageID]
-		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,
-		}
-	}
-	return summary, nil
+	return summary, eg.Wait()
 }