Skip to content

Commit 2c3176d

Browse files
authored
Move data fetching to a single place (#508)
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 9081f96 commit 2c3176d

12 files changed

+121
-158
lines changed

internal/controllers/variable_source.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,68 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
21+
2022
"sigs.k8s.io/controller-runtime/pkg/client"
2123

24+
"github.com/operator-framework/deppy/pkg/deppy"
2225
"github.com/operator-framework/deppy/pkg/deppy/input"
26+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2327

28+
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
29+
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
2430
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
2531
)
2632

27-
func NewVariableSource(cl client.Client, catalogClient variablesources.BundleProvider) variablesources.NestedVariableSource {
28-
return variablesources.NestedVariableSource{
33+
// BundleProvider provides the way to retrieve a list of Bundles from a source,
34+
// generally from a catalog client of some kind.
35+
type BundleProvider interface {
36+
Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error)
37+
}
38+
39+
type VariableSource struct {
40+
client client.Client
41+
catalogClient BundleProvider
42+
}
43+
44+
func NewVariableSource(cl client.Client, catalogClient BundleProvider) *VariableSource {
45+
return &VariableSource{
46+
client: cl,
47+
catalogClient: catalogClient,
48+
}
49+
}
50+
51+
func (v *VariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, error) {
52+
operatorList := operatorsv1alpha1.OperatorList{}
53+
if err := v.client.List(ctx, &operatorList); err != nil {
54+
return nil, err
55+
}
56+
57+
bundleDeploymentList := rukpakv1alpha1.BundleDeploymentList{}
58+
if err := v.client.List(ctx, &bundleDeploymentList); err != nil {
59+
return nil, err
60+
}
61+
62+
allBundles, err := v.catalogClient.Bundles(ctx)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
// We are in process of getting rid of extra variable sources.
68+
// See this for progress: https://github.com/operator-framework/operator-controller/issues/437
69+
vs := variablesources.NestedVariableSource{
2970
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
30-
return variablesources.NewOperatorVariableSource(cl, catalogClient, inputVariableSource), nil
71+
return variablesources.NewOperatorVariableSource(operatorList.Items, allBundles, inputVariableSource), nil
3172
},
3273
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
33-
return variablesources.NewBundleDeploymentVariableSource(cl, catalogClient, inputVariableSource), nil
74+
return variablesources.NewBundleDeploymentVariableSource(bundleDeploymentList.Items, allBundles, inputVariableSource), nil
3475
},
3576
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
36-
return variablesources.NewBundlesAndDepsVariableSource(catalogClient, inputVariableSource), nil
77+
return variablesources.NewBundlesAndDepsVariableSource(allBundles, inputVariableSource), nil
3778
},
3879
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
3980
return variablesources.NewCRDUniquenessConstraintsVariableSource(inputVariableSource), nil
4081
},
4182
}
83+
return vs.GetVariables(ctx)
4284
}

internal/resolution/variablesources/bundle_deployment.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package variablesources
33
import (
44
"context"
55

6+
"k8s.io/apimachinery/pkg/util/sets"
7+
68
"github.com/operator-framework/deppy/pkg/deppy"
79
"github.com/operator-framework/deppy/pkg/deppy/input"
810
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
9-
"k8s.io/apimachinery/pkg/util/sets"
10-
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
1113
)
1214

1315
var _ input.VariableSource = &BundleDeploymentVariableSource{}
1416

1517
type BundleDeploymentVariableSource struct {
16-
client client.Client
17-
catalogClient BundleProvider
18+
bundleDeployments []rukpakv1alpha1.BundleDeployment
19+
allBundles []*catalogmetadata.Bundle
1820
inputVariableSource input.VariableSource
1921
}
2022

