|
| 1 | +//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 |
| 2 | +//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_registry_interface.go ../../registry/registry_client.go ClientInterface |
| 3 | +package catalog |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/blang/semver/v4" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/util/errors" |
| 12 | + |
| 13 | + "github.com/operator-framework/operator-registry/pkg/api" |
| 14 | + "github.com/operator-framework/operator-registry/pkg/client" |
| 15 | + |
| 16 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry" |
| 17 | +) |
| 18 | + |
| 19 | +const SkipPackageAnnotationKey = "olm.skipRange" |
| 20 | + |
| 21 | +type SourceRef struct { |
| 22 | + Address string |
| 23 | + Client client.Interface |
| 24 | + LastConnect metav1.Time |
| 25 | + LastHealthy metav1.Time |
| 26 | +} |
| 27 | + |
| 28 | +type SourceQuerier interface { |
| 29 | + FindReplacement(currentVersion *semver.Version, bundleName, pkgName, channelName string, initialSource registry.CatalogKey) (*api.Bundle, *registry.CatalogKey, error) |
| 30 | + Queryable() error |
| 31 | +} |
| 32 | + |
| 33 | +type NamespaceSourceQuerier struct { |
| 34 | + sources map[registry.CatalogKey]registry.ClientInterface |
| 35 | +} |
| 36 | + |
| 37 | +var _ SourceQuerier = &NamespaceSourceQuerier{} |
| 38 | + |
| 39 | +func NewNamespaceSourceQuerier(sources map[registry.CatalogKey]registry.ClientInterface) *NamespaceSourceQuerier { |
| 40 | + return &NamespaceSourceQuerier{ |
| 41 | + sources: sources, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (q *NamespaceSourceQuerier) Queryable() error { |
| 46 | + if len(q.sources) == 0 { |
| 47 | + return fmt.Errorf("no catalog sources available") |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (q *NamespaceSourceQuerier) FindReplacement(currentVersion *semver.Version, bundleName, pkgName, channelName string, initialSource registry.CatalogKey) (*api.Bundle, *registry.CatalogKey, error) { |
| 53 | + errs := []error{} |
| 54 | + |
| 55 | + if initialSource.Name != "" && initialSource.Namespace != "" { |
| 56 | + source, ok := q.sources[initialSource] |
| 57 | + if !ok { |
| 58 | + return nil, nil, fmt.Errorf("CatalogSource %s not found", initialSource.Name) |
| 59 | + } |
| 60 | + |
| 61 | + bundle, err := q.findChannelHead(currentVersion, pkgName, channelName, source) |
| 62 | + if bundle != nil { |
| 63 | + return bundle, &initialSource, nil |
| 64 | + } |
| 65 | + if err != nil { |
| 66 | + errs = append(errs, err) |
| 67 | + } |
| 68 | + |
| 69 | + bundle, err = source.GetReplacementBundleInPackageChannel(context.TODO(), bundleName, pkgName, channelName) |
| 70 | + if bundle != nil { |
| 71 | + return bundle, &initialSource, nil |
| 72 | + } |
| 73 | + if err != nil { |
| 74 | + errs = append(errs, err) |
| 75 | + } |
| 76 | + |
| 77 | + return nil, nil, errors.NewAggregate(errs) |
| 78 | + } |
| 79 | + |
| 80 | + for key, source := range q.sources { |
| 81 | + bundle, err := q.findChannelHead(currentVersion, pkgName, channelName, source) |
| 82 | + if bundle != nil { |
| 83 | + return bundle, &initialSource, nil |
| 84 | + } |
| 85 | + if err != nil { |
| 86 | + errs = append(errs, err) |
| 87 | + } |
| 88 | + |
| 89 | + bundle, err = source.GetReplacementBundleInPackageChannel(context.TODO(), bundleName, pkgName, channelName) |
| 90 | + if bundle != nil { |
| 91 | + return bundle, &key, nil |
| 92 | + } |
| 93 | + if err != nil { |
| 94 | + errs = append(errs, err) |
| 95 | + } |
| 96 | + } |
| 97 | + return nil, nil, errors.NewAggregate(errs) |
| 98 | +} |
| 99 | + |
| 100 | +func (q *NamespaceSourceQuerier) findChannelHead(currentVersion *semver.Version, pkgName, channelName string, source client.Interface) (*api.Bundle, error) { |
| 101 | + if currentVersion == nil { |
| 102 | + return nil, nil |
| 103 | + } |
| 104 | + |
| 105 | + latest, err := source.GetBundleInPackageChannel(context.TODO(), pkgName, channelName) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + if latest.SkipRange == "" { |
| 111 | + return nil, nil |
| 112 | + } |
| 113 | + |
| 114 | + r, err := semver.ParseRange(latest.SkipRange) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + if r(*currentVersion) { |
| 120 | + return latest, nil |
| 121 | + } |
| 122 | + return nil, nil |
| 123 | +} |
0 commit comments