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 2020/11/10 10:20:05 UTC
[camel-k] 05/25: feat: Add Camel K controllers reconcile loop
histgram metric
This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 0bb2ade6cb66fb66a3ac134fbd249d75f81186dc
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Wed Oct 14 14:58:21 2020 +0200
feat: Add Camel K controllers reconcile loop histgram metric
---
pkg/controller/build/build_controller.go | 37 +++---
.../integration/integration_controller.go | 41 ++++---
.../integrationkit/integrationkit_controller.go | 47 ++++----
.../integrationplatform_controller.go | 33 ++++--
pkg/controller/kamelet/kamelet_controller.go | 39 ++++---
.../kameletbinding/kamelet_binding_controller.go | 35 +++---
pkg/util/monitoring/controller.go | 128 +++++++++++++++++++++
pkg/util/monitoring/timer.go | 46 ++++++++
8 files changed, 315 insertions(+), 91 deletions(-)
diff --git a/pkg/controller/build/build_controller.go b/pkg/controller/build/build_controller.go
index 30a0e04..2e09188 100644
--- a/pkg/controller/build/build_controller.go
+++ b/pkg/controller/build/build_controller.go
@@ -22,12 +22,12 @@ import (
"sync"
"time"
- camelevent "github.com/apache/camel-k/pkg/event"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
+ "k8s.io/apimachinery/pkg/runtime/schema"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -40,7 +40,9 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/builder"
"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/util/monitoring"
)
// Add creates a new Build Controller and adds it to the Manager. The Manager will set fields on the Controller
@@ -55,13 +57,20 @@ func Add(mgr manager.Manager) error {
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileBuild{
- client: c,
- reader: mgr.GetAPIReader(),
- scheme: mgr.GetScheme(),
- builder: builder.New(c),
- recorder: mgr.GetEventRecorderFor("camel-k-build-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &reconcileBuild{
+ client: c,
+ reader: mgr.GetAPIReader(),
+ scheme: mgr.GetScheme(),
+ builder: builder.New(c),
+ recorder: mgr.GetEventRecorderFor("camel-k-build-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1.SchemeGroupVersion.Group,
+ Version: v1.SchemeGroupVersion.Version,
+ Kind: v1.BuildKind,
+ },
+ )
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -111,12 +120,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}
-var _ reconcile.Reconciler = &ReconcileBuild{}
+var _ reconcile.Reconciler = &reconcileBuild{}
-// ReconcileBuild reconciles a Build object
-type ReconcileBuild struct {
+// reconcileBuild reconciles a Build object
+type reconcileBuild struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
// Non-caching client to be used whenever caching may cause race conditions,
// like in the builds scheduling critical section
@@ -132,7 +141,7 @@ type ReconcileBuild struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
-func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+func (r *reconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result, error) {
rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
rlog.Info("Reconciling Build")
@@ -255,7 +264,7 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, nil
}
-func (r *ReconcileBuild) update(ctx context.Context, base *v1.Build, target *v1.Build) (reconcile.Result, error) {
+func (r *reconcileBuild) update(ctx context.Context, base *v1.Build, target *v1.Build) (reconcile.Result, error) {
err := r.client.Status().Patch(ctx, target, k8sclient.MergeFrom(base))
return reconcile.Result{}, err
diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go
index 3d94b87..5a1b5fe 100644
--- a/pkg/controller/integration/integration_controller.go
+++ b/pkg/controller/integration/integration_controller.go
@@ -20,14 +20,14 @@ package integration
import (
"context"
- camelevent "github.com/apache/camel-k/pkg/event"
- "github.com/apache/camel-k/pkg/platform"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/batch/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
+
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -39,15 +39,13 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"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/util/digest"
"github.com/apache/camel-k/pkg/util/log"
+ "github.com/apache/camel-k/pkg/util/monitoring"
)
-/**
-* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
-* business logic. Delete these comments after modifying this file.*
- */
-
// Add creates a new Integration 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 {
@@ -60,11 +58,18 @@ func Add(mgr manager.Manager) error {
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileIntegration{
- client: c,
- scheme: mgr.GetScheme(),
- recorder: mgr.GetEventRecorderFor("camel-k-integration-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &reconcileIntegration{
+ client: c,
+ scheme: mgr.GetScheme(),
+ recorder: mgr.GetEventRecorderFor("camel-k-integration-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1.SchemeGroupVersion.Group,
+ Version: v1.SchemeGroupVersion.Version,
+ Kind: v1.IntegrationKind,
+ },
+ )
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -215,12 +220,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}
-var _ reconcile.Reconciler = &ReconcileIntegration{}
+var _ reconcile.Reconciler = &reconcileIntegration{}
-// ReconcileIntegration reconciles a Integration object
-type ReconcileIntegration struct {
+// reconcileIntegration reconciles a Integration object
+type reconcileIntegration struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
@@ -231,7 +236,7 @@ type ReconcileIntegration struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
-func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+func (r *reconcileIntegration) Reconcile(request reconcile.Request) (reconcile.Result, error) {
rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
rlog.Info("Reconciling Integration")
@@ -310,7 +315,7 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R
return reconcile.Result{}, nil
}
-func (r *ReconcileIntegration) update(ctx context.Context, base *v1.Integration, target *v1.Integration) (reconcile.Result, error) {
+func (r *reconcileIntegration) update(ctx context.Context, base *v1.Integration, target *v1.Integration) (reconcile.Result, error) {
dgst, err := digest.ComputeForIntegration(target)
if err != nil {
return reconcile.Result{}, err
diff --git a/pkg/controller/integrationkit/integrationkit_controller.go b/pkg/controller/integrationkit/integrationkit_controller.go
index aaa87fe..a525725 100644
--- a/pkg/controller/integrationkit/integrationkit_controller.go
+++ b/pkg/controller/integrationkit/integrationkit_controller.go
@@ -20,18 +20,13 @@ package integrationkit
import (
"context"
- camelevent "github.com/apache/camel-k/pkg/event"
- "github.com/apache/camel-k/pkg/platform"
- "k8s.io/client-go/tools/record"
-
- "github.com/apache/camel-k/pkg/util/digest"
- "github.com/apache/camel-k/pkg/util/log"
-
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
- k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
+ "k8s.io/client-go/tools/record"
+ k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -42,6 +37,11 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"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/util/digest"
+ "github.com/apache/camel-k/pkg/util/log"
+ "github.com/apache/camel-k/pkg/util/monitoring"
)
// Add creates a new IntegrationKit Controller and adds it to the Manager. The Manager will set fields on the Controller
@@ -56,11 +56,18 @@ func Add(mgr manager.Manager) error {
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileIntegrationKit{
- client: c,
- scheme: mgr.GetScheme(),
- recorder: mgr.GetEventRecorderFor("camel-k-integration-kit-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &reconcileIntegrationKit{
+ client: c,
+ scheme: mgr.GetScheme(),
+ recorder: mgr.GetEventRecorderFor("camel-k-integration-kit-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1.SchemeGroupVersion.Group,
+ Version: v1.SchemeGroupVersion.Version,
+ Kind: v1.IntegrationKitKind,
+ },
+ )
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -112,7 +119,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}
// Watch for IntegrationPlatform phase transitioning to ready and enqueue
- // requests for any integrationkits that are in phase waiting for platform
+ // requests for any integration kits that are in phase waiting for platform
err = c.Watch(&source.Kind{Type: &v1.IntegrationPlatform{}}, &handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
platform := a.Object.(*v1.IntegrationPlatform)
@@ -149,12 +156,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}
-var _ reconcile.Reconciler = &ReconcileIntegrationKit{}
+var _ reconcile.Reconciler = &reconcileIntegrationKit{}
-// ReconcileIntegrationKit reconciles a IntegrationKit object
-type ReconcileIntegrationKit struct {
+// reconcileIntegrationKit reconciles a IntegrationKit object
+type reconcileIntegrationKit struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
@@ -165,7 +172,7 @@ type ReconcileIntegrationKit struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
-func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+func (r *reconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcile.Result, error) {
rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
rlog.Info("Reconciling IntegrationKit")
@@ -264,7 +271,7 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil
return reconcile.Result{}, nil
}
-func (r *ReconcileIntegrationKit) update(ctx context.Context, base *v1.IntegrationKit, target *v1.IntegrationKit) (reconcile.Result, error) {
+func (r *reconcileIntegrationKit) update(ctx context.Context, base *v1.IntegrationKit, target *v1.IntegrationKit) (reconcile.Result, error) {
dgst, err := digest.ComputeForIntegrationKit(target)
if err != nil {
return reconcile.Result{}, err
diff --git a/pkg/controller/integrationplatform/integrationplatform_controller.go b/pkg/controller/integrationplatform/integrationplatform_controller.go
index 9a0d99d..71a8711 100644
--- a/pkg/controller/integrationplatform/integrationplatform_controller.go
+++ b/pkg/controller/integrationplatform/integrationplatform_controller.go
@@ -21,10 +21,9 @@ import (
"context"
"time"
- camelevent "github.com/apache/camel-k/pkg/event"
- "github.com/apache/camel-k/pkg/platform"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -38,6 +37,9 @@ import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"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/util/monitoring"
)
// Add creates a new IntegrationPlatform Controller and adds it to the Manager. The Manager will set fields on the Controller
@@ -52,11 +54,18 @@ func Add(mgr manager.Manager) error {
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileIntegrationPlatform{
- client: c,
- scheme: mgr.GetScheme(),
- recorder: mgr.GetEventRecorderFor("camel-k-integration-platform-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &reconcileIntegrationPlatform{
+ client: c,
+ scheme: mgr.GetScheme(),
+ recorder: mgr.GetEventRecorderFor("camel-k-integration-platform-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1.SchemeGroupVersion.Group,
+ Version: v1.SchemeGroupVersion.Version,
+ Kind: v1.IntegrationPlatformKind,
+ },
+ )
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -90,12 +99,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}
-var _ reconcile.Reconciler = &ReconcileIntegrationPlatform{}
+var _ reconcile.Reconciler = &reconcileIntegrationPlatform{}
-// ReconcileIntegrationPlatform reconciles a IntegrationPlatform object
-type ReconcileIntegrationPlatform struct {
+// reconcileIntegrationPlatform reconciles a IntegrationPlatform object
+type reconcileIntegrationPlatform struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
@@ -106,7 +115,7 @@ type ReconcileIntegrationPlatform struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
-func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+func (r *reconcileIntegrationPlatform) Reconcile(request reconcile.Request) (reconcile.Result, error) {
rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
rlog.Info("Reconciling IntegrationPlatform")
diff --git a/pkg/controller/kamelet/kamelet_controller.go b/pkg/controller/kamelet/kamelet_controller.go
index 22d7548..a9acf50 100644
--- a/pkg/controller/kamelet/kamelet_controller.go
+++ b/pkg/controller/kamelet/kamelet_controller.go
@@ -21,13 +21,11 @@ import (
"context"
"time"
- "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
- "github.com/apache/camel-k/pkg/client"
- camelevent "github.com/apache/camel-k/pkg/event"
- "github.com/apache/camel-k/pkg/platform"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
+
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -36,6 +34,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
+
+ "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+ "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/util/monitoring"
)
// Add creates a new Kamelet Controller and adds it to the Manager. The Manager will set fields on the Controller
@@ -50,11 +54,18 @@ func Add(mgr manager.Manager) error {
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileKamelet{
- client: c,
- scheme: mgr.GetScheme(),
- recorder: mgr.GetEventRecorderFor("camel-k-kamelet-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &reconcileKamelet{
+ client: c,
+ scheme: mgr.GetScheme(),
+ recorder: mgr.GetEventRecorderFor("camel-k-kamelet-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1alpha1.SchemeGroupVersion.Group,
+ Version: v1alpha1.SchemeGroupVersion.Version,
+ Kind: v1alpha1.KameletKind,
+ },
+ )
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -88,12 +99,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}
-var _ reconcile.Reconciler = &ReconcileKamelet{}
+var _ reconcile.Reconciler = &reconcileKamelet{}
-// ReconcileKamelet reconciles a Kamelet object
-type ReconcileKamelet struct {
+// reconcileKamelet reconciles a Kamelet object
+type reconcileKamelet struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
@@ -104,7 +115,7 @@ type ReconcileKamelet struct {
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
-func (r *ReconcileKamelet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+func (r *reconcileKamelet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name)
rlog.Info("Reconciling Kamelet")
diff --git a/pkg/controller/kameletbinding/kamelet_binding_controller.go b/pkg/controller/kameletbinding/kamelet_binding_controller.go
index bbc84cb..ffde023 100644
--- a/pkg/controller/kameletbinding/kamelet_binding_controller.go
+++ b/pkg/controller/kameletbinding/kamelet_binding_controller.go
@@ -21,14 +21,11 @@ import (
"context"
"time"
- v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
- "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
- "github.com/apache/camel-k/pkg/client"
- camelevent "github.com/apache/camel-k/pkg/event"
- "github.com/apache/camel-k/pkg/platform"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
+
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -37,6 +34,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
+
+ v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+ "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+ "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/util/monitoring"
)
// Add creates a new KameletBinding Controller and adds it to the Manager. The Manager will set fields on the Controller
@@ -49,16 +53,21 @@ func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr, c))
}
-// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
- return &ReconcileKameletBinding{
- client: c,
- scheme: mgr.GetScheme(),
- recorder: mgr.GetEventRecorderFor("camel-k-kamelet-binding-controller"),
- }
+ return monitoring.NewInstrumentedReconciler(
+ &ReconcileKameletBinding{
+ client: c,
+ scheme: mgr.GetScheme(),
+ recorder: mgr.GetEventRecorderFor("camel-k-kamelet-binding-controller"),
+ },
+ schema.GroupVersionKind{
+ Group: v1alpha1.SchemeGroupVersion.Group,
+ Version: v1alpha1.SchemeGroupVersion.Version,
+ Kind: v1alpha1.KameletBindingKind,
+ },
+ )
}
-// 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("kamelet-binding-controller", mgr, controller.Options{Reconciler: r})
@@ -103,7 +112,7 @@ var _ reconcile.Reconciler = &ReconcileKameletBinding{}
// ReconcileKameletBinding reconciles a KameletBinding object
type ReconcileKameletBinding struct {
// This client, initialized using mgr.Client() above, is a split client
- // that reads objects from the cache and writes to the apiserver
+ // that reads objects from the cache and writes to the API server
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
diff --git a/pkg/util/monitoring/controller.go b/pkg/util/monitoring/controller.go
new file mode 100644
index 0000000..c9c3136
--- /dev/null
+++ b/pkg/util/monitoring/controller.go
@@ -0,0 +1,128 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+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 monitoring
+
+import (
+ "time"
+
+ "k8s.io/apimachinery/pkg/runtime/schema"
+
+ "sigs.k8s.io/controller-runtime/pkg/metrics"
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
+
+ "github.com/prometheus/client_golang/prometheus"
+)
+
+type resultLabelValue string
+
+const (
+ reconciled resultLabelValue = "Reconciled"
+ errored resultLabelValue = "Errored"
+ requeued resultLabelValue = "Requeued"
+
+ namespaceLabel = "namespace"
+ groupLabel = "group"
+ versionLabel = "version"
+ kindLabel = "kind"
+ resultLabel = "result"
+ tagLabel = "tag"
+)
+
+type tagLabelValue string
+
+const (
+ platformError tagLabelValue = "PlatformError"
+ userError tagLabelValue = "UserError"
+)
+
+type instrumentedReconciler struct {
+ reconciler reconcile.Reconciler
+ gvk schema.GroupVersionKind
+}
+
+var _ reconcile.Reconciler = &instrumentedReconciler{}
+
+func NewInstrumentedReconciler(reconciler reconcile.Reconciler, gvk schema.GroupVersionKind) reconcile.Reconciler {
+ return &instrumentedReconciler{
+ reconciler: reconciler,
+ gvk: gvk,
+ }
+}
+
+func (r *instrumentedReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
+ timer := NewTimer()
+
+ res, err := r.reconciler.Reconcile(request)
+
+ labels := prometheus.Labels{
+ namespaceLabel: request.Namespace,
+ groupLabel: r.gvk.Group,
+ versionLabel: r.gvk.Version,
+ kindLabel: r.gvk.Kind,
+ resultLabel: resultLabelFor(res, err),
+ tagLabel: "",
+ }
+ if err != nil {
+ // Controller errors are tagged as platform errors
+ labels[tagLabel] = string(platformError)
+ }
+
+ timer.ObserveDurationInSeconds(loopDuration.With(labels))
+
+ return res, err
+}
+
+func resultLabelFor(res reconcile.Result, err error) string {
+ var label resultLabelValue
+ if err != nil {
+ label = errored
+ } else if res.Requeue || res.RequeueAfter > 0 {
+ label = requeued
+ } else {
+ label = reconciled
+ }
+ return string(label)
+}
+
+var (
+ loopDuration = prometheus.NewHistogramVec(
+ prometheus.HistogramOpts{
+ Name: "camel_k_reconciliation_duration_seconds",
+ Help: "Camel K reconciliation loop duration",
+ Buckets: []float64{
+ 0.25 * time.Second.Seconds(),
+ 0.5 * time.Second.Seconds(),
+ 1 * time.Second.Seconds(),
+ 5 * time.Second.Seconds(),
+ },
+ },
+ []string{
+ namespaceLabel,
+ groupLabel,
+ versionLabel,
+ kindLabel,
+ resultLabel,
+ tagLabel,
+ },
+ )
+)
+
+func init() {
+ // Register custom metrics with the global prometheus registry
+ metrics.Registry.MustRegister(loopDuration)
+}
diff --git a/pkg/util/monitoring/timer.go b/pkg/util/monitoring/timer.go
new file mode 100644
index 0000000..215e8dd
--- /dev/null
+++ b/pkg/util/monitoring/timer.go
@@ -0,0 +1,46 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+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 monitoring
+
+import (
+ "time"
+
+ "github.com/prometheus/client_golang/prometheus"
+)
+
+// Timer is a helper type to time functions. Use NewTimer to create new
+// instances.
+type Timer struct {
+ begin time.Time
+}
+
+// NewTimer creates a new Timer.
+func NewTimer() *Timer {
+ return &Timer{
+ begin: time.Now(),
+ }
+}
+
+// ObserveDurationInSeconds records the duration passed since the Timer was created
+// with NewTimer. It calls the Observe method of the provided Observer. The observed
+// duration is also returned.
+func (t *Timer) ObserveDurationInSeconds(o prometheus.Observer) time.Duration {
+ d := time.Since(t.begin)
+ o.Observe(d.Seconds())
+ return d
+}