Skip to content

Commit

Permalink
Merge branch 'main' into resolver-e2e-test
Browse files Browse the repository at this point in the history
Signed-off-by: dtfranz <dfranz@redhat.com>
  • Loading branch information
dtfranz committed Jan 27, 2023
2 parents a06520c + 931493a commit a3acd70
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 55 deletions.
3 changes: 1 addition & 2 deletions api/v1alpha1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
)
// TODO(user): add Reasons from above
operatorutil.ConditionReasons = append(operatorutil.ConditionReasons,
ReasonNotImplemented,
ReasonNotImplemented, ReasonResolutionSucceeded, ReasonResolutionFailed,
)
}

Expand All @@ -55,7 +55,6 @@ type OperatorStatus struct {
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
BundlePath string `json:"BundlePath,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ spec:
status:
description: OperatorStatus defines the observed state of Operator
properties:
BundlePath:
type: string
conditions:
items:
description: "Condition contains details for one aspect of the current
Expand Down
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
71 changes: 36 additions & 35 deletions controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ package controllers

import (
"context"
"strings"

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/resolution"
"k8s.io/apimachinery/pkg/api/equality"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,6 +27,10 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

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"
)

// OperatorReconciler reconciles a Operator object
Expand Down Expand Up @@ -99,47 +100,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
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
}
// TODO(perdasilva): use bundlePath to stamp out bundle deployment
_ = bundlePath
break
}
}
}
}

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
7 changes: 4 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/controllers"
operatorutil "github.com/operator-framework/operator-controller/internal/util"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
Expand All @@ -38,6 +35,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/controllers"
operatorutil "github.com/operator-framework/operator-controller/internal/util"
//+kubebuilder:scaffold:imports
)

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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Predicates", func() {
})

Describe("InChannel", func() {
It("should return true when the entity has the has version in the right range", func() {
It("should return true when the entity comes from the specified channel", func() {
entity := input.NewEntity("test", map[string]string{
property.TypeChannel: `{"channelName":"stable","priority":0}`,
})
Expand Down
1 change: 0 additions & 1 deletion test/e2e/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ var _ = Describe("Operator Install", func() {
Eventually(func(g Gomega) {
err = c.Get(ctx, types.NamespacedName{Name: operator.Name}, operator)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(operator.Status.BundlePath).To(Equal("operatorhub/prometheus/0.47.0"))
g.Expect(len(operator.Status.Conditions)).To(Equal(1))
g.Expect(operator.Status.Conditions[0].Message).To(Equal("resolution was successful"))
}).WithTimeout(defaultTimeout).WithPolling(defaultPoll).Should(Succeed())
Expand Down

0 comments on commit a3acd70

Please sign in to comment.