Skip to content

Commit a03a969

Browse files
committed
wip2: switch to resolver interface
Signed-off-by: Joe Lanford <[email protected]>
1 parent afdfe7b commit a03a969

15 files changed

+911
-1176
lines changed

cmd/manager/main.go

+31-18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"github.com/operator-framework/operator-controller/internal/controllers"
5353
"github.com/operator-framework/operator-controller/internal/httputil"
5454
"github.com/operator-framework/operator-controller/internal/labels"
55+
"github.com/operator-framework/operator-controller/internal/resolve"
5556
"github.com/operator-framework/operator-controller/internal/version"
5657
"github.com/operator-framework/operator-controller/pkg/features"
5758
"github.com/operator-framework/operator-controller/pkg/scheme"
@@ -155,14 +156,6 @@ func main() {
155156
os.Exit(1)
156157
}
157158

158-
httpClient, err := httputil.BuildHTTPClient(caCertDir)
159-
if err != nil {
160-
setupLog.Error(err, "unable to create catalogd http client")
161-
}
162-
163-
cl := mgr.GetClient()
164-
catalogClient := catalogclient.New(cache.NewFilesystemCache(cachePath, httpClient))
165-
166159
installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
167160
ext := obj.(*ocv1alpha1.ClusterExtension)
168161
return ext.Spec.InstallNamespace, nil
@@ -229,17 +222,37 @@ func main() {
229222
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
230223
}
231224

