Skip to content

Commit 4366cf4

Browse files
authored
Solution refactor (#105)
* update deppy version in vendor * refactor for new deppy.Solution definition Signed-off-by: perdasilva <[email protected]>
1 parent 75482d9 commit 4366cf4

File tree

6 files changed

+50
-44
lines changed

6 files changed

+50
-44
lines changed

config/samples/operators_v1alpha1_operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ metadata:
1010
name: operator-sample
1111
spec:
1212
# TODO(user): Add fields here
13-
packageName: my-operator
13+
packageName: prometheus

controllers/operator_controller.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package controllers
1818

1919
import (
2020
"context"
21-
"strings"
2221

2322
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
2423
"github.com/operator-framework/operator-controller/internal/resolution"
24+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
2525
"k8s.io/apimachinery/pkg/api/equality"
2626
apimeta "k8s.io/apimachinery/pkg/api/meta"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -99,47 +99,47 @@ func checkForUnexpectedFieldChange(a, b operatorsv1alpha1.Operator) bool {
9999

100100
// Helper function to do the actual reconcile
101101
func (r *OperatorReconciler) reconcile(ctx context.Context, op *operatorsv1alpha1.Operator) (ctrl.Result, error) {
102+
// define condition parameters
103+
var status = metav1.ConditionTrue
104+
var reason = operatorsv1alpha1.ReasonResolutionSucceeded
105+
var message = "resolution was successful"
102106

103-
// todo(perdasilva): this is a _hack_ we probably want to find a better way to ride or die resolve and update
107+
// run resolution
104108
solution, err := r.resolver.Resolve(ctx)
105-
status := metav1.ConditionTrue
106-
reason := operatorsv1alpha1.ReasonResolutionSucceeded
107-
message := "resolution was successful"
108109
if err != nil {
109110
status = metav1.ConditionTrue
110111
reason = operatorsv1alpha1.ReasonResolutionFailed
111112
message = err.Error()
112-
}
113-
114-
// todo(perdasilva): more hacks - need to fix up the solution structure to be more useful
115-
packageVariableIDMap := map[string]string{}
116-
for variableID, ok := range solution {
117-
if ok {
118-
idComponents := strings.Split(string(variableID), "/")
119-
packageVariableIDMap[idComponents[1]] = string(variableID)
113+
} else {
114+
// extract package bundle path from resolved variable
115+
var bundlePath = ""
116+
for _, variable := range solution.SelectedVariables() {
117+
switch v := variable.(type) {
118+
case *bundles_and_dependencies.BundleVariable:
119+
packageName, err := v.BundleEntity().PackageName()
120+
if err != nil {
121+
return ctrl.Result{}, err
122+
}
123+
if packageName == op.Spec.PackageName {
124+
bundlePath, err = v.BundleEntity().BundlePath()
125+
if err != nil {
126+
return ctrl.Result{}, err
127+
}
128+
break
129+
}
130+
}
120131
}
132+
op.Status.BundlePath = bundlePath
121133
}
122134

123-
operatorList := &operatorsv1alpha1.OperatorList{}
124-
if err := r.Client.List(ctx, operatorList); err != nil {
125-
return ctrl.Result{}, err
126-
}
127-
128-
for _, operator := range operatorList.Items {
129-
apimeta.SetStatusCondition(&operator.Status.Conditions, metav1.Condition{
130-
Type: operatorsv1alpha1.TypeReady,
131-
Status: status,
132-
Reason: reason,
133-
Message: message,
134-
ObservedGeneration: op.GetGeneration(),
135-
})
136-
if varID, ok := packageVariableIDMap[operator.Spec.PackageName]; ok {
137-
operator.Status.BundlePath = varID
138-
}
139-
if err := r.Client.Status().Update(ctx, &operator); err != nil {
140-
return ctrl.Result{}, err
141-
}
142-
}
135+
// update operator status
136+
apimeta.SetStatusCondition(&op.Status.Conditions, metav1.Condition{
137+
Type: operatorsv1alpha1.TypeReady,
138+
Status: status,
139+
Reason: reason,
140+
Message: message,
141+
ObservedGeneration: op.GetGeneration(),
142+
})
143143

144144
return ctrl.Result{}, nil
145145
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/blang/semver/v4 v4.0.0
77
github.com/onsi/ginkgo/v2 v2.3.1
88
github.com/onsi/gomega v1.22.1
9-
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999
9+
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f
1010
github.com/operator-framework/operator-registry v1.26.2
1111
k8s.io/apimachinery v0.25.0
1212
k8s.io/client-go v0.25.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ github.com/onsi/ginkgo/v2 v2.3.1 h1:8SbseP7qM32WcvE6VaN6vfXxv698izmsJ1UQX9ve7T8=
287287
github.com/onsi/ginkgo/v2 v2.3.1/go.mod h1:Sv4yQXwG5VmF7tm3Q5Z+RWUpPo24LF1mpnz2crUb8Ys=
288288
github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI=
289289
github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM=
290-
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999 h1:3R+Bg57vgekyDqS0w9c7vHNsHck/bo6dkaby6U9wi/8=
291-
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999/go.mod h1:JaF7sX6tn7mpXcOehYjSHiKM1Y0z09vEfC6dca4AVuo=
290+
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f h1:YxUZyQjF2kT2hli9ceBkuK7Mmiln0lV2RV38rzBObBI=
291+
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f/go.mod h1:JaF7sX6tn7mpXcOehYjSHiKM1Y0z09vEfC6dca4AVuo=
292292
github.com/operator-framework/operator-registry v1.26.2 h1:kQToR/hPqdivljaRXM0olPllNIcc/GUk1VBoGwagJmk=
293293
github.com/operator-framework/operator-registry v1.26.2/go.mod h1:Z7XIb/3ZkhBQCvMD/rJphyuY4LmU/eWpZS+o0Mm1WAk=
294294
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

internal/resolution/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewOperatorResolver(client client.Client, entitySource input.EntitySource)
2424
}
2525
}
2626

27-
func (o *OperatorResolver) Resolve(ctx context.Context) (solver.Solution, error) {
27+
func (o *OperatorResolver) Resolve(ctx context.Context) (*solver.Solution, error) {
2828
packageNames, err := o.getPackageNames(ctx)
2929
if err != nil {
3030
return nil, fmt.Errorf("failed to get package names for resolution: %w", err)

internal/resolution/resolver_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,26 @@ var _ = Describe("OperatorResolver", func() {
7777
resolver := resolution.NewOperatorResolver(client, entitySource)
7878
solution, err := resolver.Resolve(context.Background())
7979
Expect(err).ToNot(HaveOccurred())
80-
Expect(solution).To(HaveLen(3))
81-
Expect(solution["operatorhub/packageA/2.0.0"]).To(BeTrue())
82-
Expect(solution["operatorhub/prometheus/0.47.0"]).To(BeTrue())
83-
Expect(solution["operatorhub/prometheus/0.37.0"]).To(BeFalse())
80+
// 2 * required package variables + 2 * bundle variables
81+
Expect(solution.SelectedVariables()).To(HaveLen(4))
82+
83+
Expect(solution.IsSelected("operatorhub/packageA/2.0.0")).To(BeTrue())
84+
Expect(solution.IsSelected("operatorhub/prometheus/0.47.0")).To(BeTrue())
85+
Expect(solution.IsSelected("required package packageA")).To(BeTrue())
86+
Expect(solution.IsSelected("required package prometheus")).To(BeTrue())
87+
88+
Expect(solution.IsSelected("operatorhub/prometheus/0.37.0")).To(BeFalse())
89+
8490
})
8591

86-
It("should not return an error if there are not Operator resources", func() {
92+
It("should not return an error if there are no Operator resources", func() {
8793
var resources []client.Object
8894
client := FakeClient(resources...)
8995
entitySource := input.NewCacheQuerier(testEntityCache)
9096
resolver := resolution.NewOperatorResolver(client, entitySource)
9197
solution, err := resolver.Resolve(context.Background())
9298
Expect(err).ToNot(HaveOccurred())
93-
Expect(solution).To(HaveLen(0))
99+
Expect(solution.SelectedVariables()).To(HaveLen(0))
94100
})
95101

96102
It("should return an error if the entity source throws an error", func() {

0 commit comments

Comments
 (0)