21-
func NewBundleDeploymentVariableSource(cl client.Client, catalogClient BundleProvider, inputVariableSource input.VariableSource) *BundleDeploymentVariableSource {
23+
func NewBundleDeploymentVariableSource(bundleDeployments []rukpakv1alpha1.BundleDeployment, allBundles []*catalogmetadata.Bundle, inputVariableSource input.VariableSource) *BundleDeploymentVariableSource {
2224
return &BundleDeploymentVariableSource{
23-
client: cl,
24-
catalogClient: catalogClient,
25+
bundleDeployments: bundleDeployments,
26+
allBundles: allBundles,
2527
inputVariableSource: inputVariableSource,
2628
}
2729
}
@@ -32,20 +34,15 @@ func (o *BundleDeploymentVariableSource) GetVariables(ctx context.Context) ([]de
3234
variableSources = append(variableSources, o.inputVariableSource)
3335
}
3436

35-
bundleDeployments := rukpakv1alpha1.BundleDeploymentList{}
36-
if err := o.client.List(ctx, &bundleDeployments); err != nil {
37-
return nil, err
38-
}
39-
4037
processed := sets.Set[string]{}
41-
for _, bundleDeployment := range bundleDeployments.Items {
38+
for _, bundleDeployment := range o.bundleDeployments {
4239
sourceImage := bundleDeployment.Spec.Template.Spec.Source.Image
4340
if sourceImage != nil && sourceImage.Ref != "" {
4441
if processed.Has(sourceImage.Ref) {
4542
continue
4643
}
4744
processed.Insert(sourceImage.Ref)
48-
ips, err := NewInstalledPackageVariableSource(o.catalogClient, bundleDeployment.Spec.Template.Spec.Source.Image.Ref)
45+
ips, err := NewInstalledPackageVariableSource(o.allBundles, bundleDeployment.Spec.Template.Spec.Source.Image.Ref)
4946
if err != nil {
5047
return nil, err
5148
}

internal/resolution/variablesources/bundle_deployment_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,15 @@ import (
1313
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
1414
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
1515
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
16-
testutil "github.com/operator-framework/operator-controller/test/util"
1716

1817
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19-
"k8s.io/apimachinery/pkg/runtime"
20-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
21-
"sigs.k8s.io/controller-runtime/pkg/client"
22-
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2318

2419
"github.com/operator-framework/deppy/pkg/deppy"
2520
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2621
)
2722

28-
func BundleDeploymentFakeClient(objects ...client.Object) client.Client {
29-
scheme := runtime.NewScheme()
30-
utilruntime.Must(rukpakv1alpha1.AddToScheme(scheme))
31-
return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build()
32-
}
33-
34-
func bundleDeployment(name, image string) *rukpakv1alpha1.BundleDeployment {
35-
return &rukpakv1alpha1.BundleDeployment{
23+
func bundleDeployment(name, image string) rukpakv1alpha1.BundleDeployment {
24+
return rukpakv1alpha1.BundleDeployment{
3625
ObjectMeta: metav1.ObjectMeta{
3726
Name: name,
3827
},
@@ -53,7 +42,6 @@ func bundleDeployment(name, image string) *rukpakv1alpha1.BundleDeployment {
5342
}
5443

5544
var _ = Describe("BundleDeploymentVariableSource", func() {
56-
var fakeCatalogClient testutil.FakeCatalogClient
5745
var betaChannel catalogmetadata.Channel
5846
var stableChannel catalogmetadata.Channel
5947
var testBundleList []*catalogmetadata.Bundle
@@ -111,14 +99,14 @@ var _ = Describe("BundleDeploymentVariableSource", func() {
11199
},
112100
}, InChannels: []*catalogmetadata.Channel{&stableChannel}},
113101
}
114-
115-
fakeCatalogClient = testutil.NewFakeCatalogClient(testBundleList)
116102
})
117103

118104
It("should produce RequiredPackage variables", func() {
119-
cl := BundleDeploymentFakeClient(bundleDeployment("prometheus", "quay.io/operatorhubio/prometheus@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35"))
105+
bundleDeployments := []rukpakv1alpha1.BundleDeployment{
106+
bundleDeployment("prometheus", "quay.io/operatorhubio/prometheus@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35"),
107+
}
120108

121-
bdVariableSource := variablesources.NewBundleDeploymentVariableSource(cl, &fakeCatalogClient, &MockRequiredPackageSource{})
109+
bdVariableSource := variablesources.NewBundleDeploymentVariableSource(bundleDeployments, testBundleList, &MockRequiredPackageSource{})
122110
variables, err := bdVariableSource.GetVariables(context.Background())
123111
Expect(err).ToNot(HaveOccurred())
124112

@@ -137,9 +125,11 @@ var _ = Describe("BundleDeploymentVariableSource", func() {
137125
})))
138126
})
139127
It("should return an error if the bundleDeployment image doesn't match any operator resource", func() {
140-
cl := BundleDeploymentFakeClient(bundleDeployment("prometheus", "quay.io/operatorhubio/prometheus@sha256:nonexistent"))
128+
bundleDeployments := []rukpakv1alpha1.BundleDeployment{
129+
bundleDeployment("prometheus", "quay.io/operatorhubio/prometheus@sha256:nonexistent"),
130+
}
141131

142-
bdVariableSource := variablesources.NewBundleDeploymentVariableSource(cl, &fakeCatalogClient, &MockRequiredPackageSource{})
132+
bdVariableSource := variablesources.NewBundleDeploymentVariableSource(bundleDeployments, testBundleList, &MockRequiredPackageSource{})
143133
_, err := bdVariableSource.GetVariables(context.Background())
144134
Expect(err.Error()).To(Equal("bundleImage \"quay.io/operatorhubio/prometheus@sha256:nonexistent\" not found"))
145135
})

internal/resolution/variablesources/bundle_provider.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

internal/resolution/variablesources/bundles_and_dependencies.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
var _ input.VariableSource = &BundlesAndDepsVariableSource{}
1919

2020
type BundlesAndDepsVariableSource struct {
21-
catalogClient BundleProvider
21+
allBundles []*catalogmetadata.Bundle
2222
variableSources []input.VariableSource
2323
}
2424

25-
func NewBundlesAndDepsVariableSource(catalogClient BundleProvider, inputVariableSources ...input.VariableSource) *BundlesAndDepsVariableSource {
25+
func NewBundlesAndDepsVariableSource(allBundles []*catalogmetadata.Bundle, inputVariableSources ...input.VariableSource) *BundlesAndDepsVariableSource {
2626
return &BundlesAndDepsVariableSource{
27-
catalogClient: catalogClient,
27+
allBundles: allBundles,
2828
variableSources: inputVariableSources,
2929
}
3030
}
@@ -52,11 +52,6 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
5252
}
5353
}
5454

55-
allBundles, err := b.catalogClient.Bundles(ctx)
56-
if err != nil {
57-
return nil, err
58-
}
59-
6055
// build bundle and dependency variables
6156
visited := sets.Set[deppy.Identifier]{}
6257
for len(bundleQueue) > 0 {
@@ -73,7 +68,7 @@ func (b *BundlesAndDepsVariableSource) GetVariables(ctx context.Context) ([]depp
7368
visited.Insert(id)
7469

7570
// get bundle dependencies
76-
dependencies, err := b.filterBundleDependencies(allBundles, head)
71+
dependencies, err := b.filterBundleDependencies(b.allBundles, head)
7772
if err != nil {
7873
return nil, fmt.Errorf("could not determine dependencies for bundle with id '%s': %w", id, err)
7974
}

internal/resolution/variablesources/bundles_and_dependencies_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ import (
1414
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
1515
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
1616
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
17-
testutil "github.com/operator-framework/operator-controller/test/util"
1817
)
1918

2019
var _ = Describe("BundlesAndDepsVariableSource", func() {
2120
var (
22-
bdvs *variablesources.BundlesAndDepsVariableSource
23-
testBundleList []*catalogmetadata.Bundle
24-
fakeCatalogClient testutil.FakeCatalogClient
21+
bdvs *variablesources.BundlesAndDepsVariableSource
22+
testBundleList []*catalogmetadata.Bundle
2523
)
2624

2725
BeforeEach(func() {
@@ -222,12 +220,11 @@ var _ = Describe("BundlesAndDepsVariableSource", func() {
222220
InChannels: []*catalogmetadata.Channel{&channel},
223221
},
224222
}
225-
fakeCatalogClient = testutil.NewFakeCatalogClient(testBundleList)
226223
bdvs = variablesources.NewBundlesAndDepsVariableSource(
227-
&fakeCatalogClient,
224+
testBundleList,
228225
&MockRequiredPackageSource{
229226
ResultSet: []deppy.Variable{
230-
// must match data in fakeCatalogClient
227+
// must match data in testBundleList
231228
olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{
232229
{
233230
CatalogName: "fake-catalog",
@@ -259,7 +256,7 @@ var _ = Describe("BundlesAndDepsVariableSource", func() {
259256
},
260257
&MockRequiredPackageSource{
261258
ResultSet: []deppy.Variable{
262-
// must match data in fakeCatalogClient
259+
// must match data in testBundleList
263260
olmvariables.NewRequiredPackageVariable("test-package-2", []*catalogmetadata.Bundle{
264261
// test-package-2 required package - no dependencies
265262
{
@@ -335,13 +332,10 @@ var _ = Describe("BundlesAndDepsVariableSource", func() {
335332
})
336333

337334
It("should return error if dependencies not found", func() {
338-
emptyCatalogClient := testutil.NewFakeCatalogClient(make([]*catalogmetadata.Bundle, 0))
339-
340335
bdvs = variablesources.NewBundlesAndDepsVariableSource(
341-
&emptyCatalogClient,
336+
[]*catalogmetadata.Bundle{},
342337
&MockRequiredPackageSource{
343338
ResultSet: []deppy.Variable{
344-
// must match data in fakeCatalogClient
345339
olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{
346340
{
347341
CatalogName: "fake-catalog",
@@ -379,7 +373,7 @@ var _ = Describe("BundlesAndDepsVariableSource", func() {
379373

380374
It("should return error if an inner variable source returns an error", func() {
381375
bdvs = variablesources.NewBundlesAndDepsVariableSource(
382-
&fakeCatalogClient,
376+
testBundleList,
383377
&MockRequiredPackageSource{Error: errors.New("fake error")},
384378
)
385379
_, err := bdvs.GetVariables(context.TODO())

internal/resolution/variablesources/installed_package.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ import (
1919
var _ input.VariableSource = &InstalledPackageVariableSource{}
2020

2121
type InstalledPackageVariableSource struct {
22-
catalogClient BundleProvider
23-
successors successorsFunc
24-
bundleImage string
22+
allBundles []*catalogmetadata.Bundle
23+
successors successorsFunc
24+
bundleImage string
2525
}
2626

27-
func (r *InstalledPackageVariableSource) GetVariables(ctx context.Context) ([]deppy.Variable, error) {
28-
allBundles, err := r.catalogClient.Bundles(ctx)
29-
if err != nil {
30-
return nil, err
31-
}
32-
27+
func (r *InstalledPackageVariableSource) GetVariables(_ context.Context) ([]deppy.Variable, error) {
3328
// find corresponding bundle for the installed content
34-
resultSet := catalogfilter.Filter(allBundles, catalogfilter.WithBundleImage(r.bundleImage))
29+
resultSet := catalogfilter.Filter(r.allBundles, catalogfilter.WithBundleImage(r.bundleImage))
3530
if len(resultSet) == 0 {
3631
return nil, r.notFoundError()
3732
}
@@ -44,7 +39,7 @@ func (r *InstalledPackageVariableSource) GetVariables(ctx context.Context) ([]de
4439
})
4540
installedBundle := resultSet[0]
4641

47-
upgradeEdges, err := r.successors(allBundles, installedBundle)
42+
upgradeEdges, err := r.successors(r.allBundles, installedBundle)
4843
if err != nil {
4944
return nil, err
5045
}
@@ -60,16 +55,16 @@ func (r *InstalledPackageVariableSource) notFoundError() error {
6055
return fmt.Errorf("bundleImage %q not found", r.bundleImage)
6156
}
6257

63-
func NewInstalledPackageVariableSource(catalogClient BundleProvider, bundleImage string) (*InstalledPackageVariableSource, error) {
58+
func NewInstalledPackageVariableSource(allBundles []*catalogmetadata.Bundle, bundleImage string) (*InstalledPackageVariableSource, error) {
6459
successors := legacySemanticsSuccessors
6560
if features.OperatorControllerFeatureGate.Enabled(features.ForceSemverUpgradeConstraints) {
6661
successors = semverSuccessors
6762
}
6863

6964
return &InstalledPackageVariableSource{
70-
catalogClient: catalogClient,
71-
bundleImage: bundleImage,
72-
successors: successors,
65+
allBundles: allBundles,
66+
bundleImage: bundleImage,
67+
successors: successors,
7368
}, nil
7469
}
7570

0 commit comments

Comments
 (0)