Browse Source

Refactor available image functions

Aaron Kirkbride 2 years ago
parent
commit
25d9aaf961
5 changed files with 67 additions and 68 deletions
  1. 20
    19
      daemon/daemon.go
  2. 3
    1
      daemon/images.go
  3. 36
    43
      update/images.go
  4. 6
    4
      update/images_test.go
  5. 2
    1
      update/release.go

+ 20
- 19
daemon/daemon.go View File

@@ -580,25 +580,26 @@ func getServiceContainers(service cluster.Controller, imageRepos update.ImageRep
580 580
 
581 581
 		imageRepo := c.Image.Name
582 582
 		tagPattern := getTagPattern(policyResourceMap, service.ID, c.Name)
583
-		currentImage := imageRepos.FindImageInfo(imageRepo, c.Image)
584
-
585
-		// All available images
586
-		availableImages := imageRepos.Available(imageRepo)
587
-		availableImagesCount := len(availableImages)
588
-		availableImagesErr := ""
589
-		if availableImages == nil {
590
-			availableImagesErr = registry.ErrNoImageData.Error()
583
+
584
+		images := imageRepos.GetRepoImages(imageRepo)
585
+		currentImage := images.FindWithRef(c.Image)
586
+
587
+		// All images
588
+		imagesCount := len(images)
589
+		imagesErr := ""
590
+		if images == nil {
591
+			imagesErr = registry.ErrNoImageData.Error()
591 592
 		}
592
-		var newAvailableImages []image.Info
593
-		for _, img := range availableImages {
593
+		var newImages []image.Info
594
+		for _, img := range images {
594 595
 			if img.CreatedAt.After(currentImage.CreatedAt) {
595
-				newAvailableImages = append(newAvailableImages, img)
596
+				newImages = append(newImages, img)
596 597
 			}
597 598
 		}
598
-		newAvailableImagesCount := len(newAvailableImages)
599
+		newImagesCount := len(newImages)
599 600
 
600
-		// Filtered available images
601
-		filteredImages := imageRepos.FilteredAvailable(imageRepo, tagPattern)
601
+		// Filtered images
602
+		filteredImages := images.Filter(tagPattern)
602 603
 		filteredImagesCount := len(filteredImages)
603 604
 		var newFilteredImages []image.Info
604 605
 		for _, img := range filteredImages {
@@ -615,15 +616,15 @@ func getServiceContainers(service cluster.Controller, imageRepos update.ImageRep
615 616
 			case "Current":
616 617
 				container.Current = currentImage
617 618
 			case "LatestFiltered":
618
-				container.LatestFiltered, _ = imageRepos.LatestFilteredImage(imageRepo, tagPattern)
619
+				container.LatestFiltered, _ = filteredImages.Latest()
619 620
 			case "Available":
620
-				container.Available = availableImages
621
+				container.Available = images
621 622
 			case "AvailableError":
622
-				container.AvailableError = availableImagesErr
623
+				container.AvailableError = imagesErr
623 624
 			case "AvailableImagesCount":
624
-				container.AvailableImagesCount = availableImagesCount
625
+				container.AvailableImagesCount = imagesCount
625 626
 			case "NewAvailableImagesCount":
626
-				container.NewAvailableImagesCount = newAvailableImagesCount
627
+				container.NewAvailableImagesCount = newImagesCount
627 628
 			case "FilteredImagesCount":
628 629
 				container.FilteredImagesCount = filteredImagesCount
629 630
 			case "NewFilteredImagesCount":

+ 3
- 1
daemon/images.go View File

@@ -55,7 +55,9 @@ func (d *Daemon) pollForNewImages(logger log.Logger) {
55 55
 			repo := currentImageID.Name
56 56
 			logger.Log("repo", repo, "pattern", pattern)
57 57
 
58
-			if latest, ok := imageRepos.LatestFilteredImage(repo, pattern); ok && latest.ID != currentImageID {
58
+			filteredImages := imageRepos.GetRepoImages(repo).Filter(pattern)
59
+
60
+			if latest, ok := filteredImages.Latest(); ok && latest.ID != currentImageID {
59 61
 				if latest.ID.Tag == "" {
60 62
 					logger.Log("msg", "untagged image in available images", "action", "skip", "available", repo)
61 63
 					continue

+ 36
- 43
update/images.go View File

@@ -14,74 +14,67 @@ import (
14 14
 	"github.com/weaveworks/flux/resource"
15 15
 )
16 16
 
17
-type imageReposMap map[image.CanonicalName][]image.Info
17
+type imageReposMap map[image.CanonicalName]ImageInfos
18 18
 
19 19
 // ImageRepos contains a map of image repositories to their images
20 20
 type ImageRepos struct {
21 21
 	imageRepos imageReposMap
22 22
 }
23 23
 
24
-// FindImageInfo returns image.Info given an image ref. If the image cannot be
25
-// found, return the image.Info with only the ID.
26
-func (r ImageRepos) FindImageInfo(repo image.Name, ref image.Ref) image.Info {
27
-	images, ok := r.imageRepos[ref.CanonicalName()]
28
-	if !ok {
29
-		return image.Info{ID: ref}
30
-	}
31
-	for _, img := range images {
32
-		if img.ID == ref {
33
-			return img
24
+// GetRepoImages returns image.Info entries for all the images in the
25
+// named image repository.
26
+func (r ImageRepos) GetRepoImages(repo image.Name) ImageInfos {
27
+	if canon, ok := r.imageRepos[repo.CanonicalName()]; ok {
28
+		infos := make([]image.Info, len(canon))
29
+		for i := range canon {
30
+			infos[i] = canon[i]
31
+			infos[i].ID = repo.ToRef(infos[i].ID.Tag)
34 32
 		}
33
+		return infos
35 34
 	}
36
-	return image.Info{ID: ref}
35
+	return nil
37 36
 }
38 37
 
39
-// LatestFilteredImage returns the latest releasable image for a repository
40
-// for which the tag matches a given pattern. A releasable image is
41
-// one that is not tagged "latest". (Assumes the available images are
42
-// in descending order of latestness.) If no such image exists,
43
-// returns a zero value and `false`, and the caller can decide whether
44
-// that's an error or not.
45
-func (r ImageRepos) LatestFilteredImage(repo image.Name, tagGlob string) (image.Info, bool) {
46
-	filtered := r.FilteredAvailable(repo, tagGlob)
47
-	if len(filtered) > 0 {
48
-		return filtered[0], true
49
-	}
50
-	return image.Info{}, false
51
-}
38
+// ImageInfos is a list of image.Info which can be filtered.
39
+type ImageInfos []image.Info
52 40
 
53
-// FilteredAvailable returns image.Info entries for all the images in the
54
-// named image repository which match the tagGlob.
55
-func (r ImageRepos) FilteredAvailable(repo image.Name, tagGlob string) []image.Info {
56
-	var filtered []image.Info
57
-	for _, available := range r.Available(repo) {
58
-		tag := available.ID.Tag
41
+// Filter returns only the images which match the tagGlob.
42
+func (ii ImageInfos) Filter(tagGlob string) ImageInfos {
43
+	var filtered ImageInfos
44
+	for _, i := range ii {
45
+		tag := i.ID.Tag
59 46
 		// Ignore latest if and only if it's not what the user wants.
60 47
 		if !strings.EqualFold(tagGlob, "latest") && strings.EqualFold(tag, "latest") {
61 48
 			continue
62 49
 		}
63 50
 		if glob.Glob(tagGlob, tag) {
64 51
 			var im image.Info
65
-			im = available
66
-			im.ID = repo.ToRef(tag)
52
+			im = i
67 53
 			filtered = append(filtered, im)
68 54
 		}
69 55
 	}
70 56
 	return filtered
71 57
 }
72 58
 
73
-// Available returns image.Info entries for all the images in the
74
-// named image repository.
75
-func (r ImageRepos) Available(repo image.Name) []image.Info {
76
-	if canon, ok := r.imageRepos[repo.CanonicalName()]; ok {
77
-		infos := make([]image.Info, len(canon))
78
-		for i := range canon {
79
-			infos[i] = canon[i]
80
-			infos[i].ID = repo.ToRef(infos[i].ID.Tag)
59
+// Latest returns the latest image from ImageInfos. If no such image exists,
60
+// returns a zero value and `false`, and the caller can decide whether
61
+// that's an error or not.
62
+func (ii ImageInfos) Latest() (image.Info, bool) {
63
+	if len(ii) > 0 {
64
+		return ii[0], true
65
+	}
66
+	return image.Info{}, false
67
+}
68
+
69
+// FindWithRef returns image.Info given an image ref. If the image cannot be
70
+// found, it returns the image.Info with the ID provided.
71
+func (ii ImageInfos) FindWithRef(ref image.Ref) image.Info {
72
+	for _, img := range ii {
73
+		if img.ID == ref {
74
+			return img
81 75
 		}
82
-		return infos
83 76
 	}
84
-	return nil
77
+	return image.Info{ID: ref}
85 78
 }
86 79
 
87 80
 // containers represents a collection of things that have containers

+ 6
- 4
update/images_test.go View File

@@ -24,21 +24,23 @@ func TestDecanon(t *testing.T) {
24 24
 		name: infos,
25 25
 	}}
26 26
 
27
-	latest, ok := m.LatestFilteredImage(mustParseName("weaveworks/helloworld"), "*")
27
+	filteredImages := m.GetRepoImages(mustParseName("weaveworks/helloworld")).Filter("*")
28
+	latest, ok := filteredImages.Latest()
28 29
 	if !ok {
29 30
 		t.Error("did not find latest image")
30 31
 	} else if latest.ID.Name != mustParseName("weaveworks/helloworld") {
31 32
 		t.Error("name did not match what was asked")
32 33
 	}
33 34
 
34
-	latest, ok = m.LatestFilteredImage(mustParseName("index.docker.io/weaveworks/helloworld"), "*")
35
+	filteredImages = m.GetRepoImages(mustParseName("index.docker.io/weaveworks/helloworld")).Filter("*")
36
+	latest, ok = filteredImages.Latest()
35 37
 	if !ok {
36 38
 		t.Error("did not find latest image")
37 39
 	} else if latest.ID.Name != mustParseName("index.docker.io/weaveworks/helloworld") {
38 40
 		t.Error("name did not match what was asked")
39 41
 	}
40 42
 
41
-	avail := m.Available(mustParseName("weaveworks/helloworld"))
43
+	avail := m.GetRepoImages(mustParseName("weaveworks/helloworld"))
42 44
 	if len(avail) != len(infos) {
43 45
 		t.Errorf("expected %d available images, got %d", len(infos), len(avail))
44 46
 	}
@@ -51,7 +53,7 @@ func TestDecanon(t *testing.T) {
51 53
 
52 54
 func TestAvail(t *testing.T) {
53 55
 	m := ImageRepos{imageReposMap{name: infos}}
54
-	avail := m.Available(mustParseName("weaveworks/goodbyeworld"))
56
+	avail := m.GetRepoImages(mustParseName("weaveworks/goodbyeworld"))
55 57
 	if len(avail) > 0 {
56 58
 		t.Errorf("did not expect available images, but got %#v", avail)
57 59
 	}

+ 2
- 1
update/release.go View File

@@ -231,7 +231,8 @@ func (s ReleaseSpec) calculateImageUpdates(rc ReleaseContext, candidates []*Cont
231 231
 		for _, container := range containers {
232 232
 			currentImageID := container.Image
233 233
 
234
-			latestImage, ok := imageRepos.LatestFilteredImage(currentImageID.Name, "*")
234
+			filteredImages := imageRepos.GetRepoImages(currentImageID.Name).Filter("*")
235
+			latestImage, ok := filteredImages.Latest()
235 236
 			if !ok {
236 237
 				if currentImageID.CanonicalName() != singleRepo {
237 238
 					ignoredOrSkipped = ReleaseStatusIgnored

Loading…
Cancel
Save