Skip to content

Commit e944849

Browse files
Merge pull request #2258 from anik120/all-about-predicates
feat(resolver): Indicate dependency class in resolution constraint text
2 parents 3223fce + 8c0f4aa commit e944849

File tree

9 files changed

+489
-311
lines changed

9 files changed

+489
-311
lines changed

pkg/controller/registry/resolver/cache.go

Lines changed: 0 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"sync"
99
"time"
1010

11-
"github.com/blang/semver/v4"
1211
"github.com/sirupsen/logrus"
1312

1413
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
@@ -387,16 +386,6 @@ func (s SortableSnapshots) Swap(i, j int) {
387386
s.snapshots[i], s.snapshots[j] = s.snapshots[j], s.snapshots[i]
388387
}
389388

390-
type OperatorPredicateFunc func(*Operator) bool
391-
392-
func (opf OperatorPredicateFunc) Test(o *Operator) bool {
393-
return opf(o)
394-
}
395-
396-
type OperatorPredicate interface {
397-
Test(*Operator) bool
398-
}
399-
400389
func (s *CatalogSnapshot) Find(p ...OperatorPredicate) []*Operator {
401390
s.m.RLock()
402391
defer s.m.RUnlock()
@@ -420,155 +409,6 @@ func (f EmptyOperatorFinder) Find(...OperatorPredicate) []*Operator {
420409
return nil
421410
}
422411

423-
func WithCSVName(name string) OperatorPredicate {
424-
return OperatorPredicateFunc(func(o *Operator) bool {
425-
return o.name == name
426-
})
427-
}
428-
429-
func WithChannel(channel string) OperatorPredicate {
430-
return OperatorPredicateFunc(func(o *Operator) bool {
431-
// all operators match the empty channel
432-
if channel == "" {
433-
return true
434-
}
435-
if o.bundle == nil {
436-
return false
437-
}
438-
return o.bundle.ChannelName == channel
439-
})
440-
}
441-
442-
func WithPackage(pkg string) OperatorPredicate {
443-
return OperatorPredicateFunc(func(o *Operator) bool {
444-
for _, p := range o.Properties() {
445-
if p.Type != opregistry.PackageType {
446-
continue
447-
}
448-
var prop opregistry.PackageProperty
449-
err := json.Unmarshal([]byte(p.Value), &prop)
450-
if err != nil {
451-
continue
452-
}
453-
if prop.PackageName == pkg {
454-
return true
455-
}
456-
}
457-
return o.Package() == pkg
458-
})
459-
}
460-
461-
func WithVersionInRange(r semver.Range) OperatorPredicate {
462-
return OperatorPredicateFunc(func(o *Operator) bool {
463-
for _, p := range o.Properties() {
464-
if p.Type != opregistry.PackageType {
465-
continue
466-
}
467-
var prop opregistry.PackageProperty
468-
err := json.Unmarshal([]byte(p.Value), &prop)
469-
if err != nil {
470-
continue
471-
}
472-
ver, err := semver.Parse(prop.Version)
473-
if err != nil {
474-
continue
475-
}
476-
if r(ver) {
477-
return true
478-
}
479-
}
480-
return o.version != nil && r(*o.version)
481-
})
482-
}
483-
484-
func WithLabel(label string) OperatorPredicate {
485-
return OperatorPredicateFunc(func(o *Operator) bool {
486-
for _, p := range o.Properties() {
487-
if p.Type != opregistry.LabelType {
488-
continue
489-
}
490-
var prop opregistry.LabelProperty
491-
err := json.Unmarshal([]byte(p.Value), &prop)
492-
if err != nil {
493-
continue
494-
}
495-
if prop.Label == label {
496-
return true
497-
}
498-
}
499-
return false
500-
})
501-
}
502-
503-
func WithCatalog(key registry.CatalogKey) OperatorPredicate {
504-
return OperatorPredicateFunc(func(o *Operator) bool {
505-
return key.Equal(o.SourceInfo().Catalog)
506-
})
507-
}
508-
509-
func ProvidingAPI(api opregistry.APIKey) OperatorPredicate {
510-
return OperatorPredicateFunc(func(o *Operator) bool {
511-
for _, p := range o.Properties() {
512-
if p.Type != opregistry.GVKType {
513-
continue
514-
}
515-
var prop opregistry.GVKProperty
516-
err := json.Unmarshal([]byte(p.Value), &prop)
517-
if err != nil {
518-
continue
519-
}
520-
if prop.Kind == api.Kind && prop.Version == api.Version && prop.Group == api.Group {
521-
return true
522-
}
523-
}
524-
return false
525-
})
526-
}
527-
528-
func SkipRangeIncludes(version semver.Version) OperatorPredicate {
529-
return OperatorPredicateFunc(func(o *Operator) bool {
530-
// TODO: lift range parsing to OperatorSurface
531-
semverRange, err := semver.ParseRange(o.bundle.SkipRange)
532-
return err == nil && semverRange(version)
533-
})
534-
}
535-
536-
func Replaces(name string) OperatorPredicate {
537-
return OperatorPredicateFunc(func(o *Operator) bool {
538-
if o.Replaces() == name {
539-
return true
540-
}
541-
for _, s := range o.bundle.Skips {
542-
if s == name {
543-
return true
544-
}
545-
}
546-
return false
547-
})
548-
}
549-
550-
func And(p ...OperatorPredicate) OperatorPredicate {
551-
return OperatorPredicateFunc(func(o *Operator) bool {
552-
for _, l := range p {
553-
if l.Test(o) == false {
554-
return false
555-
}
556-
}
557-
return true
558-
})
559-
}
560-
561-
func Or(p ...OperatorPredicate) OperatorPredicate {
562-
return OperatorPredicateFunc(func(o *Operator) bool {
563-
for _, l := range p {
564-
if l.Test(o) == true {
565-
return true
566-
}
567-
}
568-
return false
569-
})
570-
}
571-
572412
func AtLeast(n int, operators []*Operator) ([]*Operator, error) {
573413
if len(operators) < n {
574414
return nil, fmt.Errorf("expected at least %d operator(s), got %d", n, len(operators))
@@ -596,25 +436,3 @@ func Filter(operators []*Operator, p ...OperatorPredicate) []*Operator {
596436
func Matches(o *Operator, p ...OperatorPredicate) bool {
597437
return And(p...).Test(o)
598438
}
599-
600-
func True() OperatorPredicate {
601-
return OperatorPredicateFunc(func(*Operator) bool {
602-
return true
603-
})
604-
}
605-
606-
func False() OperatorPredicate {
607-
return OperatorPredicateFunc(func(*Operator) bool {
608-
return false
609-
})
610-
}
611-
612-
func CountingPredicate(p OperatorPredicate, n *int) OperatorPredicate {
613-
return OperatorPredicateFunc(func(o *Operator) bool {
614-
if p.Test(o) {
615-
*n++
616-
return true
617-
}
618-
return false
619-
})
620-
}

pkg/controller/registry/resolver/cache_test.go

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
123123
nc := c.Namespaced(namespaces...)
124124
for _, index := range indices {
125125
name := fmt.Sprintf("%s/%s", keys[index].Namespace, keys[index].Name)
126-
operators := nc.Find(WithCSVName(name))
126+
operators := nc.Find(CSVNamePredicate(name))
127127
if len(operators) != 1 {
128128
return fmt.Errorf("expected 1 operator, got %d", len(operators))
129129
}
@@ -159,7 +159,7 @@ func TestOperatorCacheExpiration(t *testing.T) {
159159
c := NewOperatorCache(rcp, logrus.New(), catsrcLister)
160160
c.ttl = 0 // instantly stale
161161

162-
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(WithCSVName("csvname")), 1)
162+
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(CSVNamePredicate("csvname")), 1)
163163
}
164164

165165
func TestOperatorCacheReuse(t *testing.T) {
@@ -182,7 +182,7 @@ func TestOperatorCacheReuse(t *testing.T) {
182182

183183
c := NewOperatorCache(rcp, logrus.New(), catsrcLister)
184184

185-
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(WithCSVName("csvname")), 1)
185+
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(CSVNamePredicate("csvname")), 1)
186186
}
187187

188188
func TestCatalogSnapshotExpired(t *testing.T) {
@@ -232,7 +232,7 @@ func TestCatalogSnapshotFind(t *testing.T) {
232232
for _, tt := range []tc{
233233
{
234234
Name: "nothing satisfies predicate",
235-
Predicate: OperatorPredicateFunc(func(*Operator) bool {
235+
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
236236
return false
237237
}),
238238
Operators: []*Operator{
@@ -244,15 +244,15 @@ func TestCatalogSnapshotFind(t *testing.T) {
244244
},
245245
{
246246
Name: "no operators in snapshot",
247-
Predicate: OperatorPredicateFunc(func(*Operator) bool {
247+
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
248248
return true
249249
}),
250250
Operators: nil,
251251
Expected: nil,
252252
},
253253
{
254254
Name: "everything satisfies predicate",
255-
Predicate: OperatorPredicateFunc(func(*Operator) bool {
255+
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
256256
return true
257257
}),
258258
Operators: []*Operator{
@@ -268,7 +268,7 @@ func TestCatalogSnapshotFind(t *testing.T) {
268268
},
269269
{
270270
Name: "some satisfy predicate",
271-
Predicate: OperatorPredicateFunc(func(o *Operator) bool {
271+
Predicate: OperatorPredicateTestFunc(func(o *Operator) bool {
272272
return o.name != "a"
273273
}),
274274
Operators: []*Operator{
@@ -333,55 +333,9 @@ func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
333333
c := NewOperatorCache(rcp, logrus.New(), catsrcLister)
334334

335335
nc := c.Namespaced("testnamespace")
336-
result, err := AtLeast(1, nc.Find(ProvidingAPI(opregistry.APIKey{Group: "g", Version: "v1", Kind: "K"})))
336+
result, err := AtLeast(1, nc.Find(ProvidingAPIPredicate(opregistry.APIKey{Group: "g", Version: "v1", Kind: "K"})))
337337
assert.NoError(t, err)
338338
assert.Equal(t, 1, len(result))
339339
assert.Equal(t, "K.v1.g", result[0].providedAPIs.String())
340340
assert.Equal(t, "K2.v2.g2", result[0].requiredAPIs.String())
341341
}
342-
343-
func TestCountingPredicate(t *testing.T) {
344-
for _, tc := range []struct {
345-
Name string
346-
TestResults []bool
347-
Expected int
348-
}{
349-
{
350-
Name: "no increment on failure",
351-
TestResults: []bool{false},
352-
Expected: 0,
353-
},
354-
{
355-
Name: "increment on success",
356-
TestResults: []bool{true},
357-
Expected: 1,
358-
},
359-
{
360-
Name: "multiple increments",
361-
TestResults: []bool{true, true},
362-
Expected: 2,
363-
},
364-
{
365-
Name: "no increment without test",
366-
TestResults: nil,
367-
Expected: 0,
368-
},
369-
} {
370-
t.Run(tc.Name, func(t *testing.T) {
371-
var (
372-
n int
373-
result bool
374-
)
375-
376-
p := CountingPredicate(OperatorPredicateFunc(func(*Operator) bool {
377-
return result
378-
}), &n)
379-
380-
for _, result = range tc.TestResults {
381-
p.Test(nil)
382-
}
383-
384-
assert.Equal(t, tc.Expected, n)
385-
})
386-
}
387-
}

pkg/controller/registry/resolver/installabletypes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (i *BundleInstallable) AddConflict(id solver.Identifier) {
3232
i.constraints = append(i.constraints, solver.Conflict(id))
3333
}
3434

35-
func (i *BundleInstallable) AddDependency(dependencies []solver.Identifier) {
36-
i.constraints = append(i.constraints, solver.Dependency(dependencies...))
35+
func (i *BundleInstallable) AddConstraint(c solver.Constraint) {
36+
i.constraints = append(i.constraints, c)
3737
}
3838

3939
func (i *BundleInstallable) BundleSourceInfo() (string, string, registry.CatalogKey, error) {

0 commit comments

Comments
 (0)