You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/02/19 15:22:15 UTC

[camel-k] branch master updated: Optimize reconciliation loops

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 7d14aed  Optimize reconciliation loops
7d14aed is described below

commit 7d14aedbac0b660993ff2d9df3cee0264e2d4d95
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Tue Feb 19 14:47:27 2019 +0100

    Optimize reconciliation loops
    
    This filters out events that are not relevant, mostly those triggered by status
    subresources updates, at the exception of the status phase changes that are used
    to transition resources from one phase to another.
---
 .../integration/integration_controller.go          | 28 ++++++++++++++--------
 .../integrationcontext_controller.go               | 20 +++++++++++++++-
 .../integrationplatform_controller.go              | 20 +++++++++++++++-
 3 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go
index 06550b2..f5f5c7d 100644
--- a/pkg/controller/integration/integration_controller.go
+++ b/pkg/controller/integration/integration_controller.go
@@ -6,12 +6,15 @@ import (
 
 	camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/client"
-	appsv1 "k8s.io/api/apps/v1"
+
 	"k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/runtime"
+
 	"sigs.k8s.io/controller-runtime/pkg/controller"
+	"sigs.k8s.io/controller-runtime/pkg/event"
 	"sigs.k8s.io/controller-runtime/pkg/handler"
 	"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"
 )
@@ -48,15 +51,20 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
 	}
 
 	// Watch for changes to primary resource Integration
-	err = c.Watch(&source.Kind{Type: &camelv1alpha1.Integration{}}, &handler.EnqueueRequestForObject{})
-	if err != nil {
-		return err
-	}
-
-	// Watch for changes to secondary resource Pods and requeue the owner Integration
-	err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{
-		IsController: true,
-		OwnerType:    &camelv1alpha1.Integration{},
+	err = c.Watch(&source.Kind{Type: &camelv1alpha1.Integration{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
+		UpdateFunc: func(e event.UpdateEvent) bool {
+			oldIntegration := e.ObjectOld.(*camelv1alpha1.Integration)
+			newIntegration := e.ObjectNew.(*camelv1alpha1.Integration)
+			// Ignore updates to the integration status in which case metadata.Generation does not change,
+			// or except when the integration phase changes as it's used to transition from one phase
+			// to another
+			return oldIntegration.Generation != newIntegration.Generation ||
+				oldIntegration.Status.Phase != newIntegration.Status.Phase
+		},
+		DeleteFunc: func(e event.DeleteEvent) bool {
+			// Evaluates to false if the object has been confirmed deleted
+			return !e.DeleteStateUnknown
+		},
 	})
 	if err != nil {
 		return err
diff --git a/pkg/controller/integrationcontext/integrationcontext_controller.go b/pkg/controller/integrationcontext/integrationcontext_controller.go
index fc71a76..75ef37a 100644
--- a/pkg/controller/integrationcontext/integrationcontext_controller.go
+++ b/pkg/controller/integrationcontext/integrationcontext_controller.go
@@ -6,11 +6,15 @@ import (
 
 	camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/client"
+
 	"k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/runtime"
+
 	"sigs.k8s.io/controller-runtime/pkg/controller"
+	"sigs.k8s.io/controller-runtime/pkg/event"
 	"sigs.k8s.io/controller-runtime/pkg/handler"
 	"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"
 )
@@ -42,7 +46,21 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
 	}
 
 	// Watch for changes to primary resource IntegrationContext
-	err = c.Watch(&source.Kind{Type: &camelv1alpha1.IntegrationContext{}}, &handler.EnqueueRequestForObject{})
+	err = c.Watch(&source.Kind{Type: &camelv1alpha1.IntegrationContext{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
+		UpdateFunc: func(e event.UpdateEvent) bool {
+			oldIntegrationContext := e.ObjectOld.(*camelv1alpha1.IntegrationContext)
+			newIntegrationContext := e.ObjectNew.(*camelv1alpha1.IntegrationContext)
+			// Ignore updates to the integration context status in which case metadata.Generation
+			// does not change, or except when the integration context phase changes as it's used
+			// to transition from one phase to another
+			return oldIntegrationContext.Generation != newIntegrationContext.Generation ||
+				oldIntegrationContext.Status.Phase != newIntegrationContext.Status.Phase
+		},
+		DeleteFunc: func(e event.DeleteEvent) bool {
+			// Evaluates to false if the object has been confirmed deleted
+			return !e.DeleteStateUnknown
+		},
+	})
 	if err != nil {
 		return err
 	}
diff --git a/pkg/controller/integrationplatform/integrationplatform_controller.go b/pkg/controller/integrationplatform/integrationplatform_controller.go
index 3d574a0..818e97e 100644
--- a/pkg/controller/integrationplatform/integrationplatform_controller.go
+++ b/pkg/controller/integrationplatform/integrationplatform_controller.go
@@ -6,11 +6,15 @@ import (
 
 	camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/client"
+
 	"k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/runtime"
+
 	"sigs.k8s.io/controller-runtime/pkg/controller"
+	"sigs.k8s.io/controller-runtime/pkg/event"
 	"sigs.k8s.io/controller-runtime/pkg/handler"
 	"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"
 )
@@ -42,7 +46,21 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
 	}
 
 	// Watch for changes to primary resource IntegrationPlatform
-	err = c.Watch(&source.Kind{Type: &camelv1alpha1.IntegrationPlatform{}}, &handler.EnqueueRequestForObject{})
+	err = c.Watch(&source.Kind{Type: &camelv1alpha1.IntegrationPlatform{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
+		UpdateFunc: func(e event.UpdateEvent) bool {
+			oldIntegrationPlatform := e.ObjectOld.(*camelv1alpha1.IntegrationPlatform)
+			newIntegrationPlatform := e.ObjectNew.(*camelv1alpha1.IntegrationPlatform)
+			// Ignore updates to the integration platform status in which case metadata.Generation
+			// does not change, or except when the integration platform phase changes as it's used
+			// to transition from one phase to another
+			return oldIntegrationPlatform.Generation != newIntegrationPlatform.Generation ||
+				oldIntegrationPlatform.Status.Phase != newIntegrationPlatform.Status.Phase
+		},
+		DeleteFunc: func(e event.DeleteEvent) bool {
+			// Evaluates to false if the object has been confirmed deleted
+			return !e.DeleteStateUnknown
+		},
+	})
 	if err != nil {
 		return err
 	}