Skip to content

Commit 86998b0

Browse files
committed
feat(resolver): Move all predicates to its own file
Signed-off-by: Anik Bhattacharjee <[email protected]>
1 parent f87b076 commit 86998b0

File tree

5 files changed

+303
-286
lines changed

5 files changed

+303
-286
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: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -339,49 +339,3 @@ func TestStripPluralRequiredAndProvidedAPIKeys(t *testing.T) {
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/operators.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -434,64 +434,6 @@ func (o *Operator) DependencyPredicates() (predicates []OperatorPredicate, err e
434434
return
435435
}
436436

437-
func PredicateForProperty(property *api.Property) (OperatorPredicate, error) {
438-
if property == nil {
439-
return nil, nil
440-
}
441-
p, ok := predicates[property.Type]
442-
if !ok {
443-
return nil, nil
444-
}
445-
return p(property.Value)
446-
}
447-
448-
var predicates = map[string]func(string) (OperatorPredicate, error){
449-
"olm.gvk.required": predicateForRequiredGVKProperty,
450-
"olm.package.required": predicateForRequiredPackageProperty,
451-
"olm.label.required": predicateForRequiredLabelProperty,
452-
}
453-
454-
func predicateForRequiredGVKProperty(value string) (OperatorPredicate, error) {
455-
var gvk struct {
456-
Group string `json:"group"`
457-
Version string `json:"version"`
458-
Kind string `json:"kind"`
459-
}
460-
if err := json.Unmarshal([]byte(value), &gvk); err != nil {
461-
return nil, err
462-
}
463-
return ProvidingAPI(opregistry.APIKey{
464-
Group: gvk.Group,
465-
Version: gvk.Version,
466-
Kind: gvk.Kind,
467-
}), nil
468-
}
469-
470-
func predicateForRequiredPackageProperty(value string) (OperatorPredicate, error) {
471-
var pkg struct {
472-
PackageName string `json:"packageName"`
473-
VersionRange string `json:"versionRange"`
474-
}
475-
if err := json.Unmarshal([]byte(value), &pkg); err != nil {
476-
return nil, err
477-
}
478-
ver, err := semver.ParseRange(pkg.VersionRange)
479-
if err != nil {
480-
return nil, err
481-
}
482-
return And(WithPackage(pkg.PackageName), WithVersionInRange(ver)), nil
483-
}
484-
485-
func predicateForRequiredLabelProperty(value string) (OperatorPredicate, error) {
486-
var label struct {
487-
Label string `json:"label"`
488-
}
489-
if err := json.Unmarshal([]byte(value), &label); err != nil {
490-
return nil, err
491-
}
492-
return WithLabel(label.Label), nil
493-
}
494-
495437
func requiredAPIsToProperties(apis APISet) (out []*api.Property, err error) {
496438
if len(apis) == 0 {
497439
return

0 commit comments

Comments
 (0)