You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2023/02/17 13:43:30 UTC

[camel-k] branch main updated: Move pod's phase indexer to integration controller initialization

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 4b77766ba Move pod's phase indexer to integration controller initialization
4b77766ba is described below

commit 4b77766ba1db6a9c136b37580ff84a6c2fb35f10
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Thu Feb 16 22:51:57 2023 +0100

    Move pod's phase indexer to integration controller initialization
---
 pkg/cmd/operator/operator.go                       | 11 +------
 pkg/controller/build/build_controller.go           |  2 +-
 pkg/controller/controller.go                       |  8 +++--
 .../integration/integration_controller.go          | 34 +++++++++++++++-------
 .../integrationkit/integrationkit_controller.go    |  6 ++--
 .../integrationplatform_controller.go              |  2 +-
 pkg/controller/kamelet/kamelet_controller.go       |  2 +-
 .../kameletbinding/kamelet_binding_controller.go   |  2 +-
 8 files changed, 36 insertions(+), 31 deletions(-)

diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index 71817cf8c..69e167baf 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -224,21 +224,12 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 	})
 	exitOnError(err, "")
 
-	exitOnError(
-		mgr.GetFieldIndexer().IndexField(ctx, &corev1.Pod{}, "status.phase",
-			func(obj ctrl.Object) []string {
-				pod, _ := obj.(*corev1.Pod)
-				return []string{string(pod.Status.Phase)}
-			}),
-		"unable to set up field indexer for status.phase: %v",
-	)
-
 	log.Info("Configuring manager")
 	exitOnError(mgr.AddHealthzCheck("health-probe", healthz.Ping), "Unable add liveness check")
 	exitOnError(apis.AddToScheme(mgr.GetScheme()), "")
 	ctrlClient, err := client.FromManager(mgr)
 	exitOnError(err, "")
-	exitOnError(controller.AddToManager(mgr, ctrlClient), "")
+	exitOnError(controller.AddToManager(ctx, mgr, ctrlClient), "")
 
 	log.Info("Installing operator resources")
 	installCtx, installCancel := context.WithTimeout(ctx, 1*time.Minute)
