@@ -19,36 +19,77 @@ import (
19
19
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
20
20
)
21
21
22
+ const ExistingOperatorKey = "@existing"
23
+
24
+ type SourceKey struct {
25
+ Name string
26
+ Namespace string
27
+ }
28
+
29
+ func (k * SourceKey ) String () string {
30
+ return fmt .Sprintf ("%s/%s" , k .Name , k .Namespace )
31
+ }
32
+
33
+ func (k * SourceKey ) Empty () bool {
34
+ return k .Name == "" && k .Namespace == ""
35
+ }
36
+
37
+ func (k * SourceKey ) Equal (compare SourceKey ) bool {
38
+ return k .Name == compare .Name && k .Namespace == compare .Namespace
39
+ }
40
+
41
+ // Virtual indicates if this is a "virtual" catalog representing the currently installed operators in a namespace
42
+ func (k * SourceKey ) Virtual () bool {
43
+ return k .Name == ExistingOperatorKey && k .Namespace != ""
44
+ }
45
+
46
+ func NewVirtualSourceKey (namespace string ) SourceKey {
47
+ return SourceKey {
48
+ Name : ExistingOperatorKey ,
49
+ Namespace : namespace ,
50
+ }
51
+ }
52
+
22
53
type RegistryClientProvider interface {
23
54
ClientsForNamespaces (namespaces ... string ) map [registry.CatalogKey ]client.Interface
24
55
}
25
56
57
+ type SourceProvider interface {
58
+ // TODO: Spun off from the old RegistryClientProvider in order
59
+ // to phase out the dependency on registry.CatalogKey. Subject
60
+ // to further change as the registry client dependency is also
61
+ // removed.
62
+ ClientsForNamespaces (namespaces ... string ) map [SourceKey ]client.Interface
63
+ }
64
+
26
65
type DefaultRegistryClientProvider struct {
27
- logger logrus.FieldLogger
28
- s RegistryClientProvider
66
+ s RegistryClientProvider
29
67
}
30
68
31
- func NewDefaultRegistryClientProvider ( log logrus. FieldLogger , store RegistryClientProvider ) * DefaultRegistryClientProvider {
69
+ func SourceProviderFromRegistryClientProvider ( store RegistryClientProvider ) * DefaultRegistryClientProvider {
32
70
return & DefaultRegistryClientProvider {
33
- logger : log ,
34
- s : store ,
71
+ s : store ,
35
72
}
36
73
}
37
74
38
- func (rcp * DefaultRegistryClientProvider ) ClientsForNamespaces (namespaces ... string ) map [registry.CatalogKey ]client.Interface {
39
- return rcp .s .ClientsForNamespaces (namespaces ... )
75
+ func (rcp * DefaultRegistryClientProvider ) ClientsForNamespaces (namespaces ... string ) map [SourceKey ]client.Interface {
76
+ result := make (map [SourceKey ]client.Interface )
77
+ for key , client := range rcp .s .ClientsForNamespaces (namespaces ... ) {
78
+ result [SourceKey (key )] = client
79
+ }
80
+ return result
40
81
}
41
82
42
83
type OperatorCacheProvider interface {
43
84
Namespaced (namespaces ... string ) MultiCatalogOperatorFinder
44
- Expire (catalog registry. CatalogKey )
85
+ Expire (catalog SourceKey )
45
86
}
46
87
47
88
type OperatorCache struct {
48
89
logger logrus.FieldLogger
49
- rcp RegistryClientProvider
90
+ rcp SourceProvider
50
91
catsrcLister v1alpha1.CatalogSourceLister
51
- snapshots map [registry. CatalogKey ]* CatalogSnapshot
92
+ snapshots map [SourceKey ]* CatalogSnapshot
52
93
ttl time.Duration
53
94
sem chan struct {}
54
95
m sync.RWMutex
@@ -60,7 +101,7 @@ type catalogSourcePriority int
60
101
61
102
var _ OperatorCacheProvider = & OperatorCache {}
62
103
63
- func NewOperatorCache (rcp RegistryClientProvider , log logrus.FieldLogger , catsrcLister v1alpha1.CatalogSourceLister ) * OperatorCache {
104
+ func NewOperatorCache (rcp SourceProvider , log logrus.FieldLogger , catsrcLister v1alpha1.CatalogSourceLister ) * OperatorCache {
64
105
const (
65
106
MaxConcurrentSnapshotUpdates = 4
66
107
)
@@ -69,16 +110,16 @@ func NewOperatorCache(rcp RegistryClientProvider, log logrus.FieldLogger, catsrc
69
110
logger : log ,
70
111
rcp : rcp ,
71
112
catsrcLister : catsrcLister ,
72
- snapshots : make (map [registry. CatalogKey ]* CatalogSnapshot ),
113
+ snapshots : make (map [SourceKey ]* CatalogSnapshot ),
73
114
ttl : 5 * time .Minute ,
74
115
sem : make (chan struct {}, MaxConcurrentSnapshotUpdates ),
75
116
}
76
117
}
77
118
78
119
type NamespacedOperatorCache struct {
79
120
Namespaces []string
80
- existing * registry. CatalogKey
81
- Snapshots map [registry. CatalogKey ]* CatalogSnapshot
121
+ existing * SourceKey
122
+ Snapshots map [SourceKey ]* CatalogSnapshot
82
123
}
83
124
84
125
func (c * NamespacedOperatorCache ) Error () error {
@@ -94,7 +135,7 @@ func (c *NamespacedOperatorCache) Error() error {
94
135
return errors .NewAggregate (errs )
95
136
}
96
137
97
- func (c * OperatorCache ) Expire (catalog registry. CatalogKey ) {
138
+ func (c * OperatorCache ) Expire (catalog SourceKey ) {
98
139
c .m .Lock ()
99
140
defer c .m .Unlock ()
100
141
s , ok := c .snapshots [catalog ]
@@ -114,10 +155,10 @@ func (c *OperatorCache) Namespaced(namespaces ...string) MultiCatalogOperatorFin
114
155
115
156
result := NamespacedOperatorCache {
116
157
Namespaces : namespaces ,
117
- Snapshots : make (map [registry. CatalogKey ]* CatalogSnapshot ),
158
+ Snapshots : make (map [SourceKey ]* CatalogSnapshot ),
118
159
}
119
160
120
- var misses []registry. CatalogKey
161
+ var misses []SourceKey
121
162
func () {
122
163
c .m .RLock ()
123
164
defer c .m .RUnlock ()
@@ -148,7 +189,7 @@ func (c *OperatorCache) Namespaced(namespaces ...string) MultiCatalogOperatorFin
148
189
defer c .m .Unlock ()
149
190
150
191
// Take the opportunity to clear expired snapshots while holding the lock.
151
- var expired []registry. CatalogKey
192
+ var expired []SourceKey
152
193
for key , snapshot := range c .snapshots {
153
194
if snapshot .Expired (now ) {
154
195
snapshot .Cancel ()
@@ -270,7 +311,7 @@ func EnsurePackageProperty(o *Operator, name, version string) {
270
311
})
271
312
}
272
313
273
- func (c * NamespacedOperatorCache ) Catalog (k registry. CatalogKey ) OperatorFinder {
314
+ func (c * NamespacedOperatorCache ) Catalog (k SourceKey ) OperatorFinder {
274
315
// all catalogs match the empty catalog
275
316
if k .Empty () {
276
317
return c
@@ -281,7 +322,7 @@ func (c *NamespacedOperatorCache) Catalog(k registry.CatalogKey) OperatorFinder
281
322
return EmptyOperatorFinder {}
282
323
}
283
324
284
- func (c * NamespacedOperatorCache ) FindPreferred (preferred * registry. CatalogKey , p ... OperatorPredicate ) []* Operator {
325
+ func (c * NamespacedOperatorCache ) FindPreferred (preferred * SourceKey , p ... OperatorPredicate ) []* Operator {
285
326
var result []* Operator
286
327
if preferred != nil && preferred .Empty () {
287
328
preferred = nil
@@ -310,7 +351,7 @@ func (c *NamespacedOperatorCache) Find(p ...OperatorPredicate) []*Operator {
310
351
311
352
type CatalogSnapshot struct {
312
353
logger logrus.FieldLogger
313
- Key registry. CatalogKey
354
+ Key SourceKey
314
355
expiry time.Time
315
356
Operators []* Operator
316
357
m sync.RWMutex
@@ -329,7 +370,7 @@ func (s *CatalogSnapshot) Expired(at time.Time) bool {
329
370
330
371
// NewRunningOperatorSnapshot creates a CatalogSnapshot that represents a set of existing installed operators
331
372
// in the cluster.
332
- func NewRunningOperatorSnapshot (logger logrus.FieldLogger , key registry. CatalogKey , o []* Operator ) * CatalogSnapshot {
373
+ func NewRunningOperatorSnapshot (logger logrus.FieldLogger , key SourceKey , o []* Operator ) * CatalogSnapshot {
333
374
return & CatalogSnapshot {
334
375
logger : logger ,
335
376
Key : key ,
@@ -340,11 +381,11 @@ func NewRunningOperatorSnapshot(logger logrus.FieldLogger, key registry.CatalogK
340
381
type SortableSnapshots struct {
341
382
snapshots []* CatalogSnapshot
342
383
namespaces map [string ]int
343
- preferred * registry. CatalogKey
344
- existing * registry. CatalogKey
384
+ preferred * SourceKey
385
+ existing * SourceKey
345
386
}
346
387
347
- func NewSortableSnapshots (existing , preferred * registry. CatalogKey , namespaces []string , snapshots map [registry. CatalogKey ]* CatalogSnapshot ) SortableSnapshots {
388
+ func NewSortableSnapshots (existing , preferred * SourceKey , namespaces []string , snapshots map [SourceKey ]* CatalogSnapshot ) SortableSnapshots {
348
389
sorted := SortableSnapshots {
349
390
existing : existing ,
350
391
preferred : preferred ,
@@ -421,8 +462,8 @@ type OperatorFinder interface {
421
462
}
422
463
423
464
type MultiCatalogOperatorFinder interface {
424
- Catalog (registry. CatalogKey ) OperatorFinder
425
- FindPreferred (* registry. CatalogKey , ... OperatorPredicate ) []* Operator
465
+ Catalog (SourceKey ) OperatorFinder
466
+ FindPreferred (* SourceKey , ... OperatorPredicate ) []* Operator
426
467
WithExistingOperators (* CatalogSnapshot ) MultiCatalogOperatorFinder
427
468
Error () error
428
469
OperatorFinder
0 commit comments