/
storage_manager.go
1019 lines (811 loc) · 41.1 KB
/
storage_manager.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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package manager
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"gopkg.in/yaml.v2"
"github.com/werf/lockgate"
"github.com/werf/logboek"
"github.com/werf/logboek/pkg/style"
"github.com/werf/logboek/pkg/types"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/storage"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/util/parallel"
"github.com/werf/werf/pkg/werf"
)
var (
ErrUnexpectedStagesStorageState = errors.New("unexpected stages storage state")
ErrStageNotFound = errors.New("stage not found")
)
func IsErrUnexpectedStagesStorageState(err error) bool {
if err != nil {
return strings.HasSuffix(err.Error(), ErrUnexpectedStagesStorageState.Error())
}
return false
}
func IsErrStageNotFound(err error) bool {
if err != nil {
return strings.HasSuffix(err.Error(), ErrStageNotFound.Error())
}
return false
}
type ForEachDeleteStageOptions struct {
storage.DeleteImageOptions
storage.FilterStagesAndProcessRelatedDataOptions
}
type StorageManagerInterface interface {
InitCache(ctx context.Context) error
GetStagesStorage() storage.StagesStorage
GetFinalStagesStorage() storage.StagesStorage
GetSecondaryStagesStorageList() []storage.StagesStorage
GetImageInfoGetter(imageName string, stg stage.Interface) *image.InfoGetter
EnableParallel(parallelTasksLimit int)
MaxNumberOfWorkers() int
GenerateStageUniqueID(digest string, stages []*image.StageDescription) (string, int64)
LockStageImage(ctx context.Context, imageName string) error
GetStagesByDigest(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error)
GetStagesByDigestWithCache(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error)
GetStagesByDigestFromStagesStorage(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage) ([]*image.StageDescription, error)
GetStagesByDigestFromStagesStorageWithCache(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage) ([]*image.StageDescription, error)
GetStageDescriptionList(ctx context.Context) ([]*image.StageDescription, error)
GetStageDescriptionListWithCache(ctx context.Context) ([]*image.StageDescription, error)
GetFinalStageDescriptionList(ctx context.Context) ([]*image.StageDescription, error)
FetchStage(ctx context.Context, containerRuntime container_runtime.ContainerRuntime, stg stage.Interface) error
SelectSuitableStage(ctx context.Context, c stage.Conveyor, stg stage.Interface, stages []*image.StageDescription) (*image.StageDescription, error)
CopySuitableByDigestStage(ctx context.Context, stageDesc *image.StageDescription, sourceStagesStorage, destinationStagesStorage storage.StagesStorage, containerRuntime container_runtime.ContainerRuntime) (*image.StageDescription, error)
CopyStageIntoCacheStorages(ctx context.Context, stg stage.Interface, containerRuntime container_runtime.ContainerRuntime) error
CopyStageIntoFinalStorage(ctx context.Context, stg stage.Interface, containerRuntime container_runtime.ContainerRuntime, opts CopyStageIntoFinalStorageOptions) error
ForEachDeleteStage(ctx context.Context, options ForEachDeleteStageOptions, stagesDescriptions []*image.StageDescription, f func(ctx context.Context, stageDesc *image.StageDescription, err error) error) error
ForEachDeleteFinalStage(ctx context.Context, options ForEachDeleteStageOptions, stagesDescriptions []*image.StageDescription, f func(ctx context.Context, stageDesc *image.StageDescription, err error) error) error
ForEachRmImageMetadata(ctx context.Context, projectName, imageNameOrID string, stageIDCommitList map[string][]string, f func(ctx context.Context, commit, stageID string, err error) error) error
ForEachRmManagedImage(ctx context.Context, projectName string, managedImages []string, f func(ctx context.Context, managedImage string, err error) error) error
ForEachGetImportMetadata(ctx context.Context, projectName string, ids []string, f func(ctx context.Context, metadataID string, metadata *storage.ImportMetadata, err error) error) error
ForEachRmImportMetadata(ctx context.Context, projectName string, ids []string, f func(ctx context.Context, id string, err error) error) error
ForEachGetStageCustomTagMetadata(ctx context.Context, ids []string, f func(ctx context.Context, metadataID string, metadata *storage.CustomTagMetadata, err error) error) error
}
func RetryOnUnexpectedStagesStorageState(_ context.Context, _ StorageManagerInterface, f func() error) error {
Retry:
err := f()
if IsErrUnexpectedStagesStorageState(err) {
goto Retry
}
return err
}
func NewStorageManager(projectName string, stagesStorage storage.StagesStorage, finalStagesStorage storage.StagesStorage, secondaryStagesStorageList []storage.StagesStorage, cacheStagesStorageList []storage.StagesStorage, storageLockManager storage.LockManager) *StorageManager {
return &StorageManager{
ProjectName: projectName,
StorageLockManager: storageLockManager,
StagesStorage: stagesStorage,
FinalStagesStorage: finalStagesStorage,
CacheStagesStorageList: cacheStagesStorageList,
SecondaryStagesStorageList: secondaryStagesStorageList,
}
}
type StagesList struct {
Mux sync.Mutex
StageIDs []image.StageID
}
func NewStagesList(stageIDs []image.StageID) *StagesList {
return &StagesList{
StageIDs: stageIDs,
}
}
func (stages *StagesList) GetStageIDs() []image.StageID {
stages.Mux.Lock()
defer stages.Mux.Unlock()
var res []image.StageID
for _, stg := range stages.StageIDs {
res = append(res, stg)
}
return res
}
func (stages *StagesList) AddStageID(stageID image.StageID) {
stages.Mux.Lock()
defer stages.Mux.Unlock()
for _, stg := range stages.StageIDs {
if stg.IsEqual(stageID) {
return
}
}
stages.StageIDs = append(stages.StageIDs, stageID)
}
type StorageManager struct {
parallel bool
parallelTasksLimit int
ProjectName string
StorageLockManager storage.LockManager
StagesStorage storage.StagesStorage
FinalStagesStorage storage.StagesStorage
CacheStagesStorageList []storage.StagesStorage
SecondaryStagesStorageList []storage.StagesStorage
// These will be released automatically when current process exits
SharedHostImagesLocks []lockgate.LockHandle
FinalStagesListCacheMux sync.Mutex
FinalStagesListCache *StagesList
}
func (m *StorageManager) GetStagesStorage() storage.StagesStorage {
return m.StagesStorage
}
func (m *StorageManager) GetFinalStagesStorage() storage.StagesStorage {
return m.FinalStagesStorage
}
func (m *StorageManager) GetSecondaryStagesStorageList() []storage.StagesStorage {
return m.SecondaryStagesStorageList
}
func (m *StorageManager) GetImageInfoGetter(imageName string, stg stage.Interface) *image.InfoGetter {
stageID := stg.GetImage().GetStageDescription().StageID
info := stg.GetImage().GetStageDescription().Info
if m.FinalStagesStorage != nil {
finalImageName := m.FinalStagesStorage.ConstructStageImageName(m.ProjectName, stageID.Digest, stageID.UniqueID)
_, tag := image.ParseRepositoryAndTag(finalImageName)
return image.NewInfoGetter(imageName, finalImageName, tag)
}
return image.NewInfoGetter(
imageName,
info.Name,
info.Tag,
)
}
func (m *StorageManager) InitCache(ctx context.Context) error {
logboek.Context(ctx).Info().LogF("Initializing storage manager cache\n")
if m.FinalStagesStorage != nil {
if _, err := m.getOrCreateFinalStagesListCache(ctx); err != nil {
return fmt.Errorf("unable to get or create final stages list cache: %s", err)
}
}
return nil
}
func (m *StorageManager) EnableParallel(parallelTasksLimit int) {
m.parallel = true
m.parallelTasksLimit = parallelTasksLimit
}
func (m *StorageManager) MaxNumberOfWorkers() int {
if m.parallel && m.parallelTasksLimit > 0 {
return m.parallelTasksLimit
}
return 1
}
func (m *StorageManager) GetStageDescriptionListWithCache(ctx context.Context) ([]*image.StageDescription, error) {
return m.getStageDescriptionList(ctx, storage.WithCache())
}
func (m *StorageManager) GetStageDescriptionList(ctx context.Context) ([]*image.StageDescription, error) {
return m.getStageDescriptionList(ctx)
}
func (m *StorageManager) getStageDescriptionList(ctx context.Context, opts ...storage.Option) ([]*image.StageDescription, error) {
stageIDs, err := m.StagesStorage.GetStagesIDs(ctx, m.ProjectName, opts...)
if err != nil {
return nil, fmt.Errorf("error getting stages ids from %s: %s", m.StagesStorage, err)
}
var mutex sync.Mutex
var stages []*image.StageDescription
if err := parallel.DoTasks(ctx, len(stageIDs), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
}, func(ctx context.Context, taskId int) error {
stageID := stageIDs[taskId]
stageDesc, err := getStageDescription(ctx, m.ProjectName, stageID, m.StagesStorage, m.CacheStagesStorageList, getStageDescriptionOptions{WithLocalManifestCache: m.getWithLocalManifestCacheOption()})
if err != nil {
return fmt.Errorf("error getting stage %s description: %s", stageID.String(), err)
}
if stageDesc == nil {
logboek.Context(ctx).Warn().LogF("Ignoring stage %s: cannot get stage description from %s\n", stageID.String(), m.StagesStorage.String())
}
mutex.Lock()
defer mutex.Unlock()
stages = append(stages, stageDesc)
return nil
}); err != nil {
return nil, err
}
return stages, nil
}
func (m *StorageManager) GetFinalStageDescriptionList(ctx context.Context) ([]*image.StageDescription, error) {
existingStagesListCache, err := m.getOrCreateFinalStagesListCache(ctx)
if err != nil {
return nil, fmt.Errorf("error getting existing stages list of final repo %s: %s", m.FinalStagesStorage.String(), err)
}
logboek.Context(ctx).Debug().LogF("[%p] Got existing final stages list cache: %#v\n", m, existingStagesListCache.StageIDs)
stageIDs := existingStagesListCache.GetStageIDs()
var mutex sync.Mutex
var stages []*image.StageDescription
if err := parallel.DoTasks(ctx, len(stageIDs), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
}, func(ctx context.Context, taskId int) error {
stageID := stageIDs[taskId]
stageDesc, err := getStageDescription(ctx, m.ProjectName, stageID, m.FinalStagesStorage, nil, getStageDescriptionOptions{WithLocalManifestCache: true})
if err != nil {
return fmt.Errorf("error getting stage %s description from %s: %s", stageID.String(), m.FinalStagesStorage.String(), err)
}
if stageDesc == nil {
logboek.Context(ctx).Warn().LogF("Ignoring stage %s: cannot get stage description from %s\n", stageID.String(), m.FinalStagesStorage.String())
}
mutex.Lock()
defer mutex.Unlock()
stages = append(stages, stageDesc)
return nil
}); err != nil {
return nil, err
}
return stages, nil
}
func (m *StorageManager) ForEachDeleteFinalStage(ctx context.Context, options ForEachDeleteStageOptions, stagesDescriptions []*image.StageDescription, f func(ctx context.Context, stageDesc *image.StageDescription, err error) error) error {
return parallel.DoTasks(ctx, len(stagesDescriptions), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
InitDockerCLIForEachWorker: true,
}, func(ctx context.Context, taskId int) error {
stageDescription := stagesDescriptions[taskId]
err := m.FinalStagesStorage.DeleteStage(ctx, stageDescription, options.DeleteImageOptions)
return f(ctx, stageDescription, err)
})
}
func (m *StorageManager) ForEachDeleteStage(ctx context.Context, options ForEachDeleteStageOptions, stagesDescriptions []*image.StageDescription, f func(ctx context.Context, stageDesc *image.StageDescription, err error) error) error {
if localStagesStorage, isLocal := m.StagesStorage.(*storage.DockerServerStagesStorage); isLocal {
filteredStagesDescriptions, err := localStagesStorage.FilterStagesAndProcessRelatedData(ctx, stagesDescriptions, options.FilterStagesAndProcessRelatedDataOptions)
if err != nil {
return fmt.Errorf("error filtering local docker server stages: %s", err)
}
stagesDescriptions = filteredStagesDescriptions
}
return parallel.DoTasks(ctx, len(stagesDescriptions), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
InitDockerCLIForEachWorker: true,
}, func(ctx context.Context, taskId int) error {
stageDescription := stagesDescriptions[taskId]
for _, cacheStagesStorage := range m.CacheStagesStorageList {
if err := cacheStagesStorage.DeleteStage(ctx, stageDescription, options.DeleteImageOptions); err != nil {
logboek.Context(ctx).Warn().LogF("Unable to delete stage %s from the cache stages storage %s: %s\n", stageDescription.StageID.String(), cacheStagesStorage.String(), err)
}
}
err := m.StagesStorage.DeleteStage(ctx, stageDescription, options.DeleteImageOptions)
return f(ctx, stageDescription, err)
})
}
func (m *StorageManager) LockStageImage(ctx context.Context, imageName string) error {
imageLockName := container_runtime.ImageLockName(imageName)
_, lock, err := werf.AcquireHostLock(ctx, imageLockName, lockgate.AcquireOptions{Shared: true})
if err != nil {
return fmt.Errorf("error locking %q shared lock: %s", imageLockName, err)
}
m.SharedHostImagesLocks = append(m.SharedHostImagesLocks, lock)
return nil
}
func doFetchStage(ctx context.Context, projectName string, stagesStorage storage.StagesStorage, stageID image.StageID, img container_runtime.LegacyImageInterface) error {
err := logboek.Context(ctx).Info().LogProcess("Check manifest availability").DoError(func() error {
freshStageDescription, err := stagesStorage.GetStageDescription(ctx, projectName, stageID.Digest, stageID.UniqueID)
if err != nil {
return fmt.Errorf("unable to get stage description: %s", err)
}
if freshStageDescription == nil {
return ErrStageNotFound
}
img.SetStageDescription(freshStageDescription)
return nil
})
if err != nil {
return err
}
return logboek.Context(ctx).Info().LogProcess("Fetch image").DoError(func() error {
logboek.Context(ctx).Debug().LogF("Image name: %s\n", img.Name())
if err := stagesStorage.FetchImage(ctx, img); err != nil {
return fmt.Errorf("unable to fetch stage %s image %s: %s", stageID.String(), img.Name(), err)
}
return nil
})
}
func copyStageIntoStagesStorage(ctx context.Context, projectName string, stageID image.StageID, img container_runtime.LegacyImageInterface, stagesStorage storage.StagesStorage, containerRuntime container_runtime.ContainerRuntime) error {
targetStagesStorageImageName := stagesStorage.ConstructStageImageName(projectName, stageID.Digest, stageID.UniqueID)
if err := containerRuntime.RenameImage(ctx, img, targetStagesStorageImageName, false); err != nil {
return fmt.Errorf("unable to rename image %s to %s: %s", img.Name(), targetStagesStorageImageName, err)
}
if err := stagesStorage.StoreImage(ctx, img); err != nil {
return fmt.Errorf("unable to store stage %s into the cache stages storage %s: %s", stageID.String(), stagesStorage.String(), err)
}
if err := storeStageDescriptionIntoLocalManifestCache(ctx, projectName, stageID, stagesStorage, convertStageDescriptionForStagesStorage(img.GetStageDescription(), stagesStorage)); err != nil {
return fmt.Errorf("error storing stage %s description into local manifest cache: %s", targetStagesStorageImageName, err)
}
if err := lrumeta.CommonLRUImagesCache.AccessImage(ctx, targetStagesStorageImageName); err != nil {
return fmt.Errorf("error accessing last recently used images cache for %s: %s", targetStagesStorageImageName, err)
}
return nil
}
func (m *StorageManager) FetchStage(ctx context.Context, containerRuntime container_runtime.ContainerRuntime, stg stage.Interface) error {
logboek.Context(ctx).Debug().LogF("-- StagesManager.FetchStage %s\n", stg.LogDetailedName())
if err := m.LockStageImage(ctx, stg.GetImage().Name()); err != nil {
return fmt.Errorf("error locking stage image %q: %s", stg.GetImage().Name(), err)
}
shouldFetch, err := m.StagesStorage.ShouldFetchImage(ctx, stg.GetImage())
if err != nil {
return fmt.Errorf("error checking should fetch image: %s", err)
}
if !shouldFetch {
imageName := m.StagesStorage.ConstructStageImageName(m.ProjectName, stg.GetImage().GetStageDescription().StageID.Digest, stg.GetImage().GetStageDescription().StageID.UniqueID)
logboek.Context(ctx).Info().LogF("Image %s exists, will not perform fetch\n", imageName)
if err := lrumeta.CommonLRUImagesCache.AccessImage(ctx, imageName); err != nil {
return fmt.Errorf("error accessing last recently used images cache for %s: %s", imageName, err)
}
return nil
}
var fetchedImg container_runtime.LegacyImageInterface
var cacheStagesStorageListToRefill []storage.StagesStorage
fetchStageFromCache := func(stagesStorage storage.StagesStorage) (container_runtime.LegacyImageInterface, error) {
stageID := stg.GetImage().GetStageDescription().StageID
imageName := stagesStorage.ConstructStageImageName(m.ProjectName, stageID.Digest, stageID.UniqueID)
stageImage := container_runtime.NewLegacyStageImage(nil, imageName, containerRuntime.(*container_runtime.DockerServerRuntime))
shouldFetch, err := stagesStorage.ShouldFetchImage(ctx, stageImage)
if err != nil {
return nil, fmt.Errorf("error checking should fetch image from cache repo %s: %s", stagesStorage.String(), err)
}
if shouldFetch {
logboek.Context(ctx).Info().LogF("Cache repo image %s does not exist locally, will perform fetch\n", stageImage.Name())
proc := logboek.Context(ctx).Default().LogProcess("Fetching stage %s from %s", stg.LogDetailedName(), stagesStorage.String())
proc.Start()
err := doFetchStage(ctx, m.ProjectName, stagesStorage, *stageID, stageImage)
if IsErrStageNotFound(err) {
logboek.Context(ctx).Default().LogF("Stage not found\n")
proc.End()
return nil, err
}
if err != nil {
proc.Fail()
return nil, err
}
proc.End()
if err := storeStageDescriptionIntoLocalManifestCache(ctx, m.ProjectName, *stageID, stagesStorage, stageImage.GetStageDescription()); err != nil {
return nil, fmt.Errorf("error storing stage %s description into local manifest cache: %s", imageName, err)
}
} else {
logboek.Context(ctx).Info().LogF("Cache repo image %s exists locally, will not perform fetch\n", stageImage.Name())
}
if err := lrumeta.CommonLRUImagesCache.AccessImage(ctx, stageImage.Name()); err != nil {
return nil, fmt.Errorf("error accessing last recently used images cache for %s: %s", stageImage.Name(), err)
}
return stageImage, nil
}
prepareCacheStageAsPrimary := func(cacheImg container_runtime.LegacyImageInterface, primaryStage stage.Interface) error {
stageID := primaryStage.GetImage().GetStageDescription().StageID
primaryImageName := m.StagesStorage.ConstructStageImageName(m.ProjectName, stageID.Digest, stageID.UniqueID)
// TODO(buildah): check no bugs introduced by removing of following calls
// if err := containerRuntime.RenameImage(ctx, cacheDockerImage, primaryImageName, false); err != nil {
// return fmt.Errorf("unable to rename image %s to %s: %s", fetchedDockerImage.Image.Name(), primaryImageName, err)
// }
// if err := containerRuntime.RefreshImageObject(ctx, &container_runtime.Image{Image: primaryStage.GetImage()}); err != nil {
// return fmt.Errorf("unable to refresh stage image %s: %s", primaryStage.GetImage().Name(), err)
// }
// TODO(buildah): check no bugs introduced by removing of following calls
// if err := storeStageDescriptionIntoLocalManifestCache(ctx, m.ProjectName, *stageID, m.StagesStorage, convertStageDescriptionForStagesStorage(cacheDockerImage.Image.GetStageDescription(), m.StagesStorage)); err != nil {
if err := storeStageDescriptionIntoLocalManifestCache(ctx, m.ProjectName, *stageID, m.StagesStorage, cacheImg.GetStageDescription()); err != nil {
return fmt.Errorf("error storing stage %s description into local manifest cache: %s", primaryImageName, err)
}
if err := lrumeta.CommonLRUImagesCache.AccessImage(ctx, primaryImageName); err != nil {
return fmt.Errorf("error accessing last recently used images cache for %s: %s", primaryImageName, err)
}
return nil
}
for _, cacheStagesStorage := range m.CacheStagesStorageList {
cacheImg, err := fetchStageFromCache(cacheStagesStorage)
if err != nil {
if !IsErrStageNotFound(err) {
logboek.Context(ctx).Warn().LogF("Unable to fetch stage %s from cache stages storage %s: %s\n", stg.GetImage().GetStageDescription().StageID.String(), cacheStagesStorage.String(), err)
}
cacheStagesStorageListToRefill = append(cacheStagesStorageListToRefill, cacheStagesStorage)
continue
}
if err := prepareCacheStageAsPrimary(cacheImg, stg); err != nil {
logboek.Context(ctx).Warn().LogF("Unable to prepare stage %s fetched from cache stages storage %s as a primary: %s\n", cacheImg.Name(), cacheStagesStorage.String(), err)
cacheStagesStorageListToRefill = append(cacheStagesStorageListToRefill, cacheStagesStorage)
continue
}
fetchedImg = cacheImg
break
}
if fetchedImg == nil {
stageID := stg.GetImage().GetStageDescription().StageID
img := stg.GetImage()
err := logboek.Context(ctx).Default().LogProcess("Fetching stage %s from %s", stg.LogDetailedName(), m.StagesStorage.String()).
DoError(func() error {
return doFetchStage(ctx, m.ProjectName, m.StagesStorage, *stageID, img)
})
if IsErrStageNotFound(err) {
logboek.Context(ctx).Error().LogF("Stage is no longer available in the %q!\n", stg.LogDetailedName(), stg.GetImage().Name(), m.StagesStorage.String(), m.ProjectName)
return ErrUnexpectedStagesStorageState
}
if storage.IsErrBrokenImage(err) {
logboek.Context(ctx).Error().LogF("Invalid stage %q!\n", stg.LogDetailedName(), stg.GetImage().Name(), m.StagesStorage.String(), m.ProjectName)
logboek.Context(ctx).Error().LogF("Will mark image %q as rejected in the stages storage %s\n", stg.GetImage().Name(), m.StagesStorage.String())
if err := m.StagesStorage.RejectStage(ctx, m.ProjectName, stageID.Digest, stageID.UniqueID); err != nil {
return fmt.Errorf("unable to reject stage %s image %s in the stages storage %s: %s", stg.LogDetailedName(), stg.GetImage().Name(), m.StagesStorage.String(), err)
}
return ErrUnexpectedStagesStorageState
}
if err != nil {
return fmt.Errorf("unable to fetch stage %s from stages storage %s: %s", stageID.String(), m.StagesStorage.String(), err)
}
fetchedImg = img
}
for _, cacheStagesStorage := range cacheStagesStorageListToRefill {
stageID := stg.GetImage().GetStageDescription().StageID
err := logboek.Context(ctx).Default().LogProcess("Copy stage %s into cache %s", stg.LogDetailedName(), cacheStagesStorage.String()).
DoError(func() error {
if err := copyStageIntoStagesStorage(ctx, m.ProjectName, *stageID, fetchedImg, cacheStagesStorage, containerRuntime); err != nil {
return fmt.Errorf("unable to copy stage %s into cache stages storage %s: %s", stageID.String(), cacheStagesStorage.String(), err)
}
return nil
})
if err != nil {
logboek.Context(ctx).Warn().LogF("Warning %s\n", err)
}
}
return nil
}
func (m *StorageManager) CopyStageIntoCacheStorages(ctx context.Context, stg stage.Interface, containerRuntime container_runtime.ContainerRuntime) error {
for _, cacheStagesStorage := range m.CacheStagesStorageList {
stageID := stg.GetImage().GetStageDescription().StageID
img := stg.GetImage()
err := logboek.Context(ctx).Default().LogProcess("Copy stage %s into cache %s", stg.LogDetailedName(), cacheStagesStorage.String()).
DoError(func() error {
if err := copyStageIntoStagesStorage(ctx, m.ProjectName, *stageID, img, cacheStagesStorage, containerRuntime); err != nil {
return fmt.Errorf("unable to copy stage %s into cache stages storage %s: %s", stageID.String(), cacheStagesStorage.String(), err)
}
return nil
})
if err != nil {
logboek.Context(ctx).Warn().LogF("Warning: %s\n", err)
}
}
return nil
}
func (m *StorageManager) getOrCreateFinalStagesListCache(ctx context.Context) (*StagesList, error) {
m.FinalStagesListCacheMux.Lock()
defer m.FinalStagesListCacheMux.Unlock()
if m.FinalStagesListCache != nil {
return m.FinalStagesListCache, nil
}
stageIDs, err := m.FinalStagesStorage.GetStagesIDs(ctx, m.ProjectName)
if err != nil {
return nil, fmt.Errorf("unable to get final repo stages list: %s", err)
}
m.FinalStagesListCache = NewStagesList(stageIDs)
return m.FinalStagesListCache, nil
}
type CopyStageIntoFinalStorageOptions struct {
ShouldBeBuiltMode bool
}
func (m *StorageManager) CopyStageIntoFinalStorage(ctx context.Context, stg stage.Interface, containerRuntime container_runtime.ContainerRuntime, opts CopyStageIntoFinalStorageOptions) error {
existingStagesListCache, err := m.getOrCreateFinalStagesListCache(ctx)
if err != nil {
return fmt.Errorf("error getting existing stages list of final repo %s: %s", m.FinalStagesStorage.String(), err)
}
logboek.Context(ctx).Debug().LogF("[%p] Got existing final stages list cache: %#v\n", m, existingStagesListCache.StageIDs)
stageID := stg.GetImage().GetStageDescription().StageID
finalImageName := m.FinalStagesStorage.ConstructStageImageName(m.ProjectName, stageID.Digest, stageID.UniqueID)
for _, existingStg := range existingStagesListCache.GetStageIDs() {
if existingStg.IsEqual(*stageID) {
logboek.Context(ctx).Info().LogF("Stage %s already exists in the final repo, skipping\n", stageID.String())
logboek.Context(ctx).Default().LogFHighlight("Use cache final image for %s\n", stg.LogDetailedName())
container_runtime.LogImageName(ctx, finalImageName)
return nil
}
}
if opts.ShouldBeBuiltMode {
return fmt.Errorf("%s with digest %s is not exist in the final repo", stg.LogDetailedName(), stg.GetDigest())
}
if err := m.FetchStage(ctx, containerRuntime, stg); err != nil {
return fmt.Errorf("unable to fetch stage %s: %s", stg.LogDetailedName(), err)
}
img := stg.GetImage()
err = logboek.Context(ctx).Default().LogProcess("Copy stage %s into the final repo", stg.LogDetailedName()).
Options(func(options types.LogProcessOptionsInterface) {
options.Style(style.Highlight())
}).
DoError(func() error {
if err := copyStageIntoStagesStorage(ctx, m.ProjectName, *stageID, img, m.FinalStagesStorage, containerRuntime); err != nil {
return fmt.Errorf("unable to copy stage %s into the final repo %s: %s", stageID.String(), m.FinalStagesStorage.String(), err)
}
logboek.Context(ctx).Default().LogFDetails(" name: %s\n", finalImageName)
return nil
})
if err != nil {
return err
}
existingStagesListCache.AddStageID(*stageID)
logboek.Context(ctx).Debug().LogF("Updated existing final stages list: %#v\n", m.FinalStagesListCache.StageIDs)
return nil
}
func (m *StorageManager) SelectSuitableStage(ctx context.Context, c stage.Conveyor, stg stage.Interface, stages []*image.StageDescription) (*image.StageDescription, error) {
if len(stages) == 0 {
return nil, nil
}
var stageDesc *image.StageDescription
if err := logboek.Context(ctx).Info().LogProcess("Selecting suitable image for stage %s by digest %s", stg.Name(), stg.GetDigest()).
DoError(func() error {
var err error
stageDesc, err = stg.SelectSuitableStage(ctx, c, stages)
return err
}); err != nil {
return nil, err
}
if stageDesc == nil {
return nil, nil
}
imgInfoData, err := yaml.Marshal(stageDesc)
if err != nil {
panic(err)
}
logboek.Context(ctx).Debug().LogBlock("Selected cache image").
Options(func(options types.LogBlockOptionsInterface) {
options.Style(style.Highlight())
}).
Do(func() {
logboek.Context(ctx).Debug().LogF(string(imgInfoData))
})
return stageDesc, nil
}
func (m *StorageManager) GetStagesByDigestWithCache(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error) {
return m.GetStagesByDigestFromStagesStorageWithCache(ctx, stageName, stageDigest, m.StagesStorage)
}
func (m *StorageManager) GetStagesByDigest(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error) {
return m.GetStagesByDigestFromStagesStorage(ctx, stageName, stageDigest, m.StagesStorage)
}
func (m *StorageManager) GetStagesByDigestFromStagesStorageWithCache(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage) ([]*image.StageDescription, error) {
cachedStageDescriptionList, err := m.getStagesByDigestFromStagesStorage(ctx, stageName, stageDigest, stagesStorage, storage.WithCache())
if err != nil {
return nil, err
}
if len(cachedStageDescriptionList) != 0 {
return cachedStageDescriptionList, nil
}
return m.getStagesByDigestFromStagesStorage(ctx, stageName, stageDigest, stagesStorage)
}
func (m *StorageManager) GetStagesByDigestFromStagesStorage(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage) ([]*image.StageDescription, error) {
return m.getStagesByDigestFromStagesStorage(ctx, stageName, stageDigest, stagesStorage)
}
func (m *StorageManager) getStagesByDigestFromStagesStorage(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage, opts ...storage.Option) ([]*image.StageDescription, error) {
stageIDs, err := m.getStagesIDsByDigestFromStagesStorage(ctx, stageName, stageDigest, stagesStorage, opts...)
if err != nil {
return nil, fmt.Errorf("unable to get stages ids from %s by digest %s for stage %s: %s", stagesStorage.String(), stageDigest, stageName, err)
}
stages, err := m.getStagesDescriptions(ctx, stageIDs, stagesStorage, m.CacheStagesStorageList)
if err != nil {
return nil, fmt.Errorf("unable to get stage descriptions by ids from %s: %s", stagesStorage.String(), err)
}
return stages, nil
}
func (m *StorageManager) CopySuitableByDigestStage(ctx context.Context, stageDesc *image.StageDescription, sourceStagesStorage, destinationStagesStorage storage.StagesStorage, containerRuntime container_runtime.ContainerRuntime) (*image.StageDescription, error) {
img := container_runtime.NewLegacyStageImage(nil, stageDesc.Info.Name, containerRuntime.(*container_runtime.DockerServerRuntime))
logboek.Context(ctx).Info().LogF("Fetching %s\n", img.Name())
if err := sourceStagesStorage.FetchImage(ctx, img); err != nil {
return nil, fmt.Errorf("unable to fetch %s from %s: %s", stageDesc.Info.Name, sourceStagesStorage.String(), err)
}
newImageName := destinationStagesStorage.ConstructStageImageName(m.ProjectName, stageDesc.StageID.Digest, stageDesc.StageID.UniqueID)
logboek.Context(ctx).Info().LogF("Renaming image %s to %s\n", img.Name(), newImageName)
if err := containerRuntime.RenameImage(ctx, img, newImageName, false); err != nil {
return nil, err
}
logboek.Context(ctx).Info().LogF("Storing %s\n", newImageName)
if err := destinationStagesStorage.StoreImage(ctx, img); err != nil {
return nil, fmt.Errorf("unable to store %s to %s: %s", stageDesc.Info.Name, destinationStagesStorage.String(), err)
}
if destinationStageDesc, err := getStageDescription(ctx, m.ProjectName, *stageDesc.StageID, destinationStagesStorage, m.CacheStagesStorageList, getStageDescriptionOptions{WithLocalManifestCache: m.getWithLocalManifestCacheOption()}); err != nil {
return nil, fmt.Errorf("unable to get stage %s description from %s: %s", stageDesc.StageID.String(), destinationStagesStorage.String(), err)
} else {
return destinationStageDesc, nil
}
}
func (m *StorageManager) getWithLocalManifestCacheOption() bool {
return m.StagesStorage.Address() != storage.LocalStorageAddress
}
func (m *StorageManager) getStagesIDsByDigestFromStagesStorage(ctx context.Context, stageName, stageDigest string, stagesStorage storage.StagesStorage, opts ...storage.Option) ([]image.StageID, error) {
var stageIDs []image.StageID
if err := logboek.Context(ctx).Info().LogProcess("Get %s stages by digest %s from storage", stageName, stageDigest).
DoError(func() error {
var err error
stageIDs, err = stagesStorage.GetStagesIDsByDigest(ctx, m.ProjectName, stageDigest, opts...)
if err != nil {
return fmt.Errorf("error getting project %s stage %s images from storage: %s", m.StagesStorage.String(), stageDigest, err)
}
logboek.Context(ctx).Debug().LogF("Stages ids: %#v\n", stageIDs)
return nil
}); err != nil {
return nil, err
}
return stageIDs, nil
}
func (m *StorageManager) getStagesDescriptions(ctx context.Context, stageIDs []image.StageID, stagesStorage storage.StagesStorage, cacheStagesStorageList []storage.StagesStorage) ([]*image.StageDescription, error) {
var stages []*image.StageDescription
for _, stageID := range stageIDs {
stageDesc, err := getStageDescription(ctx, m.ProjectName, stageID, stagesStorage, cacheStagesStorageList, getStageDescriptionOptions{WithLocalManifestCache: m.getWithLocalManifestCacheOption()})
if err != nil {
return nil, err
}
if stageDesc == nil {
logboek.Context(ctx).Warn().LogF("Ignoring stage %s: cannot get stage description from %s\n", stageID.String(), m.StagesStorage.String())
continue
}
stages = append(stages, stageDesc)
}
return stages, nil
}
type getStageDescriptionOptions struct {
WithLocalManifestCache bool
}
func getStageDescriptionFromLocalManifestCache(ctx context.Context, projectName string, stageID image.StageID, stagesStorage storage.StagesStorage) (*image.StageDescription, error) {
stageImageName := stagesStorage.ConstructStageImageName(projectName, stageID.Digest, stageID.UniqueID)
logboek.Context(ctx).Debug().LogF("Getting image %s info from the manifest cache...\n", stageImageName)
imgInfo, err := image.CommonManifestCache.GetImageInfo(ctx, stagesStorage.String(), stageImageName)
if err != nil {
return nil, fmt.Errorf("error getting image %s info: %s", stageImageName, err)
}
if imgInfo != nil {
logboek.Context(ctx).Info().LogF("Got image %s info from the manifest cache (CACHE HIT)\n", stageImageName)
return &image.StageDescription{
StageID: &image.StageID{Digest: stageID.Digest, UniqueID: stageID.UniqueID},
Info: imgInfo,
}, nil
} else {
logboek.Context(ctx).Info().LogF("Not found %s image info in the manifest cache (CACHE MISS)\n", stageImageName)
}
return nil, nil
}
func storeStageDescriptionIntoLocalManifestCache(ctx context.Context, projectName string, stageID image.StageID, stagesStorage storage.StagesStorage, stageDesc *image.StageDescription) error {
stageImageName := stagesStorage.ConstructStageImageName(projectName, stageID.Digest, stageID.UniqueID)
logboek.Context(ctx).Debug().LogF("Storing image %s info into manifest cache\n", stageImageName)
if err := image.CommonManifestCache.StoreImageInfo(ctx, stagesStorage.String(), stageDesc.Info); err != nil {
return fmt.Errorf("error storing image %s info: %s", stageImageName, err)
}
return nil
}
func convertStageDescriptionForStagesStorage(stageDesc *image.StageDescription, stagesStorage storage.StagesStorage) *image.StageDescription {
return &image.StageDescription{
StageID: &image.StageID{
Digest: stageDesc.StageID.Digest,
UniqueID: stageDesc.StageID.UniqueID,
},
Info: &image.Info{
Name: fmt.Sprintf("%s:%s-%d", stagesStorage.Address(), stageDesc.StageID.Digest, stageDesc.StageID.UniqueID),
Repository: stagesStorage.Address(),
Tag: stageDesc.Info.Tag,
RepoDigest: stageDesc.Info.RepoDigest,
ID: stageDesc.Info.ID,
ParentID: stageDesc.Info.ParentID,
Labels: stageDesc.Info.Labels,
Size: stageDesc.Info.Size,
CreatedAtUnixNano: stageDesc.Info.CreatedAtUnixNano,
},
}
}
func getStageDescription(ctx context.Context, projectName string, stageID image.StageID, stagesStorage storage.StagesStorage, cacheStagesStorageList []storage.StagesStorage, opts getStageDescriptionOptions) (*image.StageDescription, error) {
if opts.WithLocalManifestCache {
stageDesc, err := getStageDescriptionFromLocalManifestCache(ctx, projectName, stageID, stagesStorage)
if err != nil {
return nil, fmt.Errorf("error getting stage %s description from %s: %s", stageID.String(), stagesStorage.String(), err)
}
if stageDesc != nil {
return stageDesc, nil
}
}
for _, cacheStagesStorage := range cacheStagesStorageList {
if opts.WithLocalManifestCache {
stageDesc, err := getStageDescriptionFromLocalManifestCache(ctx, projectName, stageID, cacheStagesStorage)
if err != nil {
return nil, fmt.Errorf("error getting stage %s description from the local manifest cache: %s", stageID.String(), err)
}
if stageDesc != nil {
return convertStageDescriptionForStagesStorage(stageDesc, stagesStorage), nil
}
}
var stageDesc *image.StageDescription
err := logboek.Context(ctx).Info().LogProcess("Get stage %s description from cache stages storage %s", stageID.String(), cacheStagesStorage.String()).
DoError(func() error {
var err error
stageDesc, err = cacheStagesStorage.GetStageDescription(ctx, projectName, stageID.Digest, stageID.UniqueID)
logboek.Context(ctx).Debug().LogF("Got stage description: %#v\n", stageDesc)
return err
})
if err != nil {
logboek.Context(ctx).Warn().LogF("Unable to get stage description from cache stages storage %s: %s\n", cacheStagesStorage.String(), err)
continue
}
if stageDesc != nil {
if opts.WithLocalManifestCache {
if err := storeStageDescriptionIntoLocalManifestCache(ctx, projectName, stageID, cacheStagesStorage, stageDesc); err != nil {
return nil, fmt.Errorf("error storing stage %s description into local manifest cache: %s", stageID.String(), err)
}
}
return convertStageDescriptionForStagesStorage(stageDesc, stagesStorage), nil
}
}
logboek.Context(ctx).Debug().LogF("Getting digest %q uniqueID %d stage info from %s...\n", stageID.Digest, stageID.UniqueID, stagesStorage.String())
stageDesc, err := stagesStorage.GetStageDescription(ctx, projectName, stageID.Digest, stageID.UniqueID)
switch {
case storage.IsErrBrokenImage(err):
return nil, nil
case err != nil:
return nil, fmt.Errorf("error getting digest %q uniqueID %d stage info from %s: %s", stageID.Digest, stageID.UniqueID, stagesStorage.String(), err)
case stageDesc != nil:
if opts.WithLocalManifestCache {
if err := storeStageDescriptionIntoLocalManifestCache(ctx, projectName, stageID, stagesStorage, stageDesc); err != nil {
return nil, fmt.Errorf("error storing stage %s description into local manifest cache: %s", stageID.String(), err)
}
}
return stageDesc, nil
default:
return nil, nil
}
}
func (m *StorageManager) GenerateStageUniqueID(digest string, stages []*image.StageDescription) (string, int64) {
var imageName string
for {
timeNow := time.Now().UTC()
uniqueID := timeNow.Unix()*1000 + int64(timeNow.Nanosecond()/1000000)
imageName = m.StagesStorage.ConstructStageImageName(m.ProjectName, digest, uniqueID)
for _, stageDesc := range stages {
if stageDesc.Info.Name == imageName {
continue
}
}
return imageName, uniqueID
}
}
type rmImageMetadataTask struct {
commit string
stageID string
}
func (m *StorageManager) ForEachRmImageMetadata(ctx context.Context, projectName, imageNameOrID string, stageIDCommitList map[string][]string, f func(ctx context.Context, commit, stageID string, err error) error) error {
var tasks []rmImageMetadataTask
for stageID, commitList := range stageIDCommitList {
for _, commit := range commitList {
tasks = append(tasks, rmImageMetadataTask{
commit: commit,
stageID: stageID,
})
}
}
return parallel.DoTasks(ctx, len(tasks), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
}, func(ctx context.Context, taskId int) error {
task := tasks[taskId]
err := m.StagesStorage.RmImageMetadata(ctx, projectName, imageNameOrID, task.commit, task.stageID)
return f(ctx, task.commit, task.stageID, err)
})
}
func (m *StorageManager) ForEachRmManagedImage(ctx context.Context, projectName string, managedImages []string, f func(ctx context.Context, managedImage string, err error) error) error {
return parallel.DoTasks(ctx, len(managedImages), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
}, func(ctx context.Context, taskId int) error {
managedImage := managedImages[taskId]
err := m.StagesStorage.RmManagedImage(ctx, projectName, managedImage)
return f(ctx, managedImage, err)
})
}
func (m *StorageManager) ForEachGetImportMetadata(ctx context.Context, projectName string, ids []string, f func(ctx context.Context, metadataID string, metadata *storage.ImportMetadata, err error) error) error {
return parallel.DoTasks(ctx, len(ids), parallel.DoTasksOptions{
MaxNumberOfWorkers: m.MaxNumberOfWorkers(),
}, func(ctx context.Context, taskId int) error {
id := ids[taskId]
metadata, err := m.StagesStorage.GetImportMetadata(ctx, projectName, id)
return f(ctx, id, metadata, err)
})
}