Skip to content

Commit 365253c

Browse files
author
Mikalai Radchuk
committed
Makes OLMVariableSource responsible for fetching
`OLMVariableSource` now contains the client and makes requests to get available operators. Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 80a5786 commit 365253c

File tree

7 files changed

+77
-78
lines changed

7 files changed

+77
-78
lines changed

cmd/manager/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"flag"
2121
"os"
2222

23+
"github.com/operator-framework/deppy/pkg/deppy/solver"
2324
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2425
"go.uber.org/zap/zapcore"
2526
"k8s.io/apimachinery/pkg/runtime"
@@ -33,8 +34,8 @@ import (
3334
catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
3435
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3536
"github.com/operator-framework/operator-controller/internal/controllers"
36-
"github.com/operator-framework/operator-controller/internal/resolution"
3737
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
38+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
3839
)
3940

4041
var (
@@ -94,9 +95,12 @@ func main() {
9495
}
9596

9697
if err = (&controllers.OperatorReconciler{
97-
Client: mgr.GetClient(),
98-
Scheme: mgr.GetScheme(),
99-
Resolver: resolution.NewOperatorResolver(mgr.GetClient(), entitysources.NewCatalogdEntitySource(mgr.GetClient())),
98+
Client: mgr.GetClient(),
99+
Scheme: mgr.GetScheme(),
100+
Resolver: solver.NewDeppySolver(
101+
entitysources.NewCatalogdEntitySource(mgr.GetClient()),
102+
olm.NewOLMVariableSource(mgr.GetClient()),
103+
),
100104
}).SetupWithManager(mgr); err != nil {
101105
setupLog.Error(err, "unable to create controller", "controller", "Operator")
102106
os.Exit(1)

internal/controllers/operator_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import (
4141

4242
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
4343
"github.com/operator-framework/operator-controller/internal/controllers/validators"
44-
"github.com/operator-framework/operator-controller/internal/resolution"
4544
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
4645
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity"
4746
)
@@ -50,7 +49,7 @@ import (
5049
type OperatorReconciler struct {
5150
client.Client
5251
Scheme *runtime.Scheme
53-
Resolver *resolution.OperatorResolver
52+
Resolver *solver.DeppySolver
5453
}
5554

5655
//+kubebuilder:rbac:groups=operators.operatorframework.io,resources=operators,verbs=get;list;watch
@@ -122,7 +121,7 @@ func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha
122121
return ctrl.Result{}, nil
123122
}
124123
// run resolution
125-
solution, err := r.Resolver.Resolve(ctx)
124+
solution, err := r.Resolver.Solve(ctx)
126125
if err != nil {
127126
op.Status.InstalledBundleResource = ""
128127
setInstalledStatusConditionUnknown(&op.Status.Conditions, "installation has not been attempted as resolution failed", op.GetGeneration())

internal/controllers/operator_controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
. "github.com/onsi/gomega"
99
"github.com/operator-framework/deppy/pkg/deppy"
1010
"github.com/operator-framework/deppy/pkg/deppy/input"
11+
"github.com/operator-framework/deppy/pkg/deppy/solver"
1112
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
1213
apimeta "k8s.io/apimachinery/pkg/api/meta"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -21,7 +22,7 @@ import (
2122
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
2223
"github.com/operator-framework/operator-controller/internal/conditionsets"
2324
"github.com/operator-framework/operator-controller/internal/controllers"
24-
"github.com/operator-framework/operator-controller/internal/resolution"
25+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
2526
)
2627

2728
var _ = Describe("Operator Controller Test", func() {
@@ -34,7 +35,7 @@ var _ = Describe("Operator Controller Test", func() {
3435
reconciler = &controllers.OperatorReconciler{
3536
Client: cl,
3637
Scheme: sch,
37-
Resolver: resolution.NewOperatorResolver(cl, testEntitySource),
38+
Resolver: solver.NewDeppySolver(testEntitySource, olm.NewOLMVariableSource(cl)),
3839
}
3940
})
4041
When("the operator does not exist", func() {

internal/resolution/resolver.go

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

internal/resolution/resolver_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
. "github.com/onsi/gomega"
1010
"github.com/operator-framework/deppy/pkg/deppy"
1111
"github.com/operator-framework/deppy/pkg/deppy/input"
12+
"github.com/operator-framework/deppy/pkg/deppy/solver"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/apimachinery/pkg/runtime"
1415
"sigs.k8s.io/controller-runtime/pkg/client"
1516
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1617

1718
"github.com/operator-framework/operator-controller/api/v1alpha1"
18-
"github.com/operator-framework/operator-controller/internal/resolution"
19+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
1920
)
2021

2122
func TestOperatorResolver(t *testing.T) {
@@ -74,8 +75,9 @@ var _ = Describe("OperatorResolver", func() {
7475
}
7576
client := FakeClient(resources...)
7677
entitySource := input.NewCacheQuerier(testEntityCache)
77-
resolver := resolution.NewOperatorResolver(client, entitySource)
78-
solution, err := resolver.Resolve(context.Background())
78+
variableSource := olm.NewOLMVariableSource(client)
79+
resolver := solver.NewDeppySolver(entitySource, variableSource)
80+
solution, err := resolver.Solve(context.Background())
7981
Expect(err).ToNot(HaveOccurred())
8082
// 2 * required package variables + 2 * bundle variables
8183
Expect(solution.SelectedVariables()).To(HaveLen(4))
@@ -93,8 +95,9 @@ var _ = Describe("OperatorResolver", func() {
9395
var resources []client.Object
9496
client := FakeClient(resources...)
9597
entitySource := input.NewCacheQuerier(testEntityCache)
96-
resolver := resolution.NewOperatorResolver(client, entitySource)
97-
solution, err := resolver.Resolve(context.Background())
98+
variableSource := olm.NewOLMVariableSource(client)
99+
resolver := solver.NewDeppySolver(entitySource, variableSource)
100+
solution, err := resolver.Solve(context.Background())
98101
Expect(err).ToNot(HaveOccurred())
99102
Expect(solution.SelectedVariables()).To(HaveLen(0))
100103
})
@@ -110,17 +113,19 @@ var _ = Describe("OperatorResolver", func() {
110113
}
111114
client := FakeClient(resource)
112115
entitySource := FailEntitySource{}
113-
resolver := resolution.NewOperatorResolver(client, entitySource)
114-
solution, err := resolver.Resolve(context.Background())
116+
variableSource := olm.NewOLMVariableSource(client)
117+
resolver := solver.NewDeppySolver(entitySource, variableSource)
118+
solution, err := resolver.Solve(context.Background())
115119
Expect(solution).To(BeNil())
116120
Expect(err).To(HaveOccurred())
117121
})
118122

119123
It("should return an error if the client throws an error", func() {
120124
client := NewFailClientWithError(fmt.Errorf("something bad happened"))
121125
entitySource := input.NewCacheQuerier(testEntityCache)
122-
resolver := resolution.NewOperatorResolver(client, entitySource)
123-
solution, err := resolver.Resolve(context.Background())
126+
variableSource := olm.NewOLMVariableSource(client)
127+
resolver := solver.NewDeppySolver(entitySource, variableSource)
128+
solution, err := resolver.Solve(context.Background())
124129
Expect(solution).To(BeNil())
125130
Expect(err).To(HaveOccurred())
126131
})

internal/resolution/variable_sources/olm/olm.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/operator-framework/deppy/pkg/deppy"
77
"github.com/operator-framework/deppy/pkg/deppy/input"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
89

910
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1011
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
@@ -15,20 +16,25 @@ import (
1516
var _ input.VariableSource = &OLMVariableSource{}
1617

1718
type OLMVariableSource struct {
18-
operators []operatorsv1alpha1.Operator
19+
client client.Client
1920
}
2021

21-
func NewOLMVariableSource(operators ...operatorsv1alpha1.Operator) *OLMVariableSource {
22+
func NewOLMVariableSource(cl client.Client) *OLMVariableSource {
2223
return &OLMVariableSource{
23-
operators: operators,
24+
client: cl,
2425
}
2526
}
2627

2728
func (o *OLMVariableSource) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) {
29+
operatorList := operatorsv1alpha1.OperatorList{}
30+
if err := o.client.List(ctx, &operatorList); err != nil {
31+
return nil, err
32+
}
33+
2834
var inputVariableSources []input.VariableSource
2935

3036
// build required package variable sources
31-
for _, operator := range o.operators {
37+
for _, operator := range operatorList.Items {
3238
rps, err := required_package.NewRequiredPackage(
3339
operator.Spec.PackageName,
3440
required_package.InVersionRange(operator.Spec.Version),

internal/resolution/variable_sources/olm/olm_test.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ import (
1111
. "github.com/onsi/gomega"
1212
"github.com/operator-framework/deppy/pkg/deppy"
1313
"github.com/operator-framework/deppy/pkg/deppy/input"
14+
"github.com/operator-framework/operator-controller/api/v1alpha1"
1415
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
1516
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
1617
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crd_constraints"
1718
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
1819
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/required_package"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2025
)
2126

27+
func FakeClient(objects ...client.Object) client.Client {
28+
scheme := runtime.NewScheme()
29+
utilruntime.Must(v1alpha1.AddToScheme(scheme))
30+
return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build()
31+
}
32+
2233
func TestGlobalConstraints(t *testing.T) {
2334
RegisterFailHandler(Fail)
2435
RunSpecs(t, "OLMVariableSource Suite")
@@ -59,7 +70,7 @@ func withVersionRange(versionRange string) opOption {
5970
}
6071
}
6172

62-
func operator(name string, opts ...opOption) operatorsv1alpha1.Operator {
73+
func operator(name string, opts ...opOption) *operatorsv1alpha1.Operator {
6374
op := operatorsv1alpha1.Operator{
6475
ObjectMeta: metav1.ObjectMeta{
6576
Name: name,
@@ -73,7 +84,7 @@ func operator(name string, opts ...opOption) operatorsv1alpha1.Operator {
7384
Fail(err.Error())
7485
}
7586
}
76-
return op
87+
return &op
7788
}
7889

7990
var _ = Describe("OLMVariableSource", func() {
@@ -84,20 +95,30 @@ var _ = Describe("OLMVariableSource", func() {
8495
})
8596

8697
It("should produce RequiredPackage variables", func() {
87-
olmVariableSource := olm.NewOLMVariableSource(operator("prometheus"), operator("packageA"))
98+
cl := FakeClient(operator("prometheus"), operator("packageA"))
99+
100+
olmVariableSource := olm.NewOLMVariableSource(cl)
88101
variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource)
89102
Expect(err).ToNot(HaveOccurred())
90103

91104
packageRequiredVariables := filterVariables[*required_package.RequiredPackageVariable](variables)
92105
Expect(packageRequiredVariables).To(HaveLen(2))
93-
Expect(packageRequiredVariables[0].Identifier()).To(Equal(deppy.IdentifierFromString("required package prometheus")))
94-
Expect(packageRequiredVariables[0].BundleEntities()).To(HaveLen(2))
95-
Expect(packageRequiredVariables[1].Identifier()).To(Equal(deppy.IdentifierFromString("required package packageA")))
96-
Expect(packageRequiredVariables[1].BundleEntities()).To(HaveLen(1))
106+
Expect(packageRequiredVariables).To(WithTransform(func(bvars []*required_package.RequiredPackageVariable) map[deppy.Identifier]int {
107+
out := map[deppy.Identifier]int{}
108+
for _, variable := range bvars {
109+
out[variable.Identifier()] = len(variable.BundleEntities())
110+
}
111+
return out
112+
}, Equal(map[deppy.Identifier]int{
113+
deppy.IdentifierFromString("required package prometheus"): 2,
114+
deppy.IdentifierFromString("required package packageA"): 1,
115+
})))
97116
})
98117

99118
It("should produce BundleVariables variables", func() {
100-
olmVariableSource := olm.NewOLMVariableSource(operator("prometheus"), operator("packageA"))
119+
cl := FakeClient(operator("prometheus"), operator("packageA"))
120+
121+
olmVariableSource := olm.NewOLMVariableSource(cl)
101122
variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource)
102123
Expect(err).ToNot(HaveOccurred())
103124

@@ -109,15 +130,17 @@ var _ = Describe("OLMVariableSource", func() {
109130
out = append(out, variable.BundleEntity().Entity)
110131
}
111132
return out
112-
}, Equal([]*input.Entity{
133+
}, ConsistOf([]*input.Entity{
113134
entityFromCache("operatorhub/prometheus/0.47.0"),
114135
entityFromCache("operatorhub/prometheus/0.37.0"),
115136
entityFromCache("operatorhub/packageA/2.0.0"),
116137
})))
117138
})
118139

119140
It("should produce version filtered BundleVariables variables", func() {
120-
olmVariableSource := olm.NewOLMVariableSource(operator("prometheus", withVersionRange(">0.40.0")), operator("packageA"))
141+
cl := FakeClient(operator("prometheus", withVersionRange(">0.40.0")), operator("packageA"))
142+
143+
olmVariableSource := olm.NewOLMVariableSource(cl)
121144
variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource)
122145
Expect(err).ToNot(HaveOccurred())
123146

@@ -129,7 +152,7 @@ var _ = Describe("OLMVariableSource", func() {
129152
out = append(out, variable.BundleEntity().Entity)
130153
}
131154
return out
132-
}, Equal([]*input.Entity{
155+
}, ConsistOf([]*input.Entity{
133156
entityFromCache("operatorhub/prometheus/0.47.0"),
134157
// filtered out
135158
// entityFromCache("operatorhub/prometheus/0.37.0"),
@@ -138,7 +161,9 @@ var _ = Describe("OLMVariableSource", func() {
138161
})
139162

140163
It("should produce GlobalConstraints variables", func() {
141-
olmVariableSource := olm.NewOLMVariableSource(operator("prometheus"), operator("packageA"))
164+
cl := FakeClient(operator("prometheus"), operator("packageA"))
165+
166+
olmVariableSource := olm.NewOLMVariableSource(cl)
142167
variables, err := olmVariableSource.GetVariables(context.Background(), testEntitySource)
143168
Expect(err).ToNot(HaveOccurred())
144169

@@ -166,7 +191,9 @@ var _ = Describe("OLMVariableSource", func() {
166191
})
167192

168193
It("should return an errors when they occur", func() {
169-
olmVariableSource := olm.NewOLMVariableSource(operator("prometheus"), operator("packageA"))
194+
cl := FakeClient(operator("prometheus"), operator("packageA"))
195+
196+
olmVariableSource := olm.NewOLMVariableSource(cl)
170197
_, err := olmVariableSource.GetVariables(context.Background(), FailEntitySource{})
171198
Expect(err).To(HaveOccurred())
172199
})

0 commit comments

Comments
 (0)