-
Notifications
You must be signed in to change notification settings - Fork 0
/
lister.go
355 lines (290 loc) · 8.63 KB
/
lister.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package synclister
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/s3"
metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metal-go/api/client/image"
"github.com/metal-stack/metal-go/api/client/partition"
"github.com/metal-stack/metal-image-cache-sync/cmd/internal/metrics"
"github.com/metal-stack/metal-image-cache-sync/pkg/api"
"github.com/metal-stack/metal-image-cache-sync/pkg/utils"
)
type SyncLister struct {
logger *slog.Logger
client metalgo.Client
config *api.Config
s3 *s3.S3
stop context.Context
imageCollector *metrics.ImageCollector
httpClient *http.Client
}
func NewSyncLister(logger *slog.Logger, client metalgo.Client, s3 *s3.S3, imageCollector *metrics.ImageCollector, config *api.Config, stop context.Context) *SyncLister {
return &SyncLister{
logger: logger,
client: client,
config: config,
s3: s3,
stop: stop,
imageCollector: imageCollector,
httpClient: http.DefaultClient,
}
}
func (s *SyncLister) DetermineImageSyncList() ([]api.OS, error) {
s3Images, err := s.retrieveImagesFromS3()
if err != nil {
return nil, fmt.Errorf("error listing images in s3:%w", err)
}
resp, err := s.client.Image().ListImages(image.NewListImagesParams(), nil)
if err != nil {
return nil, fmt.Errorf("error listing images:%w", err)
}
s.imageCollector.SetMetalAPIImageCount(len(resp.Payload))
expirationGraceDays := 24 * time.Hour * time.Duration(s.config.ExpirationGraceDays)
images := api.OSImagesByOS{}
for _, img := range resp.Payload {
if s.isExcluded(img.URL) {
s.logger.Debug("skipping image with exclude URL", "id", *img.ID)
continue
}
if img.ExpirationDate != nil {
if time.Since(time.Time(*img.ExpirationDate)) > expirationGraceDays {
s.logger.Debug("not considering expired image, skipping", "id", *img.ID)
continue
}
}
os, ver, err := utils.GetOsAndSemver(*img.ID)
if err != nil {
s.logger.Error("could not extract os and version, skipping", "error", err)
continue
}
versions, ok := images[os]
if !ok {
versions = api.OSImagesByVersion{}
}
majorMinor := fmt.Sprintf("%d.%d", ver.Major(), ver.Minor())
imageVersions := versions[majorMinor]
u, err := url.Parse(img.URL)
if err != nil {
s.logger.Error("image url is invalid, skipping", "error", err)
continue
}
bucketKey := u.Path[1:]
s3Image, ok := s3Images[bucketKey]
if !ok {
s.logger.Error("image is not contained in global image store, skipping", "path", u.Path, "id", *img.ID)
continue
}
s3MD5, ok := s3Images[bucketKey+".md5"]
if !ok {
s.logger.Error("image md5 is not contained in global image store, skipping", "path", u.Path, "id", *img.ID)
continue
}
imageVersions = append(imageVersions, api.OS{
Name: os,
Version: ver,
ApiRef: *img,
BucketKey: bucketKey,
BucketName: s.config.ImageBucket,
ImageRef: s3Image,
MD5Ref: s3MD5,
})
versions[majorMinor] = imageVersions
images[os] = versions
}
var sizeCount int64
var syncImages []api.OS
for _, versions := range images {
for _, versionedImages := range versions {
versionedImages := versionedImages
sort.Slice(versionedImages, func(i, j int) bool {
return versionedImages[i].Version.GreaterThan(versionedImages[j].Version)
})
amount := 0
for _, img := range versionedImages {
if s.config.MaxImagesPerName > 0 && amount >= s.config.MaxImagesPerName {
break
}
amount += 1
sizeCount += *img.ImageRef.Size
syncImages = append(syncImages, img)
}
}
}
api.SortOSImagesByName(syncImages)
for {
if sizeCount < s.config.MaxCacheSize {
break
}
syncImages, sizeCount, err = s.reduce(syncImages, sizeCount)
if err != nil {
s.logger.Warn("cannot reduce anymore images (all at minimum size), exceeding maximum cache size")
break
}
}
s.imageCollector.SetUnsyncedImageCount(len(resp.Payload) - len(syncImages))
return syncImages, nil
}
func (s *SyncLister) isExcluded(url string) bool {
for _, exclude := range s.config.ExcludePaths {
if strings.Contains(url, exclude) {
return true
}
}
return false
}
func (s *SyncLister) DetermineKernelSyncList() ([]api.Kernel, error) {
resp, err := s.client.Partition().ListPartitions(partition.NewListPartitionsParams(), nil)
if err != nil {
return nil, fmt.Errorf("error listing partitions:%w", err)
}
var result []api.Kernel
urls := map[string]bool{}
for _, p := range resp.Payload {
if p.Bootconfig == nil {
continue
}
kernelURL := p.Bootconfig.Kernelurl
if urls[kernelURL] {
continue
}
if s.isExcluded(kernelURL) {
s.logger.Debug("skipping kernel with exclude URL", "url", kernelURL)
continue
}
u, err := url.Parse(kernelURL)
if err != nil {
s.logger.Error("kernel url is invalid, skipping", "error", err)
continue
}
size, err := retrieveContentLength(s.stop, s.httpClient, u.String())
if err != nil {
s.logger.Warn("unable to determine kernel download size", "error", err)
}
result = append(result, api.Kernel{
SubPath: strings.TrimPrefix(u.Path, "/"),
URL: kernelURL,
Size: size,
})
urls[kernelURL] = true
}
return result, nil
}
func (s *SyncLister) DetermineBootImageSyncList() ([]api.BootImage, error) {
resp, err := s.client.Partition().ListPartitions(partition.NewListPartitionsParams(), nil)
if err != nil {
return nil, fmt.Errorf("error listing partitions:%w", err)
}
var result []api.BootImage
urls := map[string]bool{}
for _, p := range resp.Payload {
if p.Bootconfig == nil {
continue
}
bootImageURL := p.Bootconfig.Imageurl
if urls[bootImageURL] {
continue
}
if s.isExcluded(bootImageURL) {
s.logger.Debug("skipping boot image with exclude URL", "url", bootImageURL)
continue
}
u, err := url.Parse(bootImageURL)
if err != nil {
s.logger.Error("boot image url is invalid, skipping", "error", err)
continue
}
size, err := retrieveContentLength(s.stop, s.httpClient, u.String())
if err != nil {
s.logger.Warn("unable to determine boot image download size", "error", err)
}
md5URL := u.String() + ".md5"
_, err = retrieveContentLength(s.stop, s.httpClient, md5URL)
if err != nil {
s.logger.Error("boot image md5 does not exist, skipping", "url", md5URL, "error", err)
continue
}
result = append(result, api.BootImage{
SubPath: strings.TrimPrefix(u.Path, "/"),
URL: bootImageURL,
Size: size,
})
urls[bootImageURL] = true
}
return result, nil
}
func retrieveContentLength(ctx context.Context, c *http.Client, url string) (int64, error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return 0, fmt.Errorf("unable to create head request:%w", err)
}
req = req.WithContext(ctx)
resp, err := c.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("head request to url did not return OK: %s", url)
}
size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return 0, fmt.Errorf("content-length header value could not be converted to integer:%w", err)
}
return int64(size), nil
}
func (s *SyncLister) reduce(images []api.OS, sizeCount int64) ([]api.OS, int64, error) {
groups := map[string][]api.OS{}
for _, img := range images {
key := fmt.Sprintf("%s-%d.%d", img.Name, img.Version.Major(), img.Version.Minor())
groups[key] = append(groups[key], img)
}
var biggestGroup string
currentBiggest := 1
var groupNames []string
for g := range groups {
groupNames = append(groupNames, g)
}
sort.Strings(groupNames)
for _, name := range groupNames {
amount := len(groups[name])
if amount > s.config.MinImagesPerName && amount > currentBiggest {
currentBiggest = amount
biggestGroup = name
}
}
if biggestGroup == "" {
return images, sizeCount, fmt.Errorf("can not reduce any further")
}
groupImages := groups[biggestGroup]
groups[biggestGroup] = append([]api.OS{}, groupImages[1:]...)
newSize := sizeCount - *groupImages[0].ImageRef.Size
var result []api.OS
for _, imgs := range groups {
result = append(result, imgs...)
}
api.SortOSImagesByName(result)
return result, newSize, nil
}
func (s *SyncLister) retrieveImagesFromS3() (map[string]s3.Object, error) {
res := map[string]s3.Object{}
err := s.s3.ListObjectsPages(&s3.ListObjectsInput{
Bucket: &s.config.ImageBucket,
}, func(objects *s3.ListObjectsOutput, lastPage bool) bool {
for _, o := range objects.Contents {
res[*o.Key] = *o
}
return true
})
if err != nil {
return nil, fmt.Errorf("cannot list s3 objects:%w", err)
}
return res, nil
}