This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
container.go
162 lines (144 loc) · 4.68 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package v6
import (
"github.com/fluxcd/flux/pkg/image"
"github.com/fluxcd/flux/pkg/policy"
"github.com/fluxcd/flux/pkg/registry"
"github.com/fluxcd/flux/pkg/update"
"github.com/pkg/errors"
)
// Container describes an individual container including current image info and
// available images.
type Container struct {
Name string `json:",omitempty"`
Current image.Info `json:",omitempty"`
LatestFiltered image.Info `json:",omitempty"`
// All available images (ignoring tag filters)
Available update.SortedImageInfos `json:",omitempty"`
AvailableError string `json:",omitempty"`
AvailableImagesCount int `json:",omitempty"`
NewAvailableImagesCount int `json:",omitempty"`
// Filtered available images (matching tag filters)
FilteredImagesCount int `json:",omitempty"`
NewFilteredImagesCount int `json:",omitempty"`
}
type imageSorter interface {
// SortedImages returns the known images, sorted according to the
// pattern given
SortedImages(policy.Pattern) update.SortedImageInfos
// Images returns the images in no defined order
Images() []image.Info
}
// NewContainer creates a Container given a list of images and the current image
func NewContainer(name string, images imageSorter, currentImage image.Info, tagPattern policy.Pattern, fields []string) (Container, error) {
// Default fields
if len(fields) == 0 {
fields = []string{
"Name",
"Current",
"LatestFiltered",
"Available",
"AvailableError",
"AvailableImagesCount",
"NewAvailableImagesCount",
"FilteredImagesCount",
"NewFilteredImagesCount",
}
}
var c Container
// The following machinery attempts to minimise the number of
// filters (`O(n)`) and sorts (`O(n log n)`), by memoising and
// sharing intermediate results.
var (
sortedImages update.SortedImageInfos
filteredImages []image.Info
sortedFilteredImages update.SortedImageInfos
)
getFilteredImages := func() []image.Info {
if filteredImages == nil {
filteredImages = update.FilterImages(images.Images(), tagPattern)
}
return filteredImages
}
getSortedFilteredImages := func() update.SortedImageInfos {
if sortedFilteredImages == nil {
sortedFilteredImages = update.SortImages(getFilteredImages(), tagPattern)
}
return sortedFilteredImages
}
getSortedImages := func() update.SortedImageInfos {
if sortedImages == nil {
sortedImages = images.SortedImages(tagPattern)
// now that we have the sorted images anyway, the fastest
// way to get sorted, filtered images will be to filter
// the already sorted images
getSortedFilteredImages = func() update.SortedImageInfos {
if sortedFilteredImages == nil {
sortedFilteredImages = update.FilterImages(sortedImages, tagPattern)
}
return sortedFilteredImages
}
getFilteredImages = func() []image.Info {
return []image.Info(getSortedFilteredImages())
}
}
return sortedImages
}
// do these after we've gone through all the field names, since
// they depend on what else is happening
assignFields := []func(){}
for _, field := range fields {
switch field {
// these first few rely only on the inputs
case "Name":
c.Name = name
case "Current":
c.Current = currentImage
case "AvailableError":
if images == nil {
c.AvailableError = registry.ErrNoImageData.Error()
}
case "AvailableImagesCount":
c.AvailableImagesCount = len(images.Images())
// these required the sorted images, which we can get
// straight away
case "Available":
c.Available = getSortedImages()
case "NewAvailableImagesCount":
newImagesCount := 0
for _, img := range getSortedImages() {
if !tagPattern.Newer(&img, ¤tImage) {
break
}
newImagesCount++
}
c.NewAvailableImagesCount = newImagesCount
// these depend on what else gets calculated, so do them afterwards
case "LatestFiltered": // needs sorted, filtered images
assignFields = append(assignFields, func() {
latest, _ := getSortedFilteredImages().Latest()
c.LatestFiltered = latest
})
case "FilteredImagesCount": // needs filtered tags
assignFields = append(assignFields, func() {
c.FilteredImagesCount = len(getFilteredImages())
})
case "NewFilteredImagesCount": // needs filtered images
assignFields = append(assignFields, func() {
newFilteredImagesCount := 0
for _, img := range getSortedFilteredImages() {
if !tagPattern.Newer(&img, ¤tImage) {
break
}
newFilteredImagesCount++
}
c.NewFilteredImagesCount = newFilteredImagesCount
})
default:
return c, errors.Errorf("%s is an invalid field", field)
}
}
for _, fn := range assignFields {
fn()
}
return c, nil
}