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 2022/09/29 07:33:34 UTC

[camel-k] branch main updated: chore: Add client argument to controllers

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 1257792c8 chore: Add client argument to controllers
1257792c8 is described below

commit 1257792c84af10a9ca61ca1267983f12ac0a7a9b
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Wed Sep 28 14:37:53 2022 +0200

    chore: Add client argument to controllers
---
 pkg/client/client.go                               |  2 +-
 pkg/cmd/operator/operator.go                       | 25 ++++++++++++----------
 pkg/controller/add_build.go                        |  4 ++--
 pkg/controller/add_integration.go                  |  4 ++--
 pkg/controller/add_integrationkit.go               |  4 ++--
 pkg/controller/add_integrationplatform.go          |  4 ++--
 pkg/controller/add_kamelet.go                      |  4 ++--
 pkg/controller/add_kameletbinding.go               |  4 ++--
 pkg/controller/build/build_controller.go           |  6 +-----
 pkg/controller/controller.go                       | 15 +++++++------
 .../integration/integration_controller.go          | 10 +++------
 .../integrationkit/integrationkit_controller.go    |  9 +-------
 .../integrationplatform_controller.go              | 13 +++--------
 pkg/controller/kamelet/kamelet_controller.go       |  6 +-----
 .../kameletbinding/kamelet_binding_controller.go   |  7 +-----
 15 files changed, 46 insertions(+), 71 deletions(-)

diff --git a/pkg/client/client.go b/pkg/client/client.go
index 967a5fc8d..b2647c771 100644
--- a/pkg/client/client.go
+++ b/pkg/client/client.go
@@ -190,7 +190,7 @@ func FromManager(manager manager.Manager) (Client, error) {
 	}, nil
 }
 
