Skip to content

Commit f8f942a

Browse files
committed
Remove resolver dependency on registry.CatalogKey.
Signed-off-by: Ben Luddy <[email protected]>
1 parent 35deef2 commit f8f942a

File tree

14 files changed

+265
-234
lines changed

14 files changed

+265
-234
lines changed

pkg/controller/operators/catalog/operator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc"
5656
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/reconciler"
5757
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver"
58+
resolvercache "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache"
5859
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/solver"
5960
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/catalogsource"
6061
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/clients"
@@ -463,7 +464,7 @@ func (o *Operator) syncSourceState(state grpc.SourceState) {
463464

464465
switch state.State {
465466
case connectivity.Ready:
466-
o.resolver.Expire(state.Key)
467+
o.resolver.Expire(resolvercache.SourceKey(state.Key))
467468
if o.namespace == state.Key.Namespace {
468469
namespaces, err := index.CatalogSubscriberNamespaces(o.catalogSubscriberIndexer,
469470
state.Key.Name, state.Key.Namespace)

pkg/controller/registry/resolver/cache/cache.go

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,77 @@ import (
1919
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
2020
)
2121

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+
2253
type RegistryClientProvider interface {
2354
ClientsForNamespaces(namespaces ...string) map[registry.CatalogKey]client.Interface
2455
}
2556

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+
2665
type DefaultRegistryClientProvider struct {
27-
logger logrus.FieldLogger
28-
s RegistryClientProvider
66+
s RegistryClientProvider
2967
}
3068

31-
func NewDefaultRegistryClientProvider(log logrus.FieldLogger, store RegistryClientProvider) *DefaultRegistryClientProvider {
69+
func SourceProviderFromRegistryClientProvider(store RegistryClientProvider) *DefaultRegistryClientProvider {
3270
return &DefaultRegistryClientProvider{
33-
logger: log,
34-
s: store,
71+
s: store,
3572
}
3673
}
3774

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
4081
}
4182

4283
type OperatorCacheProvider interface {
4384
Namespaced(namespaces ...string) MultiCatalogOperatorFinder
44-
Expire(catalog registry.CatalogKey)
85+
Expire(catalog SourceKey)
4586
}
4687

4788
type OperatorCache struct {
4889
logger logrus.FieldLogger
49-
rcp RegistryClientProvider
90+
rcp SourceProvider
5091
catsrcLister v1alpha1.CatalogSourceLister
51-
snapshots map[registry.CatalogKey]*CatalogSnapshot
92+
snapshots map[SourceKey]*CatalogSnapshot
5293
ttl time.Duration
5394
sem chan struct{}
5495
m sync.RWMutex
@@ -60,7 +101,7 @@ type catalogSourcePriority int
60101

61102
var _ OperatorCacheProvider = &OperatorCache{}
62103

63-
func NewOperatorCache(rcp RegistryClientProvider, log logrus.FieldLogger, catsrcLister v1alpha1.CatalogSourceLister) *OperatorCache {
104+
func NewOperatorCache(rcp SourceProvider, log logrus.FieldLogger, catsrcLister v1alpha1.CatalogSourceLister) *OperatorCache {
64105
const (
65106
MaxConcurrentSnapshotUpdates = 4
66107
)
@@ -69,16 +110,16 @@ func NewOperatorCache(rcp RegistryClientProvider, log logrus.FieldLogger, catsrc
69110
logger: log,
70111
rcp: rcp,
71112
catsrcLister: catsrcLister,
72-
snapshots: make(map[registry.CatalogKey]*CatalogSnapshot),
113+
snapshots: make(map[SourceKey]*CatalogSnapshot),
73114
ttl: 5 * time.Minute,
74115
sem: make(chan struct{}, MaxConcurrentSnapshotUpdates),
75116
}
76117
}
77118

78119
type NamespacedOperatorCache struct {
79120
Namespaces []string
80-
existing *registry.CatalogKey
81-
Snapshots map[registry.CatalogKey]*CatalogSnapshot
121+
existing *SourceKey
122+
Snapshots map[SourceKey]*CatalogSnapshot
82123
}
83124

84125
func (c *NamespacedOperatorCache) Error() error {
@@ -94,7 +135,7 @@ func (c *NamespacedOperatorCache) Error() error {
94135
return errors.NewAggregate(errs)
95136
}
96137

97-
func (c *OperatorCache) Expire(catalog registry.CatalogKey) {
138+
func (c *OperatorCache) Expire(catalog SourceKey) {
98139
c.m.Lock()
99140
defer c.m.Unlock()
100141
s, ok := c.snapshots[catalog]
@@ -114,10 +155,10 @@ func (c *OperatorCache) Namespaced(namespaces ...string) MultiCatalogOperatorFin
114155

115156
result := NamespacedOperatorCache{
116157
Namespaces: namespaces,
117-
Snapshots: make(map[registry.CatalogKey]*CatalogSnapshot),
158+
Snapshots: make(map[SourceKey]*CatalogSnapshot),
118159
}
119160

120-
var misses []registry.CatalogKey
161+
var misses []SourceKey
121162
func() {
122163
c.m.RLock()
123164
defer c.m.RUnlock()
@@ -148,7 +189,7 @@ func (c *OperatorCache) Namespaced(namespaces ...string) MultiCatalogOperatorFin
148189
defer c.m.Unlock()
149190

150191
// Take the opportunity to clear expired snapshots while holding the lock.
151-
var expired []registry.CatalogKey
192+
var expired []SourceKey
152193
for key, snapshot := range c.snapshots {
153194
if snapshot.Expired(now) {
154195
snapshot.Cancel()
@@ -270,7 +311,7 @@ func EnsurePackageProperty(o *Operator, name, version string) {
270311
})
271312
}
272313

273-
func (c *NamespacedOperatorCache) Catalog(k registry.CatalogKey) OperatorFinder {
314+
func (c *NamespacedOperatorCache) Catalog(k SourceKey) OperatorFinder {
274315
// all catalogs match the empty catalog
275316
if k.Empty() {
276317
return c
@@ -281,7 +322,7 @@ func (c *NamespacedOperatorCache) Catalog(k registry.CatalogKey) OperatorFinder
281322
return EmptyOperatorFinder{}
282323
}
283324

284-
func (c *NamespacedOperatorCache) FindPreferred(preferred *registry.CatalogKey, p ...OperatorPredicate) []*Operator {
325+
func (c *NamespacedOperatorCache) FindPreferred(preferred *SourceKey, p ...OperatorPredicate) []*Operator {
285326
var result []*Operator
286327
if preferred != nil && preferred.Empty() {
287328
preferred = nil
@@ -310,7 +351,7 @@ func (c *NamespacedOperatorCache) Find(p ...OperatorPredicate) []*Operator {
310351

311352
type CatalogSnapshot struct {
312353
logger logrus.FieldLogger
313-
Key registry.CatalogKey
354+
Key SourceKey
314355
expiry time.Time
315356
Operators []*Operator
316357
m sync.RWMutex
@@ -329,7 +370,7 @@ func (s *CatalogSnapshot) Expired(at time.Time) bool {
329370

330371
// NewRunningOperatorSnapshot creates a CatalogSnapshot that represents a set of existing installed operators
331372
// 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 {
333374
return &CatalogSnapshot{
334375
logger: logger,
335376
Key: key,
@@ -340,11 +381,11 @@ func NewRunningOperatorSnapshot(logger logrus.FieldLogger, key registry.CatalogK
340381
type SortableSnapshots struct {
341382
snapshots []*CatalogSnapshot
342383
namespaces map[string]int
343-
preferred *registry.CatalogKey
344-
existing *registry.CatalogKey
384+
preferred *SourceKey
385+
existing *SourceKey
345386
}
346387

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 {
348389
sorted := SortableSnapshots{
349390
existing: existing,
350391
preferred: preferred,
@@ -421,8 +462,8 @@ type OperatorFinder interface {
421462
}
422463

423464
type MultiCatalogOperatorFinder interface {
424-
Catalog(registry.CatalogKey) OperatorFinder
425-
FindPreferred(*registry.CatalogKey, ...OperatorPredicate) []*Operator
465+
Catalog(SourceKey) OperatorFinder
466+
FindPreferred(*SourceKey, ...OperatorPredicate) []*Operator
426467
WithExistingOperators(*CatalogSnapshot) MultiCatalogOperatorFinder
427468
Error() error
428469
OperatorFinder

pkg/controller/registry/resolver/cache/cache_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
1717

18-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1918
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
2019
"github.com/operator-framework/operator-registry/pkg/api"
2120
"github.com/operator-framework/operator-registry/pkg/client"
@@ -77,9 +76,9 @@ func (s *RegistryClientStub) Close() error {
7776
return nil
7877
}
7978

80-
type RegistryClientProviderStub map[registry.CatalogKey]client.Interface
79+
type RegistryClientProviderStub map[SourceKey]client.Interface
8180

82-
func (s RegistryClientProviderStub) ClientsForNamespaces(namespaces ...string) map[registry.CatalogKey]client.Interface {
81+
func (s RegistryClientProviderStub) ClientsForNamespaces(namespaces ...string) map[SourceKey]client.Interface {
8382
return s
8483
}
8584

@@ -89,10 +88,10 @@ func TestOperatorCacheConcurrency(t *testing.T) {
8988
)
9089
rcp := RegistryClientProviderStub{}
9190
catsrcLister := operatorlister.NewLister().OperatorsV1alpha1().CatalogSourceLister()
92-
var keys []registry.CatalogKey
91+
var keys []SourceKey
9392
for i := 0; i < 128; i++ {
9493
for j := 0; j < 8; j++ {
95-
key := registry.CatalogKey{Namespace: strconv.Itoa(i), Name: strconv.Itoa(j)}
94+
key := SourceKey{Namespace: strconv.Itoa(i), Name: strconv.Itoa(j)}
9695
keys = append(keys, key)
9796
rcp[key] = &RegistryClientStub{
9897
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
@@ -145,7 +144,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
145144
func TestOperatorCacheExpiration(t *testing.T) {
146145
rcp := RegistryClientProviderStub{}
147146
catsrcLister := operatorlister.NewLister().OperatorsV1alpha1().CatalogSourceLister()
148-
key := registry.CatalogKey{Namespace: "dummynamespace", Name: "dummyname"}
147+
key := SourceKey{Namespace: "dummynamespace", Name: "dummyname"}
149148
rcp[key] = &RegistryClientStub{
150149
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
151150
Bundles: []*api.Bundle{{
@@ -169,7 +168,7 @@ func TestOperatorCacheExpiration(t *testing.T) {
169168
func TestOperatorCacheReuse(t *testing.T) {
170169
rcp := RegistryClientProviderStub{}
171170
catsrcLister := operatorlister.NewLister().OperatorsV1alpha1().CatalogSourceLister()
172-
key := registry.CatalogKey{Namespace: "dummynamespace", Name: "dummyname"}
171+
key := SourceKey{Namespace: "dummynamespace", Name: "dummyname"}
173172
rcp[key] = &RegistryClientStub{
174173
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
175174
Bundles: []*api.Bundle{{
@@ -297,7 +296,7 @@ func TestCatalogSnapshotFind(t *testing.T) {
297296
func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
298297
rcp := RegistryClientProviderStub{}
299298
catsrcLister := operatorlister.NewLister().OperatorsV1alpha1().CatalogSourceLister()
300-
key := registry.CatalogKey{Namespace: "testnamespace", Name: "testname"}
299+
key := SourceKey{Namespace: "testnamespace", Name: "testname"}
301300
rcp[key] = &RegistryClientStub{
302301
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
303302
Bundles: []*api.Bundle{{
@@ -347,7 +346,7 @@ func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
347346
func TestNamespaceOperatorCacheError(t *testing.T) {
348347
rcp := RegistryClientProviderStub{}
349348
catsrcLister := operatorlister.NewLister().OperatorsV1alpha1().CatalogSourceLister()
350-
key := registry.CatalogKey{Namespace: "dummynamespace", Name: "dummyname"}
349+
key := SourceKey{Namespace: "dummynamespace", Name: "dummyname"}
351350
rcp[key] = &RegistryClientStub{
352351
ListBundlesError: errors.New("testing"),
353352
}

pkg/controller/registry/resolver/cache/operators.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"k8s.io/apimachinery/pkg/runtime/schema"
1212

1313
"github.com/operator-framework/api/pkg/operators/v1alpha1"
14-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1514
"github.com/operator-framework/operator-registry/pkg/api"
1615
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
1716
)
@@ -194,7 +193,7 @@ type OperatorSourceInfo struct {
194193
Package string
195194
Channel string
196195
StartingCSV string
197-
Catalog registry.CatalogKey
196+
Catalog SourceKey
198197
DefaultChannel bool
199198
Subscription *v1alpha1.Subscription
200199
}
@@ -203,7 +202,7 @@ func (i *OperatorSourceInfo) String() string {
203202
return fmt.Sprintf("%s/%s in %s/%s", i.Package, i.Channel, i.Catalog.Name, i.Catalog.Namespace)
204203
}
205204

206-
var NoCatalog = registry.CatalogKey{Name: "", Namespace: ""}
205+
var NoCatalog = SourceKey{Name: "", Namespace: ""}
207206
var ExistingOperator = OperatorSourceInfo{Package: "", Channel: "", StartingCSV: "", Catalog: NoCatalog, DefaultChannel: false}
208207

209208
type Operator struct {
@@ -219,7 +218,7 @@ type Operator struct {
219218
Properties []*api.Property
220219
}
221220

222-
func NewOperatorFromBundle(bundle *api.Bundle, startingCSV string, sourceKey registry.CatalogKey, defaultChannel string) (*Operator, error) {
221+
func NewOperatorFromBundle(bundle *api.Bundle, startingCSV string, sourceKey SourceKey, defaultChannel string) (*Operator, error) {
223222
parsedVersion, err := semver.ParseTolerant(bundle.Version)
224223
version := &parsedVersion
225224
if err != nil {

0 commit comments

Comments
 (0)