Skip to content

Commit

Permalink
wip2: switch to resolver interface
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Jul 12, 2024
1 parent afdfe7b commit 3664e64
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 1,123 deletions.
39 changes: 29 additions & 10 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/httputil"
"github.com/operator-framework/operator-controller/internal/labels"
"github.com/operator-framework/operator-controller/internal/resolve"
"github.com/operator-framework/operator-controller/internal/version"
"github.com/operator-framework/operator-controller/pkg/features"
"github.com/operator-framework/operator-controller/pkg/scheme"
Expand Down Expand Up @@ -229,17 +230,35 @@ func main() {
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
}

resolver := &resolve.CatalogResolver{
WalkCatalogsFunc: func(ctx context.Context, packageName string, f resolve.CatalogWalkFunc, catalogListOpts ...client.ListOption) error {
var catalogs catalogd.ClusterCatalogList
if err := cl.List(ctx, &catalogs, catalogListOpts...); err != nil {
return fmt.Errorf("error listing catalogs: %w", err)
}

for i := range catalogs.Items {
cat := &catalogs.Items[i]
fbc, err := catalogClient.GetPackage(ctx, cat, packageName)
if err := f(ctx, cat, fbc, err); err != nil {
return err
}
}
return nil
},
}

if err = (&controllers.ClusterExtensionReconciler{
Client: cl,
CatalogPackageProvider: catalogClient,
ActionClientGetter: acg,
Unpacker: unpacker,
Storage: localStorage,
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
Finalizers: clusterExtensionFinalizers,
CaCertDir: caCertDir,
Preflights: preflights,
Client: cl,
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
Resolver: resolver,
ActionClientGetter: acg,
Unpacker: unpacker,
Storage: localStorage,
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
Finalizers: clusterExtensionFinalizers,
CaCertDir: caCertDir,
Preflights: preflights,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
os.Exit(1)
Expand Down
18 changes: 2 additions & 16 deletions internal/bundleutil/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bundleutil
import (
"encoding/json"
"fmt"
"sync"

bsemver "github.com/blang/semver/v4"

Expand All @@ -13,19 +12,7 @@ import (
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
)

var VersionGetter = &versionGetter{cache: make(map[*declcfg.Bundle]*bsemver.Version)}

type versionGetter struct {
mu sync.Mutex
cache map[*declcfg.Bundle]*bsemver.Version
}

func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error) {
v.mu.Lock()
defer v.mu.Unlock()
if vers, ok := v.cache[b]; ok {
return &(*vers), nil
}
func GetVersion(b declcfg.Bundle) (*bsemver.Version, error) {
for _, p := range b.Properties {
if p.Type == property.TypePackage {
var pkg property.Package
Expand All @@ -36,8 +23,7 @@ func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error)
if err != nil {
return nil, err
}
v.cache[b] = &vers
return &(*(&vers)), nil
return &vers, nil
}
}
return nil, fmt.Errorf("no package property found in bundle %q", b.Name)
Expand Down
3 changes: 0 additions & 3 deletions internal/catalogmetadata/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/fs"
"testing/fstest"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -43,8 +42,6 @@ func (c *Client) GetPackage(ctx context.Context, catalog *catalogd.ClusterCatalo
return nil, fmt.Errorf("catalog %q is not unpacked", catalog.Name)
}

var _ fs.SubFS = fstest.MapFS{}

catalogFsys, err := c.fetcher.FetchCatalogContents(ctx, catalog)
if err != nil {
return nil, fmt.Errorf("error fetching catalog contents: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/catalogmetadata/filter/bundle_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func InMastermindsSemverRange(semverRange *mmsemver.Constraints) Predicate[declcfg.Bundle] {
return func(b declcfg.Bundle) bool {
bVersion, err := bundleutil.VersionGetter.GetVersion(&b)
bVersion, err := bundleutil.GetVersion(b)
if err != nil {
return false
}
Expand Down
12 changes: 4 additions & 8 deletions internal/catalogmetadata/filter/successors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package filter

import (
"sort"
"slices"
"testing"

bsemver "github.com/blang/semver/v4"
Expand All @@ -17,7 +17,7 @@ import (

ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/bundleutil"
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
"github.com/operator-framework/operator-controller/pkg/features"
)

Expand Down Expand Up @@ -181,9 +181,7 @@ func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsEnabled(t *testing.
result := Filter(allBundles, successors)

// sort before comparison for stable order
sort.Slice(result, func(i, j int) bool {
return catalogsort.ByVersion(&result[i], &result[j]) < 0
})
slices.SortFunc(result, sort.ByVersion)

gocmpopts := []cmp.Option{
cmpopts.IgnoreUnexported(declcfg.Bundle{}),
Expand Down Expand Up @@ -340,9 +338,7 @@ func TestSuccessorsPredicateWithForceSemverUpgradeConstraintsDisabled(t *testing
result := Filter(allBundles, successors)

// sort before comparison for stable order
sort.Slice(result, func(i, j int) bool {
return catalogsort.ByVersion(&result[i], &result[j]) < 0
})
slices.SortFunc(result, sort.ByVersion)

gocmpopts := []cmp.Option{
cmpopts.IgnoreUnexported(declcfg.Bundle{}),
Expand Down
14 changes: 9 additions & 5 deletions internal/catalogmetadata/sort/sort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sort

import (
"fmt"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/operator-framework/operator-registry/alpha/declcfg"
Expand All @@ -10,26 +12,28 @@ import (

// ByVersion is a sort "less" function that orders bundles
// in inverse version order (higher versions on top).
func ByVersion(b1, b2 *declcfg.Bundle) int {
v1, err1 := bundleutil.VersionGetter.GetVersion(b1)
v2, err2 := bundleutil.VersionGetter.GetVersion(b2)
func ByVersion(b1, b2 declcfg.Bundle) int {
v1, err1 := bundleutil.GetVersion(b1)
v2, err2 := bundleutil.GetVersion(b2)
if err1 != nil || err2 != nil {
return compareErrors(err1, err2)
}

fmt.Println(v1, v2, v2.Compare(*v1))

// Check for "greater than" because
// we want higher versions on top
return v2.Compare(*v1)
}

func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b *declcfg.Bundle) int {
func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b declcfg.Bundle) int {
deprecatedBundles := sets.New[string]()
for _, entry := range deprecation.Entries {
if entry.Reference.Schema == declcfg.SchemaBundle {
deprecatedBundles.Insert(entry.Reference.Name)
}
}
return func(a, b *declcfg.Bundle) int {
return func(a, b declcfg.Bundle) int {
aDeprecated := deprecatedBundles.Has(a.Name)
bDeprecated := deprecatedBundles.Has(b.Name)
if aDeprecated && !bDeprecated {
Expand Down
26 changes: 6 additions & 20 deletions internal/catalogmetadata/sort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package sort_test

import (
"encoding/json"
"sort"
"slices"
"testing"

"github.com/stretchr/testify/assert"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"

catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
)

func TestByVersion(t *testing.T) {
Expand Down Expand Up @@ -56,27 +56,13 @@ func TestByVersion(t *testing.T) {

t.Run("all bundles valid", func(t *testing.T) {
toSort := []declcfg.Bundle{b3, b2, b1}
sort.Slice(toSort, func(i, j int) bool {
return catalogsort.ByVersion(&toSort[i], &toSort[j]) < 0
})

assert.Len(t, toSort, 3)
assert.Equal(t, b1, toSort[0])
assert.Equal(t, b3, toSort[1])
assert.Equal(t, b2, toSort[2])
slices.SortStableFunc(toSort, sort.ByVersion)
assert.Equal(t, []declcfg.Bundle{b1, b3, b2}, toSort)
})

t.Run("some bundles are missing version", func(t *testing.T) {
toSort := []declcfg.Bundle{b3, b4noVersion, b2, b5empty, b1}
sort.Slice(toSort, func(i, j int) bool {
return catalogsort.ByVersion(&toSort[i], &toSort[j]) < 0
})

assert.Len(t, toSort, 5)
assert.Equal(t, b1, toSort[0])
assert.Equal(t, b3, toSort[1])
assert.Equal(t, b2, toSort[2])
assert.Equal(t, b4noVersion, toSort[3])
assert.Equal(t, b5empty, toSort[4])
slices.SortStableFunc(toSort, sort.ByVersion)
assert.Equal(t, []declcfg.Bundle{b1, b3, b2, b4noVersion, b5empty}, toSort)
})
}
Loading

0 comments on commit 3664e64

Please sign in to comment.