225+
cl := mgr.GetClient()
226+
httpClient, err := httputil.BuildHTTPClient(caCertDir)
227+
if err != nil {
228+
setupLog.Error(err, "unable to create catalogd http client")
229+
}
230+
catalogClient := catalogclient.New(cache.NewFilesystemCache(cachePath, httpClient))
231+
232+
resolver := &resolve.CatalogResolver{
233+
WalkCatalogsFunc: resolve.CatalogWalker(
234+
func(ctx context.Context, option ...client.ListOption) ([]catalogd.ClusterCatalog, error) {
235+
var catalogs catalogd.ClusterCatalogList
236+
if err := cl.List(ctx, &catalogs, option...); err != nil {
237+
return nil, err
238+
}
239+
return catalogs.Items, nil
240+
},
241+
catalogClient.GetPackage,
242+
),
243+
}
244+
232245
if err = (&controllers.ClusterExtensionReconciler{
233-
Client: cl,
234-
CatalogPackageProvider: catalogClient,
235-
ActionClientGetter: acg,
236-
Unpacker: unpacker,
237-
Storage: localStorage,
238-
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
239-
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
240-
Finalizers: clusterExtensionFinalizers,
241-
CaCertDir: caCertDir,
242-
Preflights: preflights,
246+
Client: cl,
247+
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
248+
Resolver: resolver,
249+
ActionClientGetter: acg,
250+
Unpacker: unpacker,
251+
Storage: localStorage,
252+
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
253+
Finalizers: clusterExtensionFinalizers,
254+
CaCertDir: caCertDir,
255+
Preflights: preflights,
243256
}).SetupWithManager(mgr); err != nil {
244257
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
245258
os.Exit(1)

internal/bundleutil/bundle.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bundleutil
33
import (
44
"encoding/json"
55
"fmt"
6-
"sync"
76

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

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

16-
var VersionGetter = &versionGetter{cache: make(map[*declcfg.Bundle]*bsemver.Version)}
17-
18-
type versionGetter struct {
19-
mu sync.Mutex
20-
cache map[*declcfg.Bundle]*bsemver.Version
21-
}
22-
23-
func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error) {
24-
v.mu.Lock()
25-
defer v.mu.Unlock()
26-
if vers, ok := v.cache[b]; ok {
27-
return &(*vers), nil
28-
}
15+
func GetVersion(b declcfg.Bundle) (*bsemver.Version, error) {
2916
for _, p := range b.Properties {
3017
if p.Type == property.TypePackage {
3118
var pkg property.Package
@@ -36,8 +23,7 @@ func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error)
3623
if err != nil {
3724
return nil, err
3825
}
39-
v.cache[b] = &vers
40-
return &(*(&vers)), nil
26+
return &vers, nil
4127
}
4228
}
4329
return nil, fmt.Errorf("no package property found in bundle %q", b.Name)

internal/catalogmetadata/client/client.go

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io/fs"
8-
"testing/fstest"
98

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

46-
var _ fs.SubFS = fstest.MapFS{}
47-
4845
catalogFsys, err := c.fetcher.FetchCatalogContents(ctx, catalog)
4946
if err != nil {
5047
return nil, fmt.Errorf("error fetching catalog contents: %v", err)

internal/catalogmetadata/filter/bundle_predicates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func InMastermindsSemverRange(semverRange *mmsemver.Constraints) Predicate[declcfg.Bundle] {
1212
return func(b declcfg.Bundle) bool {
13-
bVersion, err := bundleutil.VersionGetter.GetVersion(&b)
13+
bVersion, err := bundleutil.GetVersion(b)
1414
if err != nil {
1515
return false
1616
}

internal/catalogmetadata/filter/successors_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package filter
22

33
import (
4-
"sort"
4+
"slices"
55
"testing"
66

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

1818
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1919
"github.com/operator-framework/operator-controller/internal/bundleutil"
20-
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
20+
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
2121
"github.com/operator-framework/operator-controller/pkg/features"
2222
)
2323

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

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

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

342340
// sort before comparison for stable order
343-
sort.Slice(result, func(i, j int) bool {
344-
return catalogsort.ByVersion(&result[i], &result[j]) < 0
345-
})
341+
slices.SortFunc(result, sort.ByVersion)
346342

347343
gocmpopts := []cmp.Option{
348344
cmpopts.IgnoreUnexported(declcfg.Bundle{}),

internal/catalogmetadata/sort/sort.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
// ByVersion is a sort "less" function that orders bundles
1212
// in inverse version order (higher versions on top).
13-
func ByVersion(b1, b2 *declcfg.Bundle) int {
14-
v1, err1 := bundleutil.VersionGetter.GetVersion(b1)
15-
v2, err2 := bundleutil.VersionGetter.GetVersion(b2)
13+
func ByVersion(b1, b2 declcfg.Bundle) int {
14+
v1, err1 := bundleutil.GetVersion(b1)
15+
v2, err2 := bundleutil.GetVersion(b2)
1616
if err1 != nil || err2 != nil {
1717
return compareErrors(err1, err2)
1818
}
@@ -22,14 +22,14 @@ func ByVersion(b1, b2 *declcfg.Bundle) int {
2222
return v2.Compare(*v1)
2323
}
2424

25-
func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b *declcfg.Bundle) int {
25+
func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b declcfg.Bundle) int {
2626
deprecatedBundles := sets.New[string]()
2727
for _, entry := range deprecation.Entries {
2828
if entry.Reference.Schema == declcfg.SchemaBundle {
2929
deprecatedBundles.Insert(entry.Reference.Name)
3030
}
3131
}
32-
return func(a, b *declcfg.Bundle) int {
32+
return func(a, b declcfg.Bundle) int {
3333
aDeprecated := deprecatedBundles.Has(a.Name)
3434
bDeprecated := deprecatedBundles.Has(b.Name)
3535
if aDeprecated && !bDeprecated {

internal/catalogmetadata/sort/sort_test.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package sort_test
22

33
import (
44
"encoding/json"
5-
"sort"
5+
"slices"
66
"testing"
77

88
"github.com/stretchr/testify/assert"
99

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

13-
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
13+
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
1414
)
1515

1616
func TestByVersion(t *testing.T) {
@@ -56,27 +56,13 @@ func TestByVersion(t *testing.T) {
5656

5757
t.Run("all bundles valid", func(t *testing.T) {
5858
toSort := []declcfg.Bundle{b3, b2, b1}
59-
sort.Slice(toSort, func(i, j int) bool {
60-
return catalogsort.ByVersion(&toSort[i], &toSort[j]) < 0
61-
})
62-
63-
assert.Len(t, toSort, 3)
64-
assert.Equal(t, b1, toSort[0])
65-
assert.Equal(t, b3, toSort[1])
66-
assert.Equal(t, b2, toSort[2])
59+
slices.SortStableFunc(toSort, sort.ByVersion)
60+
assert.Equal(t, []declcfg.Bundle{b1, b3, b2}, toSort)
6761
})
6862

6963
t.Run("some bundles are missing version", func(t *testing.T) {
7064
toSort := []declcfg.Bundle{b3, b4noVersion, b2, b5empty, b1}
71-
sort.Slice(toSort, func(i, j int) bool {
72-
return catalogsort.ByVersion(&toSort[i], &toSort[j]) < 0
73-
})
74-
75-
assert.Len(t, toSort, 5)
76-
assert.Equal(t, b1, toSort[0])
77-
assert.Equal(t, b3, toSort[1])
78-
assert.Equal(t, b2, toSort[2])
79-
assert.Equal(t, b4noVersion, toSort[3])
80-
assert.Equal(t, b5empty, toSort[4])
65+
slices.SortStableFunc(toSort, sort.ByVersion)
66+
assert.Equal(t, []declcfg.Bundle{b1, b3, b2, b4noVersion, b5empty}, toSort)
8167
})
8268
}

0 commit comments

Comments
 (0)