Skip to content

Commit

Permalink
Rename resolver/cache.Operator to Entry.
Browse files Browse the repository at this point in the history
This type is a data object representing a resolver cache entry. It
doesn't have a particular connection to operators other than residing
where it resides. This name change should clarify what, exactly, the
type represents.

OperatorPredicate is also renamed to cache.Predicate because the
argument to its core Test method is a cache.Entry.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
  • Loading branch information
benluddy committed Oct 5, 2021
1 parent a03dd0c commit 27ce072
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 189 deletions.
26 changes: 13 additions & 13 deletions pkg/controller/registry/resolver/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ func (c *NamespacedOperatorCache) Catalog(k SourceKey) OperatorFinder {
return EmptyOperatorFinder{}
}

func (c *NamespacedOperatorCache) FindPreferred(preferred *SourceKey, preferredNamespace string, p ...OperatorPredicate) []*Operator {
var result []*Operator
func (c *NamespacedOperatorCache) FindPreferred(preferred *SourceKey, preferredNamespace string, p ...Predicate) []*Entry {
var result []*Entry
if preferred != nil && preferred.Empty() {
preferred = nil
}
Expand Down Expand Up @@ -291,12 +291,12 @@ func (c *NamespacedOperatorCache) WithExistingOperators(snapshot *Snapshot, name
return o
}

func (c *NamespacedOperatorCache) Find(p ...OperatorPredicate) []*Operator {
func (c *NamespacedOperatorCache) Find(p ...Predicate) []*Entry {
return c.FindPreferred(nil, "", p...)
}

type Snapshot struct {
Entries []*Operator
Entries []*Entry
}

var _ Source = &Snapshot{}
Expand Down Expand Up @@ -402,7 +402,7 @@ func (s sortableSnapshots) Swap(i, j int) {
s.snapshots[i], s.snapshots[j] = s.snapshots[j], s.snapshots[i]
}

func (s *snapshotHeader) Find(p ...OperatorPredicate) []*Operator {
func (s *snapshotHeader) Find(p ...Predicate) []*Entry {
s.m.RLock()
defer s.m.RUnlock()
if s.snapshot == nil {
Expand All @@ -412,39 +412,39 @@ func (s *snapshotHeader) Find(p ...OperatorPredicate) []*Operator {
}

type OperatorFinder interface {
Find(...OperatorPredicate) []*Operator
Find(...Predicate) []*Entry
}

type MultiCatalogOperatorFinder interface {
Catalog(SourceKey) OperatorFinder
FindPreferred(preferred *SourceKey, preferredNamespace string, predicates ...OperatorPredicate) []*Operator
FindPreferred(preferred *SourceKey, preferredNamespace string, predicates ...Predicate) []*Entry
WithExistingOperators(snapshot *Snapshot, namespace string) MultiCatalogOperatorFinder
Error() error
OperatorFinder
}

type EmptyOperatorFinder struct{}

func (f EmptyOperatorFinder) Find(...OperatorPredicate) []*Operator {
func (f EmptyOperatorFinder) Find(...Predicate) []*Entry {
return nil
}

func AtLeast(n int, operators []*Operator) ([]*Operator, error) {
func AtLeast(n int, operators []*Entry) ([]*Entry, error) {
if len(operators) < n {
return nil, fmt.Errorf("expected at least %d operator(s), got %d", n, len(operators))
}
return operators, nil
}

func ExactlyOne(operators []*Operator) (*Operator, error) {
func ExactlyOne(operators []*Entry) (*Entry, error) {
if len(operators) != 1 {
return nil, fmt.Errorf("expected exactly one operator, got %d", len(operators))
}
return operators[0], nil
}

func Filter(operators []*Operator, p ...OperatorPredicate) []*Operator {
var result []*Operator
func Filter(operators []*Entry, p ...Predicate) []*Entry {
var result []*Entry
for _, o := range operators {
if Matches(o, p...) {
result = append(result, o)
Expand All @@ -453,6 +453,6 @@ func Filter(operators []*Operator, p ...OperatorPredicate) []*Operator {
return result
}

func Matches(o *Operator, p ...OperatorPredicate) bool {
func Matches(o *Entry, p ...Predicate) bool {
return And(p...).Test(o)
}
34 changes: 17 additions & 17 deletions pkg/controller/registry/resolver/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestOperatorCacheConcurrency(t *testing.T) {
key := SourceKey{Namespace: strconv.Itoa(i), Name: strconv.Itoa(j)}
keys = append(keys, key)
sp[key] = &Snapshot{
Entries: []*Operator{
Entries: []*Entry{
{Name: fmt.Sprintf("%s/%s", key.Namespace, key.Name)},
},
}
Expand Down Expand Up @@ -71,14 +71,14 @@ func TestOperatorCacheExpiration(t *testing.T) {
c.ttl = 0 // instantly stale

ssp[key] = &Snapshot{
Entries: []*Operator{
Entries: []*Entry{
{Name: "v1"},
},
}
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(CSVNamePredicate("v1")), 1)

ssp[key] = &Snapshot{
Entries: []*Operator{
Entries: []*Entry{
{Name: "v2"},
},
}
Expand All @@ -91,14 +91,14 @@ func TestOperatorCacheReuse(t *testing.T) {
c := New(ssp)

ssp[key] = &Snapshot{
Entries: []*Operator{
Entries: []*Entry{
{Name: "v1"},
},
}
require.Len(t, c.Namespaced("dummynamespace").Catalog(key).Find(CSVNamePredicate("v1")), 1)

ssp[key] = &Snapshot{
Entries: []*Operator{
Entries: []*Entry{
{Name: "v2"},
},
}
Expand Down Expand Up @@ -172,18 +172,18 @@ func TestCatalogSnapshotValid(t *testing.T) {
func TestCatalogSnapshotFind(t *testing.T) {
type tc struct {
Name string
Predicate OperatorPredicate
Operators []*Operator
Expected []*Operator
Predicate Predicate
Operators []*Entry
Expected []*Entry
}

for _, tt := range []tc{
{
Name: "nothing satisfies predicate",
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
Predicate: OperatorPredicateTestFunc(func(*Entry) bool {
return false
}),
Operators: []*Operator{
Operators: []*Entry{
{Name: "a"},
{Name: "b"},
{Name: "c"},
Expand All @@ -192,39 +192,39 @@ func TestCatalogSnapshotFind(t *testing.T) {
},
{
Name: "no operators in snapshot",
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
Predicate: OperatorPredicateTestFunc(func(*Entry) bool {
return true
}),
Operators: nil,
Expected: nil,
},
{
Name: "everything satisfies predicate",
Predicate: OperatorPredicateTestFunc(func(*Operator) bool {
Predicate: OperatorPredicateTestFunc(func(*Entry) bool {
return true
}),
Operators: []*Operator{
Operators: []*Entry{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
Expected: []*Operator{
Expected: []*Entry{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
},
{
Name: "some satisfy predicate",
Predicate: OperatorPredicateTestFunc(func(o *Operator) bool {
Predicate: OperatorPredicateTestFunc(func(o *Entry) bool {
return o.Name != "a"
}),
Operators: []*Operator{
Operators: []*Entry{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
Expected: []*Operator{
Expected: []*Entry{
{Name: "b"},
{Name: "c"},
},
Expand Down
16 changes: 8 additions & 8 deletions pkg/controller/registry/resolver/cache/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ func (s APISet) StripPlural() APISet {
return set
}

type APIOwnerSet map[opregistry.APIKey]*Operator
type APIOwnerSet map[opregistry.APIKey]*Entry

func EmptyAPIOwnerSet() APIOwnerSet {
return map[opregistry.APIKey]*Operator{}
return map[opregistry.APIKey]*Entry{}
}

type OperatorSet map[string]*Operator
type OperatorSet map[string]*Entry

func EmptyOperatorSet() OperatorSet {
return map[string]*Operator{}
return map[string]*Entry{}
}

// Snapshot returns a new set, pointing to the same values
func (o OperatorSet) Snapshot() OperatorSet {
out := make(map[string]*Operator)
out := make(map[string]*Entry)
for key, val := range o {
out[key] = val
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (i *OperatorSourceInfo) String() string {
var NoCatalog = SourceKey{Name: "", Namespace: ""}
var ExistingOperator = OperatorSourceInfo{Package: "", Channel: "", StartingCSV: "", Catalog: NoCatalog, DefaultChannel: false}

type Operator struct {
type Entry struct {
Name string
Replaces string
Skips []string
Expand All @@ -223,14 +223,14 @@ type Operator struct {
Bundle *api.Bundle
}

func (o *Operator) Package() string {
func (o *Entry) Package() string {
if si := o.SourceInfo; si != nil {
return si.Package
}
return ""
}

func (o *Operator) Channel() string {
func (o *Entry) Channel() string {
if si := o.SourceInfo; si != nil {
return si.Channel
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/controller/registry/resolver/cache/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,15 +728,15 @@ func TestAPIMultiOwnerSet_PopAPIKey(t *testing.T) {
{
name: "OneApi/OneOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
},
},
},
{
name: "OneApi/MultiOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
Expand All @@ -745,11 +745,11 @@ func TestAPIMultiOwnerSet_PopAPIKey(t *testing.T) {
{
name: "MultipleApi/MultiOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
{Group: "g2", Version: "v2", Kind: "k2", Plural: "p2"}: map[string]*Operator{
{Group: "g2", Version: "v2", Kind: "k2", Plural: "p2"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
Expand Down Expand Up @@ -788,40 +788,40 @@ func TestAPIMultiOwnerSet_PopAPIRequirers(t *testing.T) {
{
name: "OneApi/OneOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
},
},
want: map[string]*Operator{
want: map[string]*Entry{
"owner1": {Name: "op1"},
},
},
{
name: "OneApi/MultiOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
},
want: map[string]*Operator{
want: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
},
{
name: "MultipleApi/MultiOwner",
s: map[opregistry.APIKey]OperatorSet{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Operator{
{Group: "g", Version: "v", Kind: "k", Plural: "p"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
{Group: "g2", Version: "v2", Kind: "k2", Plural: "p2"}: map[string]*Operator{
{Group: "g2", Version: "v2", Kind: "k2", Plural: "p2"}: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
},
want: map[string]*Operator{
want: map[string]*Entry{
"owner1": {Name: "op1"},
"owner2": {Name: "op2"},
},
Expand Down
Loading

0 comments on commit 27ce072

Please sign in to comment.