From abe714dc470c44008416b4f4854db7e0a922d18f Mon Sep 17 00:00:00 2001 From: Mike Metral <1112768+metral@users.noreply.github.com> Date: Wed, 8 Jul 2020 17:12:06 +0000 Subject: [PATCH] stack-cntlr(Reconcile): skip extra loop if status updated See: - https://github.com/operator-framework/operator-sdk/issues/2795 - https://github.com/kubernetes-sigs/kubebuilder/issues/1103 - https://github.com/kubernetes-sigs/controller-runtime/pull/553 - https://book-v1.book.kubebuilder.io/basics/status_subresource.html --- pkg/controller/stack/stack_controller.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/controller/stack/stack_controller.go b/pkg/controller/stack/stack_controller.go index 0ad5ebaa..128a495f 100644 --- a/pkg/controller/stack/stack_controller.go +++ b/pkg/controller/stack/stack_controller.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" ) @@ -53,8 +54,15 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { return err } + // Filter out update events if an object's metadata.generation is unchanged. + // - https://github.com/operator-framework/operator-sdk/issues/2795 + // - https://github.com/kubernetes-sigs/kubebuilder/issues/1103 + // - https://github.com/kubernetes-sigs/controller-runtime/pull/553 + // - https://book-v1.book.kubebuilder.io/basics/status_subresource.html + pred := predicate.GenerationChangedPredicate{} + // Watch for changes to primary resource Stack - err = c.Watch(&source.Kind{Type: &pulumiv1alpha1.Stack{}}, &handler.EnqueueRequestForObject{}) + err = c.Watch(&source.Kind{Type: &pulumiv1alpha1.Stack{}}, &handler.EnqueueRequestForObject{}, pred) if err != nil { return err } @@ -214,7 +222,7 @@ func (r *ReconcileStack) Reconcile(request reconcile.Request) (reconcile.Result, } err = r.client.Status().Update(context.TODO(), instance) if err != nil { - reqLogger.Error(err, "Failed to update Stack outputs", "Stack.Name", stack.Stack) + reqLogger.Error(err, "Failed to update Stack status", "Stack.Name", stack.Stack) return reconcile.Result{}, err }