/
repo_stages_storage.go
858 lines (656 loc) · 30.9 KB
/
repo_stages_storage.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package storage
import (
"context"
"fmt"
"strconv"
"strings"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/docker_registry"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/slug"
"github.com/werf/werf/pkg/util"
)
const (
RepoStage_ImageFormat = "%s:%s-%d"
RepoManagedImageRecord_ImageTagPrefix = "managed-image-"
RepoManagedImageRecord_ImageNameFormat = "%s:managed-image-%s"
RepoRejectedStageImageRecord_ImageTagSuffix = "-rejected"
RepoRejectedStageImageRecord_ImageNameFormat = "%s:%s-%d-rejected"
RepoImageMetadataByCommitRecord_ImageTagPrefix = "meta-"
RepoImageMetadataByCommitRecord_TagFormat = "meta-%s_%s_%s"
RepoCustomTagMetadata_ImageTagPrefix = "custom-tag-meta-"
RepoCustomTagMetadata_ImageNameFormat = "%s:custom-tag-meta-%s"
RepoImportMetadata_ImageTagPrefix = "import-metadata-"
RepoImportMetadata_ImageNameFormat = "%s:import-metadata-%s"
RepoClientIDRecord_ImageTagPrefix = "client-id-"
RepoClientIDRecord_ImageNameFormat = "%s:client-id-%s-%d"
UnexpectedTagFormatErrorPrefix = "unexpected tag format"
)
func getDigestAndUniqueIDFromRepoStageImageTag(repoStageImageTag string) (string, int64, error) {
parts := strings.SplitN(repoStageImageTag, "-", 2)
if len(parts) != 2 {
return "", 0, fmt.Errorf("%s %s", UnexpectedTagFormatErrorPrefix, repoStageImageTag)
}
if uniqueID, err := image.ParseUniqueIDAsTimestamp(parts[1]); err != nil {
return "", 0, fmt.Errorf("%s %s: unable to parse unique id %s as timestamp: %s", UnexpectedTagFormatErrorPrefix, repoStageImageTag, parts[1], err)
} else {
return parts[0], uniqueID, nil
}
}
func isUnexpectedTagFormatError(err error) bool {
return strings.HasPrefix(err.Error(), UnexpectedTagFormatErrorPrefix)
}
type RepoStagesStorage struct {
RepoAddress string
DockerRegistry docker_registry.DockerRegistry
ContainerRuntime container_runtime.ContainerRuntime
}
type RepoStagesStorageOptions struct {
docker_registry.DockerRegistryOptions
ContainerRegistry string
}
func NewRepoStagesStorage(repoAddress string, containerRuntime container_runtime.ContainerRuntime, options RepoStagesStorageOptions) (*RepoStagesStorage, error) {
dockerRegistry, err := docker_registry.NewDockerRegistry(repoAddress, options.ContainerRegistry, options.DockerRegistryOptions)
if err != nil {
return nil, fmt.Errorf("error creating container registry accessor for repo %q: %s", repoAddress, err)
}
return &RepoStagesStorage{
RepoAddress: repoAddress,
DockerRegistry: dockerRegistry,
ContainerRuntime: containerRuntime,
}, nil
}
func (storage *RepoStagesStorage) ConstructStageImageName(_, digest string, uniqueID int64) string {
return fmt.Sprintf(RepoStage_ImageFormat, storage.RepoAddress, digest, uniqueID)
}
func (storage *RepoStagesStorage) GetStagesIDs(ctx context.Context, _ string) ([]image.StageID, error) {
var res []image.StageID
if tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress); err != nil {
return nil, fmt.Errorf("unable to fetch tags for repo %q: %s", storage.RepoAddress, err)
} else {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetStagesIDs fetched tags for %q: %#v\n", storage.RepoAddress, tags)
for _, tag := range tags {
if len(tag) != 70 || len(strings.Split(tag, "-")) != 2 { // 2604b86b2c7a1c6d19c62601aadb19e7d5c6bb8f17bc2bf26a390ea7-1611836746968
continue
}
if strings.HasPrefix(tag, RepoManagedImageRecord_ImageTagPrefix) || strings.HasPrefix(tag, RepoImageMetadataByCommitRecord_ImageTagPrefix) || strings.HasSuffix(tag, RepoRejectedStageImageRecord_ImageTagSuffix) {
continue
}
if digest, uniqueID, err := getDigestAndUniqueIDFromRepoStageImageTag(tag); err != nil {
if isUnexpectedTagFormatError(err) {
logboek.Context(ctx).Debug().LogLn(err.Error())
continue
}
return nil, err
} else {
res = append(res, image.StageID{Digest: digest, UniqueID: uniqueID})
logboek.Context(ctx).Debug().LogF("Selected stage by digest %q uniqueID %d\n", digest, uniqueID)
}
}
return res, nil
}
}
func (storage *RepoStagesStorage) ExportStage(ctx context.Context, stageDescription *image.StageDescription, destinationReference string) error {
return storage.DockerRegistry.MutateAndPushImage(ctx, stageDescription.Info.Name, destinationReference, mutateExportStageConfig)
}
func mutateExportStageConfig(config v1.Config) (v1.Config, error) {
if config.Labels == nil {
panic("unexpected condition: stage image without labels")
}
for name := range config.Labels {
if strings.HasPrefix(name, image.WerfLabelPrefix) {
delete(config.Labels, name)
}
}
return config, nil
}
func (storage *RepoStagesStorage) DeleteStage(ctx context.Context, stageDescription *image.StageDescription, _ DeleteImageOptions) error {
if err := storage.DockerRegistry.DeleteRepoImage(ctx, stageDescription.Info); err != nil {
return fmt.Errorf("unable to remove repo image %s: %s", stageDescription.Info.Name, err)
}
rejectedImageName := makeRepoRejectedStageImageRecord(storage.RepoAddress, stageDescription.StageID.Digest, stageDescription.StageID.UniqueID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.DeleteStage full image name: %s\n", rejectedImageName)
if rejectedImgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, rejectedImageName); err != nil {
return fmt.Errorf("unable to get rejected image record %q: %s", rejectedImageName, err)
} else if rejectedImgInfo != nil {
if err := storage.DockerRegistry.DeleteRepoImage(ctx, rejectedImgInfo); err != nil {
return fmt.Errorf("unable to remove rejected image record %q: %s", rejectedImageName, err)
}
}
return nil
}
func makeRepoRejectedStageImageRecord(repoAddress, digest string, uniqueID int64) string {
return fmt.Sprintf(RepoRejectedStageImageRecord_ImageNameFormat, repoAddress, digest, uniqueID)
}
func (storage *RepoStagesStorage) RejectStage(ctx context.Context, projectName, digest string, uniqueID int64) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RejectStage %s %s %d\n", projectName, digest, uniqueID)
rejectedImageName := makeRepoRejectedStageImageRecord(storage.RepoAddress, digest, uniqueID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RejectStage full image name: %s\n", rejectedImageName)
if isExists, err := storage.DockerRegistry.IsRepoImageExists(ctx, rejectedImageName); err != nil {
return err
} else if isExists {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RejectStage record %q is exists => exiting\n", rejectedImageName)
return nil
}
opts := &docker_registry.PushImageOptions{Labels: map[string]string{image.WerfLabel: projectName}}
if err := storage.DockerRegistry.PushImage(ctx, rejectedImageName, opts); err != nil {
return fmt.Errorf("unable to push rejected stage image record %s: %s", rejectedImageName, err)
}
logboek.Context(ctx).Info().LogF("Rejected stage by digest %s uniqueID %d\n", digest, uniqueID)
return nil
}
func (storage *RepoStagesStorage) CreateRepo(ctx context.Context) error {
return storage.DockerRegistry.CreateRepo(ctx, storage.RepoAddress)
}
func (storage *RepoStagesStorage) DeleteRepo(ctx context.Context) error {
return storage.DockerRegistry.DeleteRepo(ctx, storage.RepoAddress)
}
func (storage *RepoStagesStorage) GetStagesIDsByDigest(ctx context.Context, _, digest string) ([]image.StageID, error) {
var res []image.StageID
if tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress); err != nil {
return nil, fmt.Errorf("unable to fetch tags for repo %q: %s", storage.RepoAddress, err)
} else {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetRepoImagesByDigest fetched tags for %q: %#v\n", storage.RepoAddress, tags)
var rejectedStages []image.StageID
for _, tag := range tags {
if !strings.HasSuffix(tag, RepoRejectedStageImageRecord_ImageTagSuffix) {
continue
}
realTag := strings.TrimSuffix(tag, RepoRejectedStageImageRecord_ImageTagSuffix)
if _, uniqueID, err := getDigestAndUniqueIDFromRepoStageImageTag(realTag); err != nil {
if isUnexpectedTagFormatError(err) {
logboek.Context(ctx).Info().LogF("Unexpected tag %q format: %s\n", realTag, err)
continue
}
return nil, fmt.Errorf("unable to get digest and uniqueID from rejected stage tag %q: %s", tag, err)
} else {
logboek.Context(ctx).Info().LogF("Found rejected stage %q\n", tag)
rejectedStages = append(rejectedStages, image.StageID{Digest: digest, UniqueID: uniqueID})
}
}
FindSuitableStages:
for _, tag := range tags {
if !strings.HasPrefix(tag, digest) {
logboek.Context(ctx).Debug().LogF("Discard tag %q: should have prefix %q\n", tag, digest)
continue
}
if strings.HasSuffix(tag, RepoRejectedStageImageRecord_ImageTagSuffix) {
continue
}
if _, uniqueID, err := getDigestAndUniqueIDFromRepoStageImageTag(tag); err != nil {
if isUnexpectedTagFormatError(err) {
logboek.Context(ctx).Debug().LogLn(err.Error())
logboek.Context(ctx).Info().LogF("Unexpected tag %q format: %s\n", tag, err)
continue
}
return nil, fmt.Errorf("unable to get digest and uniqueID from tag %q: %s", tag, err)
} else {
stageID := image.StageID{Digest: digest, UniqueID: uniqueID}
for _, rejectedStage := range rejectedStages {
if rejectedStage.Digest == stageID.Digest && rejectedStage.UniqueID == stageID.UniqueID {
logboek.Context(ctx).Info().LogF("Discarding rejected stage %q\n", tag)
continue FindSuitableStages
}
}
logboek.Context(ctx).Debug().LogF("Stage %q is suitable for digest %q\n", tag, digest)
res = append(res, stageID)
}
}
}
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetRepoImagesByDigest result for %q: %#v\n", storage.RepoAddress, res)
return res, nil
}
func (storage *RepoStagesStorage) GetStageDescription(ctx context.Context, projectName, digest string, uniqueID int64) (*image.StageDescription, error) {
stageImageName := storage.ConstructStageImageName(projectName, digest, uniqueID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage GetStageDescription %s %s %d\n", projectName, digest, uniqueID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage stageImageName = %q\n", stageImageName)
imgInfo, err := storage.DockerRegistry.GetRepoImage(ctx, stageImageName)
if docker_registry.IsNameUnknownError(err) || docker_registry.IsManifestUnknownError(err) {
return nil, nil
}
if docker_registry.IsBlobUnknownError(err) || docker_registry.IsHarbor404Error(err) || docker_registry.IsQuayTagExpiredErr(err) {
return nil, ErrBrokenImage
}
if err != nil {
return nil, fmt.Errorf("unable to inspect repo image %s: %s", stageImageName, err)
}
rejectedImageName := makeRepoRejectedStageImageRecord(storage.RepoAddress, digest, uniqueID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetStageDescription check rejected image name: %s\n", rejectedImageName)
if rejectedImgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, rejectedImageName); err != nil {
return nil, fmt.Errorf("unable to get repo image %q: %s", rejectedImageName, err)
} else if rejectedImgInfo != nil {
logboek.Context(ctx).Info().LogF("Stage digest %s uniqueID %d image is rejected: ignore stage image\n", digest, uniqueID)
return nil, nil
}
return &image.StageDescription{
StageID: &image.StageID{Digest: digest, UniqueID: uniqueID},
Info: imgInfo,
}, nil
}
func (storage *RepoStagesStorage) CheckStageCustomTag(ctx context.Context, stageDescription *image.StageDescription, tag string) error {
fullImageName := strings.Join([]string{storage.RepoAddress, tag}, ":")
customTagImgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return err
}
if customTagImgInfo == nil {
return fmt.Errorf("custom tag %q not found", tag)
}
if customTagImgInfo.ID != stageDescription.Info.ID {
return fmt.Errorf("custom tag %q image must be the same as associated content-based tag %q image", tag, stageDescription.StageID.String())
}
return nil
}
func (storage *RepoStagesStorage) AddStageCustomTag(ctx context.Context, stageDescription *image.StageDescription, tag string) error {
if err := storage.addStageCustomTagMetadata(ctx, stageDescription, tag); err != nil {
return fmt.Errorf("unable to add stage custom tag metadata: %s", err)
}
return storage.DockerRegistry.TagRepoImage(ctx, stageDescription.Info, tag)
}
func (storage *RepoStagesStorage) DeleteStageCustomTag(ctx context.Context, tag string) error {
if err := storage.deleteStageCustomTagMetadata(ctx, tag); err != nil {
return fmt.Errorf("unable to delete stage custom tag metadata: %s", err)
}
fullImageName := strings.Join([]string{storage.RepoAddress, tag}, ":")
imgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return fmt.Errorf("unable to get repo image %q info: %s", fullImageName, err)
}
if imgInfo == nil {
return nil
}
if err := storage.DockerRegistry.DeleteRepoImage(ctx, imgInfo); err != nil {
return fmt.Errorf("unable to delete image %q from repo: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) addStageCustomTagMetadata(ctx context.Context, stageDescription *image.StageDescription, tag string) error {
fullImageName := makeRepoCustomTagMetadataRecord(storage.RepoAddress, tag)
metadata := newCustomTagMetadata(stageDescription.StageID.String(), tag)
opts := &docker_registry.PushImageOptions{
Labels: metadata.ToLabels(),
}
opts.Labels[image.WerfLabel] = stageDescription.Info.Labels[image.WerfLabel]
if err := storage.DockerRegistry.PushImage(ctx, fullImageName, opts); err != nil {
return fmt.Errorf("unable to push image %s: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) deleteStageCustomTagMetadata(ctx context.Context, tagOrID string) error {
fullImageName := makeRepoCustomTagMetadataRecord(storage.RepoAddress, tagOrID)
imgInfo, err := storage.DockerRegistry.GetRepoImage(ctx, fullImageName)
if err != nil {
return fmt.Errorf("unable to get repo image %q info: %s", fullImageName, err)
}
if imgInfo == nil {
panic("unexpected condition")
}
if err := storage.DockerRegistry.DeleteRepoImage(ctx, imgInfo); err != nil {
return fmt.Errorf("unable to delete image %q from repo: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) GetStageCustomTagMetadata(ctx context.Context, tagOrID string) (*CustomTagMetadata, error) {
fullImageName := makeRepoCustomTagMetadataRecord(storage.RepoAddress, tagOrID)
img, err := storage.DockerRegistry.GetRepoImage(ctx, fullImageName)
if err != nil {
return nil, fmt.Errorf("unable to get repo image %s: %s", fullImageName, err)
}
if img == nil {
panic("unexpected condition")
}
return newCustomTagMetadataFromLabels(img.Labels), nil
}
func (storage *RepoStagesStorage) GetStageCustomTagMetadataIDs(ctx context.Context) ([]string, error) {
tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress)
if err != nil {
return nil, fmt.Errorf("unable to get repo %s tags: %s", storage.RepoAddress, err)
}
var res []string
for _, tag := range tags {
if !strings.HasPrefix(tag, RepoCustomTagMetadata_ImageTagPrefix) {
continue
}
id := strings.TrimPrefix(tag, RepoCustomTagMetadata_ImageTagPrefix)
res = append(res, id)
}
return res, nil
}
func (storage *RepoStagesStorage) AddManagedImage(ctx context.Context, projectName, imageName string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.AddManagedImage %s %s\n", projectName, imageName)
if validateImageName(imageName) != nil {
return nil
}
fullImageName := makeRepoManagedImageRecord(storage.RepoAddress, imageName)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.AddManagedImage full image name: %s\n", fullImageName)
if isExists, err := storage.DockerRegistry.IsRepoImageExists(ctx, fullImageName); err != nil {
return err
} else if isExists {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.AddManagedImage record %q is exists => exiting\n", fullImageName)
return nil
}
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.AddManagedImage record %q does not exist => creating record\n", fullImageName)
opts := &docker_registry.PushImageOptions{Labels: map[string]string{image.WerfLabel: projectName}}
if err := storage.DockerRegistry.PushImage(ctx, fullImageName, opts); err != nil {
return fmt.Errorf("unable to push image %s: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) RmManagedImage(ctx context.Context, projectName, imageName string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmManagedImage %s %s\n", projectName, imageName)
fullImageName := makeRepoManagedImageRecord(storage.RepoAddress, imageName)
imgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return fmt.Errorf("unable to get repo image %q info: %s", fullImageName, err)
}
if imgInfo == nil {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmManagedImage record %q does not exist => exiting\n", fullImageName)
return nil
}
if err := storage.DockerRegistry.DeleteRepoImage(ctx, imgInfo); err != nil {
return fmt.Errorf("unable to delete image %q from repo: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) GetManagedImages(ctx context.Context, projectName string) ([]string, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetManagedImages %s\n", projectName)
var res []string
if tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress); err != nil {
return nil, fmt.Errorf("unable to get repo %s tags: %s", storage.RepoAddress, err)
} else {
for _, tag := range tags {
if !strings.HasPrefix(tag, RepoManagedImageRecord_ImageTagPrefix) {
continue
}
managedImageName := unslugDockerImageTagAsImageName(strings.TrimPrefix(tag, RepoManagedImageRecord_ImageTagPrefix))
if validateImageName(managedImageName) != nil {
continue
}
res = append(res, managedImageName)
}
}
return res, nil
}
func (storage *RepoStagesStorage) FetchImage(ctx context.Context, img container_runtime.LegacyImageInterface) error {
if err := storage.ContainerRuntime.PullImageFromRegistry(ctx, img); err != nil {
if strings.HasSuffix(err.Error(), "unknown blob") {
return ErrBrokenImage
}
return err
}
return nil
}
func (storage *RepoStagesStorage) StoreImage(ctx context.Context, img container_runtime.LegacyImageInterface) error {
if img.GetBuiltId() != "" {
if err := storage.ContainerRuntime.Tag(ctx, img.GetBuiltId(), img.Name(), container_runtime.TagOpts{}); err != nil {
return fmt.Errorf("unable to tag built image %q by %q: %s", img.GetBuiltId(), img.Name(), err)
}
}
if err := storage.ContainerRuntime.Push(ctx, img.Name(), container_runtime.PushOpts{}); err != nil {
return fmt.Errorf("unable to push image %q: %s", img.Name(), err)
}
return nil
}
func (storage *RepoStagesStorage) ShouldFetchImage(ctx context.Context, img container_runtime.LegacyImageInterface) (bool, error) {
if info, err := storage.ContainerRuntime.GetImageInfo(ctx, img.Name(), container_runtime.GetImageInfoOpts{}); err != nil {
return false, fmt.Errorf("unable to get inspect for image %s: %s", img.Name(), err)
} else if info != nil {
img.SetInfo(info)
return false, nil
}
return true, nil
}
func (storage *RepoStagesStorage) PutImageMetadata(ctx context.Context, projectName, imageName, commit, stageID string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PutImageMetadata %s %s %s %s\n", projectName, imageName, commit, stageID)
fullImageName := makeRepoImageMetadataName(storage.RepoAddress, imageName, commit, stageID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PutImageMetadata full image name: %s\n", fullImageName)
opts := &docker_registry.PushImageOptions{Labels: map[string]string{image.WerfLabel: projectName}}
if err := storage.DockerRegistry.PushImage(ctx, fullImageName, opts); err != nil {
return fmt.Errorf("unable to push image %s: %s", fullImageName, err)
}
logboek.Context(ctx).Info().LogF("Put image %s commit %s stage ID %s\n", imageName, commit, stageID)
return nil
}
func (storage *RepoStagesStorage) RmImageMetadata(ctx context.Context, projectName, imageNameOrID, commit, stageID string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmImageMetadata %s %s %s %s\n", projectName, imageNameOrID, commit, stageID)
img, err := storage.selectMetadataNameImage(ctx, imageNameOrID, commit, stageID)
if err != nil {
return err
}
if img == nil {
return nil
}
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmImageMetadata full image name: %s\n", img.Tag)
if err := storage.DockerRegistry.DeleteRepoImage(ctx, img); err != nil {
return fmt.Errorf("unable to remove repo image %s: %s", img.Tag, err)
}
logboek.Context(ctx).Info().LogF("Removed image %s commit %s stage ID %s\n", imageNameOrID, commit, stageID)
return nil
}
func (storage *RepoStagesStorage) selectMetadataNameImage(ctx context.Context, imageNameOrID, commit, stageID string) (*image.Info, error) {
for _, fullImageName := range []string{
makeRepoImageMetadataName(storage.RepoAddress, imageNameOrID, commit, stageID),
makeRepoImageMetadataNameByImageID(storage.RepoAddress, imageNameOrID, commit, stageID),
} {
if img, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName); err != nil {
return nil, fmt.Errorf("unable to get repo image %s: %s", fullImageName, err)
} else if img != nil {
return img, nil
}
}
return nil, nil
}
func (storage *RepoStagesStorage) IsImageMetadataExist(ctx context.Context, projectName, imageName, commit, stageID string) (bool, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.IsImageMetadataExist %s %s %s %s\n", projectName, imageName, commit, stageID)
fullImageName := makeRepoImageMetadataName(storage.RepoAddress, imageName, commit, stageID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.IsImageMetadataExist full image name: %s\n", fullImageName)
img, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
return img != nil, err
}
func (storage *RepoStagesStorage) GetAllAndGroupImageMetadataByImageName(ctx context.Context, projectName string, imageNameList []string) (map[string]map[string][]string, map[string]map[string][]string, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetImageNameStageIDCommitList %s %s\n", projectName)
tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress)
if err != nil {
return nil, nil, fmt.Errorf("unable to get repo %s tags: %s", storage.RepoAddress, err)
}
return groupImageMetadataTagsByImageName(ctx, imageNameList, tags, RepoImageMetadataByCommitRecord_ImageTagPrefix)
}
func (storage *RepoStagesStorage) GetImportMetadata(ctx context.Context, _, id string) (*ImportMetadata, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetImportMetadata %s\n", id)
fullImageName := makeRepoImportMetadataName(storage.RepoAddress, id)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetImportMetadata full image name: %s\n", fullImageName)
img, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return nil, fmt.Errorf("unable to get repo image %s: %s", fullImageName, err)
}
if img != nil {
return newImportMetadataFromLabels(img.Labels), nil
}
return nil, nil
}
func (storage *RepoStagesStorage) PutImportMetadata(ctx context.Context, projectName string, metadata *ImportMetadata) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PutImportMetadata %v\n", metadata)
fullImageName := makeRepoImportMetadataName(storage.RepoAddress, metadata.ImportSourceID)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PutImportMetadata full image name: %s\n", fullImageName)
opts := &docker_registry.PushImageOptions{
Labels: metadata.ToLabels(),
}
opts.Labels[image.WerfLabel] = projectName
if err := storage.DockerRegistry.PushImage(ctx, fullImageName, opts); err != nil {
return fmt.Errorf("unable to push image %s: %s", fullImageName, err)
}
return nil
}
func (storage *RepoStagesStorage) RmImportMetadata(ctx context.Context, _, id string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmImportMetadata %s\n", id)
fullImageName := makeRepoImportMetadataName(storage.RepoAddress, id)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.RmImportMetadata full image name: %s\n", fullImageName)
img, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return fmt.Errorf("unable to get repo image %s: %s", fullImageName, err)
} else if img == nil {
return nil
}
if err := storage.DockerRegistry.DeleteRepoImage(ctx, img); err != nil {
return fmt.Errorf("unable to remove repo image %s: %s", img.Tag, err)
}
return nil
}
func (storage *RepoStagesStorage) GetImportMetadataIDs(ctx context.Context, _ string) ([]string, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetImportMetadataIDs\n")
tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress)
if err != nil {
return nil, fmt.Errorf("unable to get repo %s tags: %s", storage.RepoAddress, err)
}
var ids []string
for _, tag := range tags {
if !strings.HasPrefix(tag, RepoImportMetadata_ImageTagPrefix) {
continue
}
ids = append(ids, getImportMetadataIDFromRepoTag(tag))
}
return ids, nil
}
func getImportMetadataIDFromRepoTag(tag string) string {
return strings.TrimPrefix(tag, RepoImportMetadata_ImageTagPrefix)
}
func makeRepoImportMetadataName(repoAddress, importSourceID string) string {
return fmt.Sprintf(RepoImportMetadata_ImageNameFormat, repoAddress, importSourceID)
}
func groupImageMetadataTagsByImageName(ctx context.Context, imageNameList []string, tags []string, imageTagPrefix string) (map[string]map[string][]string, map[string]map[string][]string, error) {
imageNameNameByID := map[string]string{}
for _, imageName := range imageNameList {
imageNameNameByID[imageNameID(imageName)] = imageName
}
result := map[string]map[string][]string{}
resultNotManagedImageName := map[string]map[string][]string{}
for _, tag := range tags {
var res map[string]map[string][]string
if !strings.HasPrefix(tag, imageTagPrefix) {
continue
}
sluggedImageAndCommit := strings.TrimPrefix(tag, imageTagPrefix)
sluggedImageAndCommitParts := strings.Split(sluggedImageAndCommit, "_")
if len(sluggedImageAndCommitParts) != 3 {
// unexpected
continue
}
tagImageNameID := sluggedImageAndCommitParts[0]
tagCommit := sluggedImageAndCommitParts[1]
tagStageID := sluggedImageAndCommitParts[2]
logboek.Context(ctx).Debug().LogF("Found image ID %s commit %s stage ID %s\n", tagImageNameID, tagCommit, tagStageID)
imageName, ok := imageNameNameByID[tagImageNameID]
if !ok {
res = resultNotManagedImageName
imageName = tagImageNameID
} else {
res = result
}
stageIDCommitList, ok := res[imageName]
if !ok {
stageIDCommitList = map[string][]string{}
}
commitList, ok := stageIDCommitList[tagStageID]
if !ok {
commitList = []string{}
}
commitList = append(commitList, tagCommit)
stageIDCommitList[tagStageID] = commitList
res[imageName] = stageIDCommitList
}
return result, resultNotManagedImageName, nil
}
func makeRepoImageMetadataName(repoAddress, imageNameOrID, commit, stageID string) string {
return makeRepoImageMetadataNameByImageID(repoAddress, imageNameID(imageNameOrID), commit, stageID)
}
func makeRepoImageMetadataNameByImageID(repoAddress, imageID, commit, stageID string) string {
return strings.Join([]string{
repoAddress,
fmt.Sprintf(RepoImageMetadataByCommitRecord_TagFormat, imageID, commit, stageID),
}, ":")
}
func (storage *RepoStagesStorage) String() string {
return storage.RepoAddress
}
func (storage *RepoStagesStorage) Address() string {
return storage.RepoAddress
}
func makeRepoCustomTagMetadataRecord(repoAddress, tag string) string {
return fmt.Sprintf(RepoCustomTagMetadata_ImageNameFormat, repoAddress, slug.LimitedSlug(tag, 48))
}
func makeRepoManagedImageRecord(repoAddress, imageName string) string {
return fmt.Sprintf(RepoManagedImageRecord_ImageNameFormat, repoAddress, slugImageNameAsDockerImageTag(imageName))
}
func imageNameID(imageName string) string {
return util.MurmurHash(imageName)
}
func slugImageNameAsDockerImageTag(imageName string) string {
res := imageName
res = strings.ReplaceAll(res, "/", "__slash__")
res = strings.ReplaceAll(res, "+", "__plus__")
if imageName == "" {
res = NamelessImageRecordTag
}
return res
}
func unslugDockerImageTagAsImageName(tag string) string {
res := tag
res = strings.ReplaceAll(res, "__slash__", "/")
res = strings.ReplaceAll(res, "__plus__", "+")
if res == NamelessImageRecordTag {
res = ""
}
return res
}
func validateImageName(name string) error {
if strings.ToLower(name) != name {
return fmt.Errorf("no upcase symbols allowed")
}
return nil
}
func (storage *RepoStagesStorage) GetClientIDRecords(ctx context.Context, projectName string) ([]*ClientIDRecord, error) {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetClientIDRecords for project %s\n", projectName)
var res []*ClientIDRecord
if tags, err := storage.DockerRegistry.Tags(ctx, storage.RepoAddress); err != nil {
return nil, fmt.Errorf("unable to get repo %s tags: %s", storage.RepoAddress, err)
} else {
for _, tag := range tags {
if !strings.HasPrefix(tag, RepoClientIDRecord_ImageTagPrefix) {
continue
}
tagWithoutPrefix := strings.TrimPrefix(tag, RepoClientIDRecord_ImageTagPrefix)
dataParts := strings.SplitN(util.Reverse(tagWithoutPrefix), "-", 2)
if len(dataParts) != 2 {
continue
}
clientID, timestampMillisecStr := util.Reverse(dataParts[1]), util.Reverse(dataParts[0])
timestampMillisec, err := strconv.ParseInt(timestampMillisecStr, 10, 64)
if err != nil {
continue
}
rec := &ClientIDRecord{ClientID: clientID, TimestampMillisec: timestampMillisec}
res = append(res, rec)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.GetClientIDRecords got clientID record: %s\n", rec)
}
}
return res, nil
}
func (storage *RepoStagesStorage) PostClientIDRecord(ctx context.Context, projectName string, rec *ClientIDRecord) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PostClientID %s for project %s\n", rec.ClientID, projectName)
fullImageName := fmt.Sprintf(RepoClientIDRecord_ImageNameFormat, storage.RepoAddress, rec.ClientID, rec.TimestampMillisec)
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.PostClientID full image name: %s\n", fullImageName)
if isExists, err := storage.DockerRegistry.IsRepoImageExists(ctx, fullImageName); err != nil {
return err
} else if isExists {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage.AddManagedImage record %q is exists => exiting\n", fullImageName)
return nil
}
opts := &docker_registry.PushImageOptions{Labels: map[string]string{image.WerfLabel: projectName}}
if err := storage.DockerRegistry.PushImage(ctx, fullImageName, opts); err != nil {
return fmt.Errorf("unable to push image %s: %s", fullImageName, err)
}
logboek.Context(ctx).Info().LogF("Posted new clientID %q for project %s\n", rec.ClientID, projectName)
return nil
}