Skip to content

Commit 88ea524

Browse files
committed
Enable csi-lib metrics
This PR enables csi-lib metrics feature in cert-manager/csi-lib#73 Signed-off-by: Jing Liu <jingliu@apple.com>
1 parent 17a2bfb commit 88ea524

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

cmd/app/app.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

4752
const (
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
},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-logr/logr v1.4.3
99
github.com/onsi/ginkgo/v2 v2.27.3
1010
github.com/onsi/gomega v1.38.3
11+
github.com/prometheus/client_golang v1.23.2
1112
github.com/spf13/cobra v1.10.2
1213
github.com/spf13/pflag v1.0.10
1314
github.com/stretchr/testify v1.11.1
@@ -70,7 +71,6 @@ require (
7071
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
7172
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
7273
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
73-
github.com/prometheus/client_golang v1.23.2 // indirect
7474
github.com/prometheus/client_model v0.6.2 // indirect
7575
github.com/prometheus/common v0.66.1 // indirect
7676
github.com/prometheus/procfs v0.17.0 // indirect

0 commit comments

Comments
 (0)