Skip to content

Prune API of the resolver package. #2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/controller/operators/catalog/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
// get the set of sources that should be used for resolution and best-effort get their connections working
logger.Debug("resolving sources")

querier := resolver.NewNamespaceSourceQuerier(o.sources.AsClients(o.namespace, namespace))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: this wasn't (never?) actually dependency injected

even if it was, the DI should have taken a factory anyway

querier := NewNamespaceSourceQuerier(o.sources.AsClients(o.namespace, namespace))

logger.Debug("checking if subscriptions need update")

Expand Down Expand Up @@ -950,7 +950,7 @@ func (o *Operator) syncResolvingNamespace(obj interface{}) error {
logger.Debug("resolving subscriptions in namespace")

// resolve a set of steps to apply to a cluster, a set of subscriptions to create/update, and any errors
steps, bundleLookups, updatedSubs, err := o.resolver.ResolveSteps(namespace, querier)
steps, bundleLookups, updatedSubs, err := o.resolver.ResolveSteps(namespace)
if err != nil {
go o.recorder.Event(ns, corev1.EventTypeWarning, "ResolutionFailed", err.Error())
// If the error is constraints not satisfiable, then simply project the
Expand Down Expand Up @@ -1085,7 +1085,7 @@ func (o *Operator) ensureSubscriptionInstallPlanState(logger *logrus.Entry, sub
return updated, true, nil
}

func (o *Operator) ensureSubscriptionCSVState(logger *logrus.Entry, sub *v1alpha1.Subscription, querier resolver.SourceQuerier) (*v1alpha1.Subscription, bool, error) {
func (o *Operator) ensureSubscriptionCSVState(logger *logrus.Entry, sub *v1alpha1.Subscription, querier SourceQuerier) (*v1alpha1.Subscription, bool, error) {
if sub.Status.CurrentCSV == "" {
return sub, false, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/operators/catalog/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ func TestSyncResolvingNamespace(t *testing.T) {

o.sourcesLastUpdate.Set(tt.fields.sourcesLastUpdate.Time)
o.resolver = &fakes.FakeStepResolver{
ResolveStepsStub: func(string, resolver.SourceQuerier) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) {
ResolveStepsStub: func(string) ([]*v1alpha1.Step, []v1alpha1.BundleLookup, []*v1alpha1.Subscription, error) {
return nil, nil, nil, tt.fields.resolveErr
},
}
Expand Down
123 changes: 123 additions & 0 deletions pkg/controller/operators/catalog/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_registry_client.go ../../../../vendor/github.com/operator-framework/operator-registry/pkg/api.RegistryClient
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_registry_interface.go ../../registry/registry_client.go ClientInterface
package catalog

import (
"context"
"fmt"

"github.com/blang/semver/v4"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/errors"

"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/client"

"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
)

const SkipPackageAnnotationKey = "olm.skipRange"

type SourceRef struct {
Address string
Client client.Interface
LastConnect metav1.Time
LastHealthy metav1.Time
}

type SourceQuerier interface {
FindReplacement(currentVersion *semver.Version, bundleName, pkgName, channelName string, initialSource registry.CatalogKey) (*api.Bundle, *registry.CatalogKey, error)
Queryable() error
}

type NamespaceSourceQuerier struct {
sources map[registry.CatalogKey]registry.ClientInterface
}

var _ SourceQuerier = &NamespaceSourceQuerier{}

func NewNamespaceSourceQuerier(sources map[registry.CatalogKey]registry.ClientInterface) *NamespaceSourceQuerier {
return &NamespaceSourceQuerier{
sources: sources,
}
}

func (q *NamespaceSourceQuerier) Queryable() error {
if len(q.sources) == 0 {
return fmt.Errorf("no catalog sources available")
}
return nil
}

func (q *NamespaceSourceQuerier) FindReplacement(currentVersion *semver.Version, bundleName, pkgName, channelName string, initialSource registry.CatalogKey) (*api.Bundle, *registry.CatalogKey, error) {
errs := []error{}

if initialSource.Name != "" && initialSource.Namespace != "" {
source, ok := q.sources[initialSource]
if !ok {
return nil, nil, fmt.Errorf("CatalogSource %s not found", initialSource.Name)
}

bundle, err := q.findChannelHead(currentVersion, pkgName, channelName, source)
if bundle != nil {
return bundle, &initialSource, nil
}
if err != nil {
errs = append(errs, err)
}

bundle, err = source.GetReplacementBundleInPackageChannel(context.TODO(), bundleName, pkgName, channelName)
if bundle != nil {
return bundle, &initialSource, nil
}
if err != nil {
errs = append(errs, err)
}

return nil, nil, errors.NewAggregate(errs)
}

for key, source := range q.sources {
bundle, err := q.findChannelHead(currentVersion, pkgName, channelName, source)
if bundle != nil {
return bundle, &initialSource, nil
}
if err != nil {
errs = append(errs, err)
}

bundle, err = source.GetReplacementBundleInPackageChannel(context.TODO(), bundleName, pkgName, channelName)
if bundle != nil {
return bundle, &key, nil
}
if err != nil {
errs = append(errs, err)
}
}
return nil, nil, errors.NewAggregate(errs)
}

func (q *NamespaceSourceQuerier) findChannelHead(currentVersion *semver.Version, pkgName, channelName string, source client.Interface) (*api.Bundle, error) {
if currentVersion == nil {
return nil, nil
}

latest, err := source.GetBundleInPackageChannel(context.TODO(), pkgName, channelName)
if err != nil {
return nil, err
}

if latest.SkipRange == "" {
return nil, nil
}

r, err := semver.ParseRange(latest.SkipRange)
if err != nil {
return nil, err
}

if r(*currentVersion) {
return latest, nil
}
return nil, nil
}
Loading