diff --git a/pkg/controller/build/build_controller.go b/pkg/controller/build/build_controller.go
index dc3db68d1..3bfab58ef 100644
--- a/pkg/controller/build/build_controller.go
+++ b/pkg/controller/build/build_controller.go
@@ -41,7 +41,7 @@ import (
 
 // Add creates a new Build Controller and adds it to the Manager. The Manager will set fields on the Controller
 // and Start it when the Manager is Started.
-func Add(mgr manager.Manager, c client.Client) error {
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }
 
diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go
index ace2d9cb6..721c93aa3 100644
--- a/pkg/controller/controller.go
+++ b/pkg/controller/controller.go
@@ -18,18 +18,20 @@ limitations under the License.
 package controller
 
 import (
+	"context"
+
 	ctrl "sigs.k8s.io/controller-runtime"
 
 	"github.com/apache/camel-k/pkg/client"
 )
 
 // addToManager is a list of functions to add all Controllers to the Manager.
-var addToManager []func(ctrl.Manager, client.Client) error
+var addToManager []func(context.Context, ctrl.Manager, client.Client) error
 
 // AddToManager adds all Controllers to the Manager.
-func AddToManager(manager ctrl.Manager, client client.Client) error {
+func AddToManager(ctx context.Context, manager ctrl.Manager, client client.Client) error {
 	for _, f := range addToManager {
-		if err := f(manager, client); err != nil {
+		if err := f(ctx, manager, client); err != nil {
 			return err
 		}
 	}
diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go
index de73ecd11..48887fa8c 100644
--- a/pkg/controller/integration/integration_controller.go
+++ b/pkg/controller/integration/integration_controller.go
@@ -23,6 +23,8 @@ import (
 	"reflect"
 	"time"
 
+	"github.com/pkg/errors"
+
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1 "k8s.io/api/batch/v1"
 	corev1 "k8s.io/api/core/v1"
@@ -54,8 +56,18 @@ import (
 	"github.com/apache/camel-k/pkg/util/monitoring"
 )
 
-func Add(mgr manager.Manager, c client.Client) error {
-	return add(mgr, c, newReconciler(mgr, c))
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
+	err := mgr.GetFieldIndexer().IndexField(ctx, &corev1.Pod{}, "status.phase",
+		func(obj ctrl.Object) []string {
+			pod, _ := obj.(*corev1.Pod)
+			return []string{string(pod.Status.Phase)}
+		})
+
+	if err != nil {
+		return errors.Wrapf(err, "unable to set up field indexer for status.phase")
+	}
+
+	return add(ctx, mgr, c, newReconciler(mgr, c))
 }
 
 func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
@@ -113,7 +125,7 @@ func isIntegrationUpdated(it *v1.Integration, previous, next *v1.IntegrationCond
 	return false
 }
 
-func integrationKitEnqueueRequestsFromMapFunc(c client.Client, kit *v1.IntegrationKit) []reconcile.Request {
+func integrationKitEnqueueRequestsFromMapFunc(ctx context.Context, c client.Client, kit *v1.IntegrationKit) []reconcile.Request {
 	var requests []reconcile.Request
 	if kit.Status.Phase != v1.IntegrationKitPhaseReady && kit.Status.Phase != v1.IntegrationKitPhaseError {
 		return requests
@@ -125,7 +137,7 @@ func integrationKitEnqueueRequestsFromMapFunc(c client.Client, kit *v1.Integrati
 	if !platform.IsCurrentOperatorGlobal() {
 		opts = append(opts, ctrl.InNamespace(kit.Namespace))
 	}
-	if err := c.List(context.Background(), list, opts...); err != nil {
+	if err := c.List(ctx, list, opts...); err != nil {
 		log.Error(err, "Failed to retrieve integration list")
 		return requests
 	}
@@ -158,7 +170,7 @@ func integrationKitEnqueueRequestsFromMapFunc(c client.Client, kit *v1.Integrati
 	return requests
 }
 
-func integrationPlatformEnqueueRequestsFromMapFunc(c client.Client, p *v1.IntegrationPlatform) []reconcile.Request {
+func integrationPlatformEnqueueRequestsFromMapFunc(ctx context.Context, c client.Client, p *v1.IntegrationPlatform) []reconcile.Request {
 	var requests []reconcile.Request
 
 	if p.Status.Phase == v1.IntegrationPlatformPhaseReady {
@@ -170,7 +182,7 @@ func integrationPlatformEnqueueRequestsFromMapFunc(c client.Client, p *v1.Integr
 			opts = append(opts, ctrl.InNamespace(p.Namespace))
 		}
 
-		if err := c.List(context.Background(), list, opts...); err != nil {
+		if err := c.List(ctx, list, opts...); err != nil {
 			log.Error(err, "Failed to list integrations")
 			return requests
 		}
@@ -191,7 +203,7 @@ func integrationPlatformEnqueueRequestsFromMapFunc(c client.Client, p *v1.Integr
 	return requests
 }
 
-func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
+func add(ctx context.Context, mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
 	b := builder.ControllerManagedBy(mgr).
 		Named("integration-controller").
 		// Watch for changes to primary resource Integration
@@ -225,7 +237,7 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
 					return []reconcile.Request{}
 				}
 
-				return integrationKitEnqueueRequestsFromMapFunc(c, kit)
+				return integrationKitEnqueueRequestsFromMapFunc(ctx, c, kit)
 			})).
 		// Watch for IntegrationPlatform phase transitioning to ready and enqueue
 		// requests for any integrations that are in phase waiting for platform
@@ -237,7 +249,7 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
 					return []reconcile.Request{}
 				}
 
-				return integrationPlatformEnqueueRequestsFromMapFunc(c, p)
+				return integrationPlatformEnqueueRequestsFromMapFunc(ctx, c, p)
 			})).
 		// Watch for the owned Deployments
 		Owns(&appsv1.Deployment{}, builder.WithPredicates(StatusChangedPredicate{})).
@@ -269,9 +281,9 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
 		return err
 	} else if ok {
 		// Check for permission to watch the ConsoleCLIDownload resource
-		ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
+		checkCtx, cancel := context.WithTimeout(ctx, time.Minute)
 		defer cancel()
-		if ok, err = kubernetes.CheckPermission(ctx, c, serving.GroupName, "services", platform.GetOperatorWatchNamespace(), "", "watch"); err != nil {
+		if ok, err = kubernetes.CheckPermission(checkCtx, c, serving.GroupName, "services", platform.GetOperatorWatchNamespace(), "", "watch"); err != nil {
 			return err
 		} else if ok {
 			b.Owns(&servingv1.Service{}, builder.WithPredicates(StatusChangedPredicate{}))
diff --git a/pkg/controller/integrationkit/integrationkit_controller.go b/pkg/controller/integrationkit/integrationkit_controller.go
index cdb41fe11..b52005b27 100644
--- a/pkg/controller/integrationkit/integrationkit_controller.go
+++ b/pkg/controller/integrationkit/integrationkit_controller.go
@@ -46,8 +46,8 @@ import (
 
 // Add creates a new IntegrationKit Controller and adds it to the Manager. The Manager will set fields on the Controller
 // and Start it when the Manager is Started.
-func Add(mgr manager.Manager, c client.Client) error {
-	return add(mgr, newReconciler(mgr, c))
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
+	return add(ctx, mgr, newReconciler(mgr, c))
 }
 
 func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
@@ -65,7 +65,7 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 	)
 }
 
-func add(mgr manager.Manager, r reconcile.Reconciler) error {
+func add(_ context.Context, mgr manager.Manager, r reconcile.Reconciler) error {
 	c, err := controller.New("integrationkit-controller", mgr, controller.Options{Reconciler: r})
 	if err != nil {
 		return err
diff --git a/pkg/controller/integrationplatform/integrationplatform_controller.go b/pkg/controller/integrationplatform/integrationplatform_controller.go
index a3a35da3d..b604b389e 100644
--- a/pkg/controller/integrationplatform/integrationplatform_controller.go
+++ b/pkg/controller/integrationplatform/integrationplatform_controller.go
@@ -43,7 +43,7 @@ import (
 
 // Add creates a new IntegrationPlatform Controller and adds it to the Manager. The Manager will set fields
 // on the Controller and Start it when the Manager is Started.
-func Add(mgr manager.Manager, c client.Client) error {
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }
 
diff --git a/pkg/controller/kamelet/kamelet_controller.go b/pkg/controller/kamelet/kamelet_controller.go
index a7715fa06..c7e74cccb 100644
--- a/pkg/controller/kamelet/kamelet_controller.go
+++ b/pkg/controller/kamelet/kamelet_controller.go
@@ -43,7 +43,7 @@ import (
 
 // Add creates a new Kamelet Controller and adds it to the Manager. The Manager will set fields on the Controller
 // and Start it when the Manager is Started.
-func Add(mgr manager.Manager, c client.Client) error {
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }
 
diff --git a/pkg/controller/kameletbinding/kamelet_binding_controller.go b/pkg/controller/kameletbinding/kamelet_binding_controller.go
index 8709c3e38..aeab73d51 100644
--- a/pkg/controller/kameletbinding/kamelet_binding_controller.go
+++ b/pkg/controller/kameletbinding/kamelet_binding_controller.go
@@ -46,7 +46,7 @@ import (
 
 // Add creates a new KameletBinding Controller and adds it to the Manager. The Manager will set fields on the Controller
 // and Start it when the Manager is Started.
-func Add(mgr manager.Manager, c client.Client) error {
+func Add(ctx context.Context, mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }