Skip to content

Commit e7fa82a

Browse files
committed
Add catalog priority for dependency resolution
Signed-off-by: Harish <[email protected]>
1 parent 7b35209 commit e7fa82a

25 files changed

+544
-51
lines changed

deploy/chart/crds/0000_50_olm_00-catalogsources.crd.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ spec:
9090
a registry-server with. Only used when SourceType = SourceTypeGrpc.
9191
If present, the address field is ignored.
9292
type: string
93+
priority:
94+
description: 'Priority field assigns a weight to the catalog source
95+
to prioritize them so that it can be consumed by the dependency
96+
resolver. Usage: Higher weight indicates that this catalog source
97+
is preferred over lower weighted catalog sources during dependency
98+
resolution. The range of the priority value can go from positive
99+
to negative in the range of int32. The default value to a catalog
100+
source with unassigned priority would be 0. The catalog source with
101+
the same priority values will be ranked lexicographically based
102+
on its name.'
103+
type: integer
93104
publisher:
94105
type: string
95106
secrets:

deploy/chart/crds/0000_50_olm_00-clusterserviceversions.crd.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,19 @@ spec:
662662
deployments:
663663
type: array
664664
items:
665-
description: StrategyDeploymentSpec contains the name and
666-
spec for the deployment ALM should create
665+
description: StrategyDeploymentSpec contains the name, spec
666+
and labels for the deployment ALM should create
667667
type: object
668668
required:
669669
- name
670670
- spec
671671
properties:
672+
label:
673+
description: Set is a map of label:value. It implements
674+
Labels.
675+
type: object
676+
additionalProperties:
677+
type: string
672678
name:
673679
type: string
674680
spec:
@@ -8698,6 +8704,10 @@ spec:
86988704
containerPort:
86998705
type: integer
87008706
format: int32
8707+
conversionCRDs:
8708+
type: array
8709+
items:
8710+
type: string
87018711
deploymentName:
87028712
type: string
87038713
failurePolicy:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/onsi/gomega v1.9.0
2323
github.com/openshift/api v0.0.0-20200331152225-585af27e34fd
2424
github.com/openshift/client-go v0.0.0-20200326155132-2a6cd50aedd0
25-
github.com/operator-framework/api v0.3.7
25+
github.com/operator-framework/api v0.3.9
2626
github.com/operator-framework/operator-registry v1.13.3
2727
github.com/otiai10/copy v1.2.0
2828
github.com/pkg/errors v0.9.1

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
409409
github.com/grpc-ecosystem/grpc-health-probe v0.3.2 h1:daShAySXI1DnGc8U9B1E4Qm6o7qzmFR4aRIJ4vY/TUo=
410410
github.com/grpc-ecosystem/grpc-health-probe v0.3.2/go.mod h1:izVOQ4RWbjUR6lm4nn+VLJyQ+FyaiGmprEYgI04Gs7U=
411411
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
412+
github.com/harishsurf/api v0.3.8-0.20200727161005-2f00961f221d h1:Q9Z/l6BmNar+eqGPRwWFRxnC0F0lFyb/n3x14JTKryM=
413+
github.com/harishsurf/api v0.3.8-0.20200727161005-2f00961f221d/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
412414
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
413415
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
414416
github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I=
@@ -585,8 +587,8 @@ github.com/openshift/client-go v0.0.0-20200326155132-2a6cd50aedd0 h1:kMiuiZXH1Gd
585587
github.com/openshift/client-go v0.0.0-20200326155132-2a6cd50aedd0/go.mod h1:uUQ4LClRO+fg5MF/P6QxjMCb1C9f7Oh4RKepftDnEJE=
586588
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
587589
github.com/operator-framework/api v0.3.7-0.20200602203552-431198de9fc2/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
588-
github.com/operator-framework/api v0.3.7 h1:gTER+WQdjRAegxKdcoylNqHVGGnBRAAgC2zS3CZL78g=
589-
github.com/operator-framework/api v0.3.7/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
590+
github.com/operator-framework/api v0.3.9 h1:ggRW5C/7MLmA4cOJ2mjEZ5RqDMsIeMknd6vx15S7Xo4=
591+
github.com/operator-framework/api v0.3.9/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q=
590592
github.com/operator-framework/operator-registry v1.13.3 h1:SaZ1IKLKGizVgTtT8AlDgaMXFYOiIPxRf8KLve0/DXM=
591593
github.com/operator-framework/operator-registry v1.13.3/go.mod h1:YhnIzOVjRU2ZwZtzt+fjcjW8ujJaSFynBEu7QVKaSdU=
592594
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=

pkg/controller/registry/resolver/cache.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
78
"sort"
89
"sync"
910
"time"
1011

1112
"github.com/blang/semver"
13+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
14+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1215
"github.com/operator-framework/operator-registry/pkg/api"
1316
"github.com/operator-framework/operator-registry/pkg/client"
1417
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
1518
"github.com/sirupsen/logrus"
16-
17-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1819
)
1920

2021
type RegistryClientProvider interface {
@@ -45,6 +46,7 @@ type OperatorCacheProvider interface {
4546
type OperatorCache struct {
4647
logger logrus.FieldLogger
4748
rcp RegistryClientProvider
49+
crClient versioned.Interface
4850
snapshots map[registry.CatalogKey]*CatalogSnapshot
4951
ttl time.Duration
5052
sem chan struct{}
@@ -53,14 +55,15 @@ type OperatorCache struct {
5355

5456
var _ OperatorCacheProvider = &OperatorCache{}
5557

56-
func NewOperatorCache(rcp RegistryClientProvider, log logrus.FieldLogger) *OperatorCache {
58+
func NewOperatorCache(rcp RegistryClientProvider, log logrus.FieldLogger, client versioned.Interface) *OperatorCache {
5759
const (
5860
MaxConcurrentSnapshotUpdates = 4
5961
)
6062

6163
return &OperatorCache{
6264
logger: log,
6365
rcp: rcp,
66+
crClient: client,
6467
snapshots: make(map[registry.CatalogKey]*CatalogSnapshot),
6568
ttl: 5 * time.Minute,
6669
sem: make(chan struct{}, MaxConcurrentSnapshotUpdates),
@@ -151,11 +154,20 @@ func (c *OperatorCache) Namespaced(namespaces ...string) MultiCatalogOperatorFin
151154

152155
for _, miss := range misses {
153156
ctx, cancel := context.WithTimeout(context.Background(), CachePopulateTimeout)
157+
158+
catsrcPriority := 0
159+
// Ignoring error and treating catalog source priority as 0 if not found
160+
catsrc, err := c.crClient.OperatorsV1alpha1().CatalogSources(miss.Namespace).Get(ctx, miss.Name, v1.GetOptions{})
161+
if err == nil {
162+
catsrcPriority = catsrc.Spec.Priority
163+
}
164+
154165
s := CatalogSnapshot{
155-
logger: c.logger.WithField("catalog", miss),
156-
key: miss,
157-
expiry: now.Add(c.ttl),
158-
pop: cancel,
166+
logger: c.logger.WithField("catalog", miss),
167+
key: miss,
168+
expiry: now.Add(c.ttl),
169+
pop: cancel,
170+
priority: setCatalogPriority(miss, catsrcPriority),
159171
}
160172
s.m.Lock()
161173
c.snapshots[miss] = &s
@@ -277,6 +289,7 @@ type CatalogSnapshot struct {
277289
operators []*Operator
278290
m sync.RWMutex
279291
pop context.CancelFunc
292+
priority Priority
280293
}
281294

282295
func (s *CatalogSnapshot) Cancel() {
@@ -354,6 +367,9 @@ func (s SortableSnapshots) Less(i, j int) bool {
354367
return false
355368
}
356369

370+
if s.snapshots[i].priority != s.snapshots[j].priority {
371+
return s.snapshots[i].priority.catsrcPriority > s.snapshots[j].priority.catsrcPriority
372+
}
357373
// the rest are sorted first in namespace preference order, then by name
358374
if s.snapshots[i].key.Namespace != s.snapshots[j].key.Namespace {
359375
return s.namespaces[s.snapshots[i].key.Namespace] < s.namespaces[s.snapshots[j].key.Namespace]

pkg/controller/registry/resolver/cache_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
1515

16+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
17+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1618
"github.com/operator-framework/operator-registry/pkg/api"
1719
"github.com/operator-framework/operator-registry/pkg/client"
1820
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
19-
20-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
2121
)
2222

2323
type BundleStreamStub struct {
@@ -85,6 +85,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
8585
)
8686

8787
rcp := RegistryClientProviderStub{}
88+
var crClient versioned.Interface
8889
var keys []registry.CatalogKey
8990
for i := 0; i < 128; i++ {
9091
for j := 0; j < 8; j++ {
@@ -106,7 +107,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
106107
}
107108
}
108109

109-
c := NewOperatorCache(rcp, logrus.New())
110+
c := NewOperatorCache(rcp, logrus.New(), crClient)
110111

111112
errs := make(chan error)
112113
for w := 0; w < NWorkers; w++ {
@@ -140,6 +141,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
140141

141142
func TestOperatorCacheExpiration(t *testing.T) {
142143
rcp := RegistryClientProviderStub{}
144+
var crClient versioned.Interface
143145
key := registry.CatalogKey{Namespace: "dummynamespace", Name: "dummyname"}
144146
rcp[key] = &RegistryClientStub{
145147
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
@@ -155,14 +157,15 @@ func TestOperatorCacheExpiration(t *testing.T) {
155157
}),
156158
}
157159

158-
c := NewOperatorCache(rcp, logrus.New())
160+
c := NewOperatorCache(rcp, logrus.New(), crClient)
159161
c.ttl = 0 // instantly stale
160162

161163
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(WithCSVName("csvname")), 1)
162164
}
163165

164166
func TestOperatorCacheReuse(t *testing.T) {
165167
rcp := RegistryClientProviderStub{}
168+
var crClient versioned.Interface
166169
key := registry.CatalogKey{Namespace: "dummynamespace", Name: "dummyname"}
167170
rcp[key] = &RegistryClientStub{
168171
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
@@ -178,7 +181,7 @@ func TestOperatorCacheReuse(t *testing.T) {
178181
}),
179182
}
180183

181-
c := NewOperatorCache(rcp, logrus.New())
184+
c := NewOperatorCache(rcp, logrus.New(), crClient)
182185

183186
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(WithCSVName("csvname")), 1)
184187
}
@@ -290,6 +293,7 @@ func TestCatalogSnapshotFind(t *testing.T) {
290293

291294
func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
292295
rcp := RegistryClientProviderStub{}
296+
var crClient versioned.Interface
293297
key := registry.CatalogKey{Namespace: "testnamespace", Name: "testname"}
294298
rcp[key] = &RegistryClientStub{
295299
BundleIterator: client.NewBundleIterator(&BundleStreamStub{
@@ -327,7 +331,7 @@ func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
327331
}),
328332
}
329333

330-
c := NewOperatorCache(rcp, logrus.New())
334+
c := NewOperatorCache(rcp, logrus.New(), crClient)
331335

332336
nc := c.Namespaced("testnamespace")
333337
result, err := AtLeast(1, nc.Find(ProvidingAPI(opregistry.APIKey{Group: "g", Version: "v1", Kind: "K"})))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package resolver
2+
3+
import "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
4+
5+
type Priority struct {
6+
catsrcPriority int
7+
}
8+
9+
const (
10+
defaultRHCatalogSourceName = "redhat-operators"
11+
defaultCertifiedCatalogSourceName = "certified-operators"
12+
defaultCommunityCatalogSourceName = "community-operators"
13+
defaultRHCatalogSourcePriority = -100
14+
defaultCertifiedCatalogSourcePriority = -200
15+
defaultCommunityCatalogSourcePriority = -300
16+
)
17+
18+
func setCatalogPriority(key registry.CatalogKey, catsrc int) Priority {
19+
if catsrc == 0 {
20+
return assignDefaultCatalogPriority(key, catsrc)
21+
}
22+
return Priority{catsrcPriority: catsrc}
23+
}
24+
25+
func assignDefaultCatalogPriority(key registry.CatalogKey, catsrc int) Priority {
26+
27+
if key.Name == defaultRHCatalogSourceName {
28+
return Priority{catsrcPriority: defaultRHCatalogSourcePriority}
29+
}
30+
31+
if key.Name == defaultCertifiedCatalogSourceName {
32+
return Priority{catsrcPriority: defaultCertifiedCatalogSourcePriority}
33+
}
34+
35+
if key.Name == defaultCommunityCatalogSourceName {
36+
return Priority{catsrcPriority: defaultCommunityCatalogSourcePriority}
37+
}
38+
39+
return Priority{catsrcPriority: 0}
40+
}

pkg/controller/registry/resolver/resolver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
78
"sort"
89

910
"github.com/operator-framework/api/pkg/operators/v1alpha1"
@@ -24,9 +25,9 @@ type SatResolver struct {
2425
log logrus.FieldLogger
2526
}
2627

27-
func NewDefaultSatResolver(rcp RegistryClientProvider, log logrus.FieldLogger) *SatResolver {
28+
func NewDefaultSatResolver(rcp RegistryClientProvider, log logrus.FieldLogger, client versioned.Interface) *SatResolver {
2829
return &SatResolver{
29-
cache: NewOperatorCache(rcp, log),
30+
cache: NewOperatorCache(rcp, log, client),
3031
log: log,
3132
}
3233
}

0 commit comments

Comments
 (0)