Repository navigation
feat(metrics): Add metrics for dynamically generated serving certificates - #9432
Conversation
|
Hi @efadeev. Thanks for your PR. I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
6c55a31 to
2b25542
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Metric registration can silently accept a conflicting collector while leaving the new metrics unexposed.
1 open finding
What changed in this PR
Adds observability for dynamically generated serving certificates across cert-manager components.
Changes:
- Adds CA/certificate expiry gauges and signing counters.
- Instruments dynamic authority signing and CA rotation.
- Registers metrics with webhook, controller, and cainjector registries.
| File | Description |
|---|---|
pkg/webhook/server/server.go |
Registers metrics for the webhook. |
pkg/server/tls/authority/metrics.go |
Defines and records dynamic certificate metrics. |
pkg/server/tls/authority/metrics_test.go |
Tests metric recording and registration. |
pkg/server/tls/authority/authority.go |
Instruments CA updates and signing. |
pkg/server/tls/authority/authority_test.go |
Supports authorities with distinct test Secrets. |
pkg/metrics/metrics.go |
Registers metrics for the controller. |
cmd/cainjector/app/controller.go |
Registers metrics for cainjector. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if err := reg.Register(c); err != nil { | ||
| if !errors.As(err, &prometheus.AlreadyRegisteredError{}) { | ||
| return err | ||
| } | ||
| } |
There was a problem hiding this comment.
RegisterMetrics now only ignores AlreadyRegisteredError when ExistingCollector is the exact collector being registered, and returns the collision otherwise. Added TestRegisterMetricsCollision to cover it. The controller now also panics on a registration error, consistent with the surrounding MustRegister calls, instead of only logging it.
…ates Signed-off-by: Evgenii Fadeev <[email protected]>
2b25542 to
8954dac
Compare
| // The metrics are shared by all DynamicAuthority instances in the process and | ||
| // are partitioned by the Secret resource storing the CA, so that e.g. the | ||
| // webhook serving certificate and the metrics serving certificate of the same | ||
| // component can be told apart. |
There was a problem hiding this comment.
Important: two authorities that share a Secret also share one set of series, so they cannot be told apart.
Nothing stops the webhook serving certificate and the metrics serving certificate from using the same CA Secret. The config validation only checks that secretName and secretNamespace are set. In that case:
certificate_expiration_timestamp_secondsswitches between the two leaf certificates, depending on which was signed last.certificate_signings_totaladds the two together.
Could you either add a label that identifies the authority (for example the endpoint it serves), or change this comment and the help text to say that authorities sharing a Secret are reported together?
[Claude Fable 5.2]
There was a problem hiding this comment.
Added a server label to the leaf certificate metrics in b073a79, set from a new optional DynamicAuthority.Server field (webhook / metrics). I named it server rather than endpoint because prometheus-operator already sets an endpoint target label. The CA metric is still keyed by Secret only, since authorities sharing a Secret share the CA.
| // It will automatically set the NotBefore and NotAfter times appropriately. | ||
| func (d *DynamicAuthority) Sign(template *x509.Certificate) (*x509.Certificate, error) { | ||
| cert, err := d.sign(template) | ||
| d.observeSign(cert, err) |
There was a problem hiding this comment.
Nit (optional): observeSign runs after sign has released signMutex.
If two goroutines call Sign at the same time, the gauge can end up holding the expiry of the older certificate, not the most recently signed one. Today each DynamicSource signs from one goroutine, so nobody hits this yet. Calling observeSign from inside sign, while the mutex is held, would remove the ordering problem.
[Claude Fable 5.2]
There was a problem hiding this comment.
Done in b073a79: Sign now holds signMutex across both sign and observeSign.
| require.NoError(t, RegisterMetrics(reg)) | ||
|
|
||
| for _, c := range collectors() { | ||
| problems, err := testutil.CollectAndLint(c) |
There was a problem hiding this comment.
Nit (optional): this lint check passes without checking anything when the test runs alone.
CollectAndLint only lints the series that a collector emits. A GaugeVec or CounterVec with no children emits nothing. With go test -run TestRegisterMetrics no label values exist yet, so problems is always empty. The check only does real work when another test has already created the series.
Could you call WithLabelValues on each vector first, so the lint always sees one series of each metric?
[Claude Fable 5.2]
There was a problem hiding this comment.
Done in b073a79: the test now creates one series in each vector before linting.
|
/ok-to-test |
Signed-off-by: Evgenii Fadeev <[email protected]>
| // The CA has not been loaded yet, which is expected while the | ||
| // component is starting up. | ||
| case err != nil: | ||
| certificateSigningsTotal.WithLabelValues(d.SecretNamespace, d.SecretName, d.Server, "failure").Inc() |
There was a problem hiding this comment.
Nit (optional): the failure series does not exist until the first failure.
WithLabelValues(..., "failure") is first called when a signing fails. Before that, Prometheus has no such series. Two things follow:
increase()andrate()do not count the first failure, because the series starts at 1.- A query on
status="failure"returns no data on a healthy system, not 0.
Could you create both status series when the authority starts, for example in Run after the validation? Prometheus recommends this. This need not block the merge; a follow-up is fine.
[Claude Fable 5.2]
|
Thank you. All three points from my last review are fixed in b073a79, and CI is green. I left one optional comment about creating the Two things to do after this merges:
/lgtm [Claude Fable 5.2] |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/hold Edit: Tide merged this three seconds after I posted the hold, so the hold had no effect. The two optional points above still stand. If you want to take them up, please do so in a follow-up PR. Neither is required. [Claude Fable 5.2] |
|
Both follow-ups from #9432 (comment) are done:
|

Pull Request Motivation
Closes #7868.
When the webhook (or the controller/cainjector metrics endpoint) uses a dynamically generated serving certificate, there is currently no way to tell from metrics that the certificate or its CA is about to expire, or that renewal is failing. #7617 is an example where the webhook serving certificate silently stopped being renewed.
This PR adds three metrics, recorded by
DynamicAuthorityand labelled with the Secret storing the CA (secret_namespace,secret_name). The two certificate metrics also have aserverlabel naming the server that uses the certificate (webhookormetrics):certmanager_dynamic_serving_ca_expiration_timestamp_secondscertmanager_dynamic_serving_certificate_expiration_timestamp_secondscertmanager_dynamic_serving_certificate_signings_totalstatus=success/failureThe labels are needed because one process can run two authorities, e.g. the webhook serves both the webhook endpoint (
cert-manager-webhook-ca) and, optionally, the metrics endpoint (cert-manager-metrics-ca) with dynamic certificates. Theserverlabel tells the two apart even if they are configured to use the same Secret.Signing attempts made before the CA has been loaded (
ErrCertificateNotAvailable) are not counted as failures, as they are expected on every start-up.The metrics are registered with the controller-runtime registry in the webhook and cainjector, and with the controller's own registry. Example output from the webhook:
I'm happy to rename the metrics or change the labels. If this is accepted, I'll add them to the metrics page on cert-manager.io.
Testing: unit tests in
pkg/server/tls/authority/metrics_test.go. I also ranmake verify-golangci-lint, ran the webhook integration tests, and checked the output above against an envtest API server.AI disclosure: the implementation, tests and this description were written with Claude Code (Claude Opus 5.5) under my direction. I have reviewed and understand all of the changes.
Kind
/kind feature
Release Note