Skip to content

Commit c5230bd

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

16 files changed

+300
-1044
lines changed

cmd/manager/main.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
"github.com/operator-framework/operator-controller/internal/resolve"
2324
"net/url"
2425
"os"
2526
"path/filepath"
@@ -230,16 +231,19 @@ func main() {
230231
}
231232

232233
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,
234+
Client: cl,
235+
Resolver: &resolve.CatalogResolver{
236+
Client: cl,
237+
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
238+
CatalogPackageProvider: catalogClient,
239+
},
240+
ActionClientGetter: acg,
241+
Unpacker: unpacker,
242+
Storage: localStorage,
243+
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
244+
Finalizers: clusterExtensionFinalizers,
245+
CaCertDir: caCertDir,
246+
Preflights: preflights,
243247
}).SetupWithManager(mgr); err != nil {
244248
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
245249
os.Exit(1)

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ require (
2929
sigs.k8s.io/controller-runtime v0.18.4
3030
)
3131

32+
replace github.com/operator-framework/rukpak => ../rukpak
33+
3234
require (
3335
carvel.dev/kapp v0.62.1-0.20240508153820-7d8a03ed7ccf // indirect
3436
cloud.google.com/go/compute/metadata v0.3.0 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,6 @@ github.com/operator-framework/operator-lib v0.14.0 h1:er+BgZymZD1im2wytLJiPLZpGA
614614
github.com/operator-framework/operator-lib v0.14.0/go.mod h1:wUu4Xb9xzXnIpglvaZ3yucTMSlqGXHIoUEH9+5gWiu0=
615615
github.com/operator-framework/operator-registry v1.44.0 h1:NW5/xHYR77J2EUYm+6iBER1WNGLNS8gM+G5GBQWqTTs=
616616
github.com/operator-framework/operator-registry v1.44.0/go.mod h1:55I4XJ//Erir98Mm9OlD8UURWE0HQgL/zlvpGF+gkig=
617-
github.com/operator-framework/rukpak v0.24.0 h1:zxLyk931w4isFiTPox4soXxh/eQJr5sjq15gHcb6dlE=
618-
github.com/operator-framework/rukpak v0.24.0/go.mod h1:peTAGzf4gmU+in2RJen84ZQ8lkdB3m6qy+nfNiVv0RY=
619617
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
620618
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
621619
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=

internal/bundleutil/bundle.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package bundleutil
33
import (
44
"encoding/json"
55
"fmt"
6-
"sync"
7-
86
bsemver "github.com/blang/semver/v4"
97

108
"github.com/operator-framework/operator-registry/alpha/declcfg"
@@ -13,19 +11,7 @@ import (
1311
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1412
)
1513

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-
}
14+
func GetVersion(b declcfg.Bundle) (*bsemver.Version, error) {
2915
for _, p := range b.Properties {
3016
if p.Type == property.TypePackage {
3117
var pkg property.Package
@@ -36,7 +22,6 @@ func (v *versionGetter) GetVersion(b *declcfg.Bundle) (*bsemver.Version, error)
3622
if err != nil {
3723
return nil, err
3824
}
39-
v.cache[b] = &vers
4025
return &(*(&vers)), nil
4126
}
4227
}

internal/catalogmetadata/client/client.go

-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io/fs"
8-
"testing/fstest"
9-
108
"k8s.io/apimachinery/pkg/api/meta"
119
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1210

@@ -43,8 +41,6 @@ func (c *Client) GetPackage(ctx context.Context, catalog *catalogd.ClusterCatalo
4341
return nil, fmt.Errorf("catalog %q is not unpacked", catalog.Name)
4442
}
4543

46-
var _ fs.SubFS = fstest.MapFS{}
47-
4844
catalogFsys, err := c.fetcher.FetchCatalogContents(ctx, catalog)
4945
if err != nil {
5046
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,8 @@
11
package filter
22

33
import (
4-
"sort"
4+
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
5+
"slices"
56
"testing"
67

78
bsemver "github.com/blang/semver/v4"
@@ -17,7 +18,6 @@ import (
1718

1819
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1920
"github.com/operator-framework/operator-controller/internal/bundleutil"
20-
catalogsort "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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort
22

33
import (
4+
"fmt"
45
"k8s.io/apimachinery/pkg/util/sets"
56

67
"github.com/operator-framework/operator-registry/alpha/declcfg"
@@ -10,26 +11,28 @@ import (
1011

1112
// ByVersion is a sort "less" function that orders bundles
1213
// 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)
14+
func ByVersion(b1, b2 declcfg.Bundle) int {
15+
v1, err1 := bundleutil.GetVersion(b1)
16+
v2, err2 := bundleutil.GetVersion(b2)
1617
if err1 != nil || err2 != nil {
1718
return compareErrors(err1, err2)
1819
}
1920

21+
fmt.Println(v1, v2, v2.Compare(*v1))
22+
2023
// Check for "greater than" because
2124
// we want higher versions on top
2225
return v2.Compare(*v1)
2326
}
2427

25-
func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b *declcfg.Bundle) int {
28+
func ByDeprecationFunc(deprecation declcfg.Deprecation) func(a, b declcfg.Bundle) int {
2629
deprecatedBundles := sets.New[string]()
2730
for _, entry := range deprecation.Entries {
2831
if entry.Reference.Schema == declcfg.SchemaBundle {
2932
deprecatedBundles.Insert(entry.Reference.Name)
3033
}
3134
}
32-
return func(a, b *declcfg.Bundle) int {
35+
return func(a, b declcfg.Bundle) int {
3336
aDeprecated := deprecatedBundles.Has(a.Name)
3437
bDeprecated := deprecatedBundles.Has(b.Name)
3538
if aDeprecated && !bDeprecated {

internal/catalogmetadata/sort/sort_test.go

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

33
import (
44
"encoding/json"
5-
"sort"
5+
"github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
6+
"slices"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
910

1011
"github.com/operator-framework/operator-registry/alpha/declcfg"
1112
"github.com/operator-framework/operator-registry/alpha/property"
12-
13-
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
1413
)
1514

1615
func TestByVersion(t *testing.T) {
@@ -56,27 +55,13 @@ func TestByVersion(t *testing.T) {
5655

5756
t.Run("all bundles valid", func(t *testing.T) {
5857
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])
58+
slices.SortStableFunc(toSort, sort.ByVersion)
59+
assert.Equal(t, []declcfg.Bundle{b1, b3, b2}, toSort)
6760
})
6861

6962
t.Run("some bundles are missing version", func(t *testing.T) {
7063
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])
64+
slices.SortStableFunc(toSort, sort.ByVersion)
65+
assert.Equal(t, []declcfg.Bundle{b1, b3, b2, b4noVersion, b5empty}, toSort)
8166
})
8267
}

0 commit comments

Comments
 (0)