From 46c93e5c6c4dceb6678d6b42e31b1cbbb3e2dff7 Mon Sep 17 00:00:00 2001 From: Khaled Emara Date: Fri, 17 Jan 2025 17:15:36 +0200 Subject: [PATCH] feat(certmanager): add metrics for the reconciler Signed-off-by: Khaled Emara --- certmanager/controller.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/certmanager/controller.go b/certmanager/controller.go index 68a3a85..a13139a 100644 --- a/certmanager/controller.go +++ b/certmanager/controller.go @@ -7,12 +7,14 @@ import ( "github.com/go-logr/logr" "github.com/kyverno/pkg/tls" + "github.com/prometheus/client_golang/prometheus" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/predicate" ) @@ -21,6 +23,21 @@ const ( maxRetries = 10 ) +var ( + reconcileCount = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "certmanager_controller_reconcile_total", + Help: "Total number of reconciliations.", + }) + requeueCount = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "certmanager_controller_requeue_total", + Help: "Total number of requeues.", + }) + droppedCount = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "certmanager_controller_dropped_total", + Help: "Total number of objects dropped from the queue.", + }) +) + type CertController struct { client.Client Scheme *runtime.Scheme @@ -51,18 +68,17 @@ func NewController( } } -//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch -//+kubebuilder:rbac:groups=core,resources=secrets,verbs=create;update -//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch - func (r *CertController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx) + reconcileCount.Inc() if req.Namespace != r.namespace { + droppedCount.Inc() return ctrl.Result{}, nil } if req.Name != r.caSecretName && req.Name != r.tlsSecretName { + droppedCount.Inc() return ctrl.Result{}, nil } @@ -72,6 +88,7 @@ func (r *CertController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return ctrl.Result{}, err } + requeueCount.Inc() return ctrl.Result{ RequeueAfter: tls.CertRenewalInterval, }, nil @@ -124,3 +141,7 @@ func RetryFunc(ctx context.Context, retryInterval, timeout time.Duration, logger } } } + +func init() { + metrics.Registry.MustRegister(reconcileCount, requeueCount, droppedCount) +}