Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/samples/operators_v1alpha1_operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ metadata:
name: operator-sample
spec:
# TODO(user): Add fields here
packageName: my-operator
packageName: prometheus
66 changes: 33 additions & 33 deletions controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package controllers

import (
"context"
"strings"

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

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

// todo(perdasilva): this is a _hack_ we probably want to find a better way to ride or die resolve and update
// run resolution
solution, err := r.resolver.Resolve(ctx)
status := metav1.ConditionTrue
reason := operatorsv1alpha1.ReasonResolutionSucceeded
message := "resolution was successful"
if err != nil {
status = metav1.ConditionTrue
reason = operatorsv1alpha1.ReasonResolutionFailed
message = err.Error()
}

// todo(perdasilva): more hacks - need to fix up the solution structure to be more useful
packageVariableIDMap := map[string]string{}
for variableID, ok := range solution {
if ok {
idComponents := strings.Split(string(variableID), "/")
packageVariableIDMap[idComponents[1]] = string(variableID)
} else {
// extract package bundle path from resolved variable
var bundlePath = ""
for _, variable := range solution.SelectedVariables() {
switch v := variable.(type) {
case *bundles_and_dependencies.BundleVariable:
packageName, err := v.BundleEntity().PackageName()
if err != nil {
return ctrl.Result{}, err
}
if packageName == op.Spec.PackageName {
bundlePath, err = v.BundleEntity().BundlePath()
if err != nil {
return ctrl.Result{}, err
}
break
}
}
}
op.Status.BundlePath = bundlePath
}

operatorList := &operatorsv1alpha1.OperatorList{}
if err := r.Client.List(ctx, operatorList); err != nil {
return ctrl.Result{}, err
}

for _, operator := range operatorList.Items {
apimeta.SetStatusCondition(&operator.Status.Conditions, metav1.Condition{
Type: operatorsv1alpha1.TypeReady,
Status: status,
Reason: reason,
Message: message,
ObservedGeneration: op.GetGeneration(),
})
if varID, ok := packageVariableIDMap[operator.Spec.PackageName]; ok {
operator.Status.BundlePath = varID
}
if err := r.Client.Status().Update(ctx, &operator); err != nil {
return ctrl.Result{}, err
}
}
// update operator status
apimeta.SetStatusCondition(&op.Status.Conditions, metav1.Condition{
Type: operatorsv1alpha1.TypeReady,
Status: status,
Reason: reason,
Message: message,
ObservedGeneration: op.GetGeneration(),
})

return ctrl.Result{}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/onsi/ginkgo/v2 v2.3.1
github.com/onsi/gomega v1.22.1
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f
github.com/operator-framework/operator-registry v1.26.2
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ github.com/onsi/ginkgo/v2 v2.3.1 h1:8SbseP7qM32WcvE6VaN6vfXxv698izmsJ1UQX9ve7T8=
github.com/onsi/ginkgo/v2 v2.3.1/go.mod h1:Sv4yQXwG5VmF7tm3Q5Z+RWUpPo24LF1mpnz2crUb8Ys=
github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI=
github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM=
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999 h1:3R+Bg57vgekyDqS0w9c7vHNsHck/bo6dkaby6U9wi/8=
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999/go.mod h1:JaF7sX6tn7mpXcOehYjSHiKM1Y0z09vEfC6dca4AVuo=
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f h1:YxUZyQjF2kT2hli9ceBkuK7Mmiln0lV2RV38rzBObBI=
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f/go.mod h1:JaF7sX6tn7mpXcOehYjSHiKM1Y0z09vEfC6dca4AVuo=
github.com/operator-framework/operator-registry v1.26.2 h1:kQToR/hPqdivljaRXM0olPllNIcc/GUk1VBoGwagJmk=
github.com/operator-framework/operator-registry v1.26.2/go.mod h1:Z7XIb/3ZkhBQCvMD/rJphyuY4LmU/eWpZS+o0Mm1WAk=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
2 changes: 1 addition & 1 deletion internal/resolution/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewOperatorResolver(client client.Client, entitySource input.EntitySource)
}
}

func (o *OperatorResolver) Resolve(ctx context.Context) (solver.Solution, error) {
func (o *OperatorResolver) Resolve(ctx context.Context) (*solver.Solution, error) {
packageNames, err := o.getPackageNames(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get package names for resolution: %w", err)
Expand Down
18 changes: 12 additions & 6 deletions internal/resolution/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,26 @@ var _ = Describe("OperatorResolver", func() {
resolver := resolution.NewOperatorResolver(client, entitySource)
solution, err := resolver.Resolve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution).To(HaveLen(3))
Expect(solution["operatorhub/packageA/2.0.0"]).To(BeTrue())
Expect(solution["operatorhub/prometheus/0.47.0"]).To(BeTrue())
Expect(solution["operatorhub/prometheus/0.37.0"]).To(BeFalse())
// 2 * required package variables + 2 * bundle variables
Expect(solution.SelectedVariables()).To(HaveLen(4))

Expect(solution.IsSelected("operatorhub/packageA/2.0.0")).To(BeTrue())
Expect(solution.IsSelected("operatorhub/prometheus/0.47.0")).To(BeTrue())
Expect(solution.IsSelected("required package packageA")).To(BeTrue())
Expect(solution.IsSelected("required package prometheus")).To(BeTrue())

Expect(solution.IsSelected("operatorhub/prometheus/0.37.0")).To(BeFalse())

})

It("should not return an error if there are not Operator resources", func() {
It("should not return an error if there are no Operator resources", func() {
var resources []client.Object
client := FakeClient(resources...)
entitySource := input.NewCacheQuerier(testEntityCache)
resolver := resolution.NewOperatorResolver(client, entitySource)
solution, err := resolver.Resolve(context.Background())
Expect(err).ToNot(HaveOccurred())
Expect(solution).To(HaveLen(0))
Expect(solution.SelectedVariables()).To(HaveLen(0))
})

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