@@ -24,16 +24,21 @@ import (
2424 "encoding/pem"
2525 "fmt"
2626 "net/http"
27+ "time"
2728
29+ "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions"
2830 "github.com/cert-manager/csi-lib/driver"
2931 "github.com/cert-manager/csi-lib/manager"
3032 "github.com/cert-manager/csi-lib/manager/util"
3133 "github.com/cert-manager/csi-lib/metadata"
34+ csimetrics "github.com/cert-manager/csi-lib/metrics"
3235 "github.com/cert-manager/csi-lib/storage"
36+ "github.com/prometheus/client_golang/prometheus"
3337 "github.com/spf13/cobra"
3438 "golang.org/x/sync/errgroup"
3539 "k8s.io/utils/clock"
3640 ctrl "sigs.k8s.io/controller-runtime"
41+ ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3742 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3843
3944 "github.com/cert-manager/csi-driver/cmd/app/options"
@@ -45,7 +50,8 @@ import (
4550)
4651
4752const (
48- helpOutput = "Container Storage Interface driver to issue certificates from cert-manager"
53+ helpOutput = "Container Storage Interface driver to issue certificates from cert-manager"
54+ certRequestSharedInformerResyncPeriod = 5 * time .Second // TODO add to config if needed
4955)
5056
5157// NewCommand will return a new command instance for the cert-manager CSI driver.
@@ -80,6 +86,18 @@ func NewCommand(ctx context.Context) *cobra.Command {
8086 clientForMeta = util .ClientForMetadataTokenRequestEmptyAud (opts .RestConfig )
8187 }
8288
89+ // Setup metrics handler to track metrics for cert-manager CertificateRequests
90+ // Note, it won't be started if the --metrics-bind-address is "0".
91+ certRequestInformerFactory := externalversions .NewSharedInformerFactory (opts .CMClient , certRequestSharedInformerResyncPeriod )
92+ certRequestInformer := certRequestInformerFactory .Certmanager ().V1 ().CertificateRequests ()
93+ metricsHandler := csimetrics .New (
94+ opts .NodeID ,
95+ & opts .Logr ,
96+ ctrlmetrics .Registry .(* prometheus.Registry ),
97+ store ,
98+ certRequestInformer .Lister (),
99+ )
100+
83101 mngrlog := opts .Logr .WithName ("manager" )
84102 d , err := driver .New (ctx , opts .Endpoint , opts .Logr .WithName ("driver" ), driver.Options {
85103 DriverName : opts .DriverName ,
@@ -97,6 +115,7 @@ func NewCommand(ctx context.Context) *cobra.Command {
97115 GenerateRequest : requestgen .RequestForMetadata ,
98116 SignRequest : signRequest ,
99117 WriteKeypair : writer .WriteKeypair ,
118+ Metrics : metricsHandler ,
100119 }),
101120 })
102121 if err != nil {
@@ -163,6 +182,12 @@ func NewCommand(ctx context.Context) *cobra.Command {
163182 g .Go (func () error {
164183 return metricsServer .Start (gCTX )
165184 })
185+
186+ g .Go (func () error {
187+ certRequestInformerFactory .Start (gCTX .Done ())
188+ certRequestInformerFactory .WaitForCacheSync (gCTX .Done ())
189+ return nil
190+ })
166191 }
167192 return g .Wait ()
168193 },
0 commit comments