-
Notifications
You must be signed in to change notification settings - Fork 251
/
mirror.go
141 lines (126 loc) · 3.91 KB
/
mirror.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
package quay_io_ci_images_distributor
import (
"context"
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
cioperatorapi "github.com/openshift/ci-tools/pkg/api"
)
type MirrorTask struct {
SourceTagRef cioperatorapi.ImageStreamTagReference `json:"source_tag_ref"`
Source string `json:"source"`
Destination string `json:"destination"`
CurrentQuayDigest string `json:"current_quay_digest"`
CreatedAt time.Time `json:"created_at"`
Stale bool `json:"stale"`
Owner string `json:"owner"`
}
type MirrorStore interface {
Put(t ...MirrorTask) error
Take(n int) ([]MirrorTask, error)
Show(n int) ([]MirrorTask, int, error)
Summarize() (map[string]any, error)
}
type memoryMirrorStore struct {
mu sync.Mutex
mirrors map[string]MirrorTask
}
func (s *memoryMirrorStore) Put(tasks ...MirrorTask) error {
s.mu.Lock()
defer s.mu.Unlock()
for _, t := range tasks {
if t.Source == "" || t.Destination == "" {
logrus.WithField("source", t.Source).WithField("destination", t.Destination).Warn("Skipped an invalid mirror task: both source and destination must be set")
continue
}
t.CreatedAt = time.Now()
s.mirrors[t.Destination] = t
}
SetMirrorQueueDepth(float64(len(s.mirrors)))
return nil
}
func (s *memoryMirrorStore) Take(n int) ([]MirrorTask, error) {
ret, _, nil := s.get(n, true)
return ret, nil
}
func (s *memoryMirrorStore) Show(n int) ([]MirrorTask, int, error) {
return s.get(n, false)
}
func (s *memoryMirrorStore) get(n int, del bool) ([]MirrorTask, int, error) {
var ret []MirrorTask
s.mu.Lock()
defer s.mu.Unlock()
l := len(s.mirrors)
c := 0
for k, v := range s.mirrors {
if c < n {
ret = append(ret, v)
c = c + 1
if del {
delete(s.mirrors, k)
}
} else {
break
}
}
SetMirrorQueueDepth(float64(len(s.mirrors)))
return ret, l, nil
}
func (s *memoryMirrorStore) Summarize() (map[string]any, error) {
return map[string]any{"total": len(s.mirrors)}, nil
}
// NewMirrorStore returns a mirror store
func NewMirrorStore() MirrorStore {
return &memoryMirrorStore{
mirrors: map[string]MirrorTask{},
}
}
type MirrorConsumerController struct {
logger *logrus.Entry
quayIOImageHelper QuayIOImageHelper
mirrorStore MirrorStore
options OCImageMirrorOptions
}
func (c *MirrorConsumerController) Run(ctx context.Context) error {
for {
mirrors, err := c.mirrorStore.Take(10)
if err != nil {
c.logger.WithError(err).Warn("Failed to take mirrors")
continue
}
if len(mirrors) == 0 {
c.logger.Debug("Waiting for mirror tasks ...")
time.Sleep(3 * time.Second)
continue
}
var pairs []string
for _, mirror := range mirrors {
pairs = append(pairs, fmt.Sprintf("%s=%s", mirror.Source, mirror.Destination))
}
if err := wait.PollUntilContextTimeout(ctx, 1*time.Second, 3*time.Minute, true, func(ctx context.Context) (done bool, err error) {
if errFromMirror := c.quayIOImageHelper.ImageMirror(pairs, c.options); errFromMirror != nil {
return false, nil
}
return true, nil
}); err != nil {
// TODO use "--force" on long stale images with errors even after retries, ideally, only for the failed ones
c.logger.WithError(err).Warn("Failed to mirror even with retries")
}
}
}
func NewMirrorConsumer(mirrorStore MirrorStore, quayIOImageHelper QuayIOImageHelper, registryConfig string, dryRun bool) *MirrorConsumerController {
return &MirrorConsumerController{
quayIOImageHelper: quayIOImageHelper,
mirrorStore: mirrorStore,
options: OCImageMirrorOptions{
RegistryConfig: registryConfig,
ContinueOnError: true,
MaxPerRegistry: 20,
BatchSize: 10,
DryRun: dryRun,
},
logger: logrus.WithField("subComponent", "mirrorController"),
}
}