-// init initialize the k8s client for usage outside the cluster.
+// initialize the k8s client for usage outside the cluster.
 func initialize(kubeconfig string) {
 	if kubeconfig == "" {
 		// skip out-of-cluster initialization if inside the container
diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index e4fd8e4c6..c1e57ada2 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -30,7 +30,10 @@ import (
 	"time"
 
 	"github.com/pkg/errors"
+
 	"go.uber.org/automaxprocs/maxprocs"
+	"go.uber.org/zap"
+	"go.uber.org/zap/zapcore"
 
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1 "k8s.io/api/batch/v1"
@@ -45,8 +48,6 @@ import (
 	"k8s.io/client-go/tools/record"
 	"k8s.io/klog/v2"
 
-	"go.uber.org/zap"
-	"go.uber.org/zap/zapcore"
 	"sigs.k8s.io/controller-runtime/pkg/cache"
 	ctrl "sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -140,7 +141,7 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 	// from being throttled.
 	cfg.QPS = 20
 	cfg.Burst = 200
-	c, err := client.NewClientWithConfig(false, cfg)
+	bootstrapClient, err := client.NewClientWithConfig(false, cfg)
 	exitOnError(err, "cannot initialize client")
 
 	// We do not rely on the event broadcaster managed by controller runtime,
@@ -150,7 +151,7 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 	broadcaster := record.NewBroadcaster()
 	defer broadcaster.Shutdown()
 
-	if ok, err := kubernetes.CheckPermission(context.TODO(), c, corev1.GroupName, "events", watchNamespace, "", "create"); err != nil || !ok {
+	if ok, err := kubernetes.CheckPermission(context.TODO(), bootstrapClient, corev1.GroupName, "events", watchNamespace, "", "create"); err != nil || !ok {
 		// Do not sink Events to the server as they'll be rejected
 		broadcaster = event.NewSinkLessBroadcaster(broadcaster)
 		exitOnError(err, "cannot check permissions for creating Events")
@@ -170,10 +171,10 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 	}
 
 	// Set the operator container image if it runs in-container
-	platform.OperatorImage, err = getOperatorImage(context.TODO(), c)
+	platform.OperatorImage, err = getOperatorImage(context.TODO(), bootstrapClient)
 	exitOnError(err, "cannot get operator container image")
 
-	if ok, err := kubernetes.CheckPermission(context.TODO(), c, coordination.GroupName, "leases", operatorNamespace, "", "create"); err != nil || !ok {
+	if ok, err := kubernetes.CheckPermission(context.TODO(), bootstrapClient, coordination.GroupName, "leases", operatorNamespace, "", "create"); err != nil || !ok {
 		leaderElection = false
 		exitOnError(err, "cannot check permissions for creating Leases")
 		log.Info("The operator is not granted permissions to create Leases")
@@ -194,7 +195,7 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 		&servingv1.Service{}: {Label: selector},
 	}
 
-	if ok, err := kubernetes.IsAPIResourceInstalled(c, batchv1.SchemeGroupVersion.String(), reflect.TypeOf(batchv1.CronJob{}).Name()); ok && err == nil {
+	if ok, err := kubernetes.IsAPIResourceInstalled(bootstrapClient, batchv1.SchemeGroupVersion.String(), reflect.TypeOf(batchv1.CronJob{}).Name()); ok && err == nil {
 		selectors[&batchv1.CronJob{}] = struct {
 			Label labels.Selector
 			Field fields.Selector
@@ -203,7 +204,7 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 		}
 	}
 
-	mgr, err := manager.New(c.GetConfig(), manager.Options{
+	mgr, err := manager.New(cfg, manager.Options{
 		Namespace:                     watchNamespace,
 		EventBroadcaster:              broadcaster,
 		LeaderElection:                leaderElection,
@@ -233,13 +234,15 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
 	log.Info("Configuring manager")
 	exitOnError(mgr.AddHealthzCheck("health-probe", healthz.Ping), "Unable add liveness check")
 	exitOnError(apis.AddToScheme(mgr.GetScheme()), "")
-	exitOnError(controller.AddToManager(mgr), "")
+	ctrlClient, err := client.FromManager(mgr)
+	exitOnError(err, "")
+	exitOnError(controller.AddToManager(mgr, ctrlClient), "")
 
 	log.Info("Installing operator resources")
 	installCtx, installCancel := context.WithTimeout(context.Background(), 1*time.Minute)
 	defer installCancel()
-	install.OperatorStartupOptionalTools(installCtx, c, watchNamespace, operatorNamespace, log)
-	exitOnError(findOrCreateIntegrationPlatform(installCtx, c, operatorNamespace), "failed to create integration platform")
+	install.OperatorStartupOptionalTools(installCtx, bootstrapClient, watchNamespace, operatorNamespace, log)
+	exitOnError(findOrCreateIntegrationPlatform(installCtx, bootstrapClient, operatorNamespace), "failed to create integration platform")
 
 	log.Info("Starting the manager")
 	exitOnError(mgr.Start(signals.SetupSignalHandler()), "manager exited non-zero")
diff --git a/pkg/controller/add_build.go b/pkg/controller/add_build.go
index 5736a478e..c7c04441e 100644
--- a/pkg/controller/add_build.go
+++ b/pkg/controller/add_build.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, build.Add)
+	addToManager = append(addToManager, build.Add)
 }
diff --git a/pkg/controller/add_integration.go b/pkg/controller/add_integration.go
index 285ecc53c..bddc9f6b1 100644
--- a/pkg/controller/add_integration.go
+++ b/pkg/controller/add_integration.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, integration.Add)
+	addToManager = append(addToManager, integration.Add)
 }
diff --git a/pkg/controller/add_integrationkit.go b/pkg/controller/add_integrationkit.go
index 3b0ec0331..e47f87356 100644
--- a/pkg/controller/add_integrationkit.go
+++ b/pkg/controller/add_integrationkit.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, integrationkit.Add)
+	addToManager = append(addToManager, integrationkit.Add)
 }
diff --git a/pkg/controller/add_integrationplatform.go b/pkg/controller/add_integrationplatform.go
index 362334cee..e2c65aa09 100644
--- a/pkg/controller/add_integrationplatform.go
+++ b/pkg/controller/add_integrationplatform.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, integrationplatform.Add)
+	addToManager = append(addToManager, integrationplatform.Add)
 }
diff --git a/pkg/controller/add_kamelet.go b/pkg/controller/add_kamelet.go
index 7818bcd75..0aa2facc1 100644
--- a/pkg/controller/add_kamelet.go
+++ b/pkg/controller/add_kamelet.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, kamelet.Add)
+	addToManager = append(addToManager, kamelet.Add)
 }
diff --git a/pkg/controller/add_kameletbinding.go b/pkg/controller/add_kameletbinding.go
index 480c4df1c..44efa1b04 100644
--- a/pkg/controller/add_kameletbinding.go
+++ b/pkg/controller/add_kameletbinding.go
@@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
@@ -21,6 +22,5 @@ import (
 )
 
 func init() {
-	// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
-	AddToManagerFuncs = append(AddToManagerFuncs, kameletbinding.Add)
+	addToManager = append(addToManager, kameletbinding.Add)
 }
diff --git a/pkg/controller/build/build_controller.go b/pkg/controller/build/build_controller.go
index 117ccaa1e..dc3db68d1 100644
--- a/pkg/controller/build/build_controller.go
+++ b/pkg/controller/build/build_controller.go
@@ -41,11 +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) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+func Add(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 6ce6199b7..ace2d9cb6 100644
--- a/pkg/controller/controller.go
+++ b/pkg/controller/controller.go
@@ -14,19 +14,22 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
+
 package controller
 
 import (
-	"sigs.k8s.io/controller-runtime/pkg/manager"
+	ctrl "sigs.k8s.io/controller-runtime"
+
+	"github.com/apache/camel-k/pkg/client"
 )
 
-// AddToManagerFuncs is a list of functions to add all Controllers to the Manager.
-var AddToManagerFuncs []func(manager.Manager) error
+// addToManager is a list of functions to add all Controllers to the Manager.
+var addToManager []func(ctrl.Manager, client.Client) error
 
 // AddToManager adds all Controllers to the Manager.
-func AddToManager(m manager.Manager) error {
-	for _, f := range AddToManagerFuncs {
-		if err := f(m); err != nil {
+func AddToManager(manager ctrl.Manager, client client.Client) error {
+	for _, f := range addToManager {
+		if err := f(manager, client); err != nil {
 			return err
 		}
 	}
diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go
index bc63ef92c..de73ecd11 100644
--- a/pkg/controller/integration/integration_controller.go
+++ b/pkg/controller/integration/integration_controller.go
@@ -23,8 +23,6 @@ import (
 	"reflect"
 	"time"
 
-	"github.com/apache/camel-k/pkg/trait"
-
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1 "k8s.io/api/batch/v1"
 	corev1 "k8s.io/api/core/v1"
@@ -33,6 +31,7 @@ import (
 	"k8s.io/apimachinery/pkg/runtime/schema"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/client-go/tools/record"
+
 	"sigs.k8s.io/controller-runtime/pkg/builder"
 	ctrl "sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/event"
@@ -48,17 +47,14 @@ import (
 	"github.com/apache/camel-k/pkg/client"
 	camelevent "github.com/apache/camel-k/pkg/event"
 	"github.com/apache/camel-k/pkg/platform"
+	"github.com/apache/camel-k/pkg/trait"
 	"github.com/apache/camel-k/pkg/util/digest"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 	"github.com/apache/camel-k/pkg/util/log"
 	"github.com/apache/camel-k/pkg/util/monitoring"
 )
 
-func Add(mgr manager.Manager) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+func Add(mgr manager.Manager, c client.Client) error {
 	return add(mgr, c, newReconciler(mgr, c))
 }
 
diff --git a/pkg/controller/integrationkit/integrationkit_controller.go b/pkg/controller/integrationkit/integrationkit_controller.go
index c1f839636..02a5c7ce8 100644
--- a/pkg/controller/integrationkit/integrationkit_controller.go
+++ b/pkg/controller/integrationkit/integrationkit_controller.go
@@ -46,15 +46,10 @@ 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) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+func Add(mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }
 
-// newReconciler returns a new reconcile.Reconciler.
 func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 	return monitoring.NewInstrumentedReconciler(
 		&reconcileIntegrationKit{
@@ -70,9 +65,7 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 	)
 }
 
-// add adds a new Controller to mgr with r as the reconcile.Reconciler.
 func add(mgr manager.Manager, r reconcile.Reconciler) error {
-	// Create a new controller
 	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 44257e0a3..a3a35da3d 100644
--- a/pkg/controller/integrationplatform/integrationplatform_controller.go
+++ b/pkg/controller/integrationplatform/integrationplatform_controller.go
@@ -41,17 +41,12 @@ import (
 	"github.com/apache/camel-k/pkg/util/monitoring"
 )
 
-// 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) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+// 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 {
 	return add(mgr, newReconciler(mgr, c))
 }
 
-// newReconciler returns a new reconcile.Reconciler.
 func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 	return monitoring.NewInstrumentedReconciler(
 		&reconcileIntegrationPlatform{
@@ -68,9 +63,7 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 	)
 }
 
-// add adds a new Controller to mgr with r as the reconcile.Reconciler.
 func add(mgr manager.Manager, r reconcile.Reconciler) error {
-	// Create a new controller
 	c, err := controller.New("integrationplatform-controller", mgr, controller.Options{Reconciler: r})
 	if err != nil {
 		return err
diff --git a/pkg/controller/kamelet/kamelet_controller.go b/pkg/controller/kamelet/kamelet_controller.go
index 5abee9855..a7715fa06 100644
--- a/pkg/controller/kamelet/kamelet_controller.go
+++ b/pkg/controller/kamelet/kamelet_controller.go
@@ -43,11 +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) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+func Add(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 77278f3c1..b14574217 100644
--- a/pkg/controller/kameletbinding/kamelet_binding_controller.go
+++ b/pkg/controller/kameletbinding/kamelet_binding_controller.go
@@ -46,11 +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) error {
-	c, err := client.FromManager(mgr)
-	if err != nil {
-		return err
-	}
+func Add(mgr manager.Manager, c client.Client) error {
 	return add(mgr, newReconciler(mgr, c))
 }
 
@@ -70,7 +66,6 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
 }
 
 func add(mgr manager.Manager, r reconcile.Reconciler) error {
-	// Create a new controller
 	c, err := controller.New("kamelet-binding-controller", mgr, controller.Options{Reconciler: r})
 	if err != nil {
 		return err