From cc2c7375262ce0a225ea9d00351ea1bcc498ced3 Mon Sep 17 00:00:00 2001 From: Varsha Prasad Narsing Date: Mon, 27 Feb 2023 16:28:24 -0500 Subject: [PATCH] Conditions: Map out-of-date bundleDeployment with an operator condition This PR brings in the change that sets the operator condition to Unknown when the BundleDeployment's conditions are out od date. Signed-off-by: Varsha Prasad Narsing --- controllers/operator_controller.go | 15 +++++++++++++ controllers/operator_controller_test.go | 30 ++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/controllers/operator_controller.go b/controllers/operator_controller.go index 8492bb8df..de290007a 100644 --- a/controllers/operator_controller.go +++ b/controllers/operator_controller.go @@ -314,6 +314,16 @@ func verifyBDStatus(dep *rukpakv1alpha1.BundleDeployment) (metav1.ConditionStatu // mapBDStatusToReadyCondition returns the operator object's "TypeReady" condition based on the bundle deployment statuses. func mapBDStatusToReadyCondition(existingBD *rukpakv1alpha1.BundleDeployment, observedGeneration int64) metav1.Condition { + // if BundleDeployment status is stale, return an unknown condition. + if isBundleDepStale(existingBD) { + return metav1.Condition{ + Type: operatorsv1alpha1.TypeReady, + Status: metav1.ConditionUnknown, + Reason: operatorsv1alpha1.ReasonInstallationStatusUnknown, + Message: fmt.Sprintf("waiting for bundleDeployment %q status to be updated. BundleDeployment conditions out of date.", existingBD.Name), + ObservedGeneration: observedGeneration, + } + } // update operator status: // 1. If the Operator "Ready" status is "Unknown": The status of successful bundleDeployment is unknown, wait till Rukpak updates the BD status. // 2. If the Operator "Ready" status is "True": Update the "successful resolution" status and return the result. @@ -338,3 +348,8 @@ func mapBDStatusToReadyCondition(existingBD *rukpakv1alpha1.BundleDeployment, ob ObservedGeneration: observedGeneration, } } + +// isBundleDepStale returns true if conditions are out of date. +func isBundleDepStale(existingTypedBundleDeployment *rukpakv1alpha1.BundleDeployment) bool { + return existingTypedBundleDeployment != nil && existingTypedBundleDeployment.Status.ObservedGeneration != existingTypedBundleDeployment.GetGeneration() +} diff --git a/controllers/operator_controller_test.go b/controllers/operator_controller_test.go index f1764ca19..68f87df8a 100644 --- a/controllers/operator_controller_test.go +++ b/controllers/operator_controller_test.go @@ -348,8 +348,31 @@ var _ = Describe("Reconcile Test", func() { Expect(err).NotTo(HaveOccurred()) }) + It("verify operator status when bundle deployment status is stale while being created", func() { + res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey}) + Expect(res).To(Equal(ctrl.Result{})) + Expect(err).NotTo(HaveOccurred()) + + By("fetching the updated operator after reconcile") + op := &operatorsv1alpha1.Operator{} + err = cl.Get(ctx, opKey, op) + Expect(err).NotTo(HaveOccurred()) + + By("checking the expected conditions") + cond := apimeta.FindStatusCondition(op.Status.Conditions, operatorsv1alpha1.TypeReady) + Expect(cond).NotTo(BeNil()) + Expect(cond.Status).To(Equal(metav1.ConditionUnknown)) + Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationStatusUnknown)) + Expect(cond.Message).To(Equal(fmt.Sprintf("waiting for bundleDeployment %q status to be updated. BundleDeployment conditions out of date.", bd.Name))) + }) + It("verify operator status when bundle deployment is waiting to be created", func() { By("running reconcile") + bd.Status.ObservedGeneration = bd.GetGeneration() + By("updating the status of bundleDeployment") + err := cl.Status().Update(ctx, bd) + Expect(err).NotTo(HaveOccurred()) + res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey}) Expect(res).To(Equal(ctrl.Result{})) Expect(err).NotTo(HaveOccurred()) @@ -364,7 +387,7 @@ var _ = Describe("Reconcile Test", func() { Expect(cond).NotTo(BeNil()) Expect(cond.Status).To(Equal(metav1.ConditionUnknown)) Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationStatusUnknown)) - Expect(cond.Message).To(ContainSubstring(`waiting for bundleDeployment`)) + Expect(cond.Message).To(Equal(fmt.Sprintf("waiting for bundleDeployment %q status to be updated", bd.Name))) }) It("verify operator status when `HasValidBundle` condition of rukpak is false", func() { @@ -374,6 +397,7 @@ var _ = Describe("Reconcile Test", func() { Message: "failed to unpack", Reason: rukpakv1alpha1.ReasonUnpackFailed, }) + bd.Status.ObservedGeneration = bd.GetGeneration() By("updating the status of bundleDeployment") err := cl.Status().Update(ctx, bd) @@ -404,6 +428,7 @@ var _ = Describe("Reconcile Test", func() { Message: "failed to install", Reason: rukpakv1alpha1.ReasonInstallFailed, }) + bd.Status.ObservedGeneration = bd.GetGeneration() By("updating the status of bundleDeployment") err := cl.Status().Update(ctx, bd) @@ -434,6 +459,7 @@ var _ = Describe("Reconcile Test", func() { Message: "operator installed successfully", Reason: rukpakv1alpha1.ReasonInstallationSucceeded, }) + bd.Status.ObservedGeneration = bd.GetGeneration() By("updating the status of bundleDeployment") err := cl.Status().Update(ctx, bd) @@ -471,6 +497,7 @@ var _ = Describe("Reconcile Test", func() { Message: "installing", Reason: rukpakv1alpha1.ReasonInstallationSucceeded, }) + bd.Status.ObservedGeneration = bd.GetGeneration() By("updating the status of bundleDeployment") err := cl.Status().Update(ctx, bd) @@ -501,6 +528,7 @@ var _ = Describe("Reconcile Test", func() { Message: "installing", Reason: rukpakv1alpha1.ReasonInstallationSucceeded, }) + bd.Status.ObservedGeneration = bd.GetGeneration() By("updating the status of bundleDeployment") err := cl.Status().Update(ctx, bd)