Skip to content

Commit

Permalink
feat(certmanager): add metrics for the reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Khaled Emara <[email protected]>
  • Loading branch information
KhaledEmaraDev committed Jan 17, 2025
1 parent 1f0cf7a commit 46c93e5
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions certmanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -124,3 +141,7 @@ func RetryFunc(ctx context.Context, retryInterval, timeout time.Duration, logger
}
}
}

func init() {
metrics.Registry.MustRegister(reconcileCount, requeueCount, droppedCount)
}

0 comments on commit 46c93e5

Please sign in to comment.