Skip to content

feat(metrics): Add metrics for dynamically generated serving certificates - #9432

Merged
cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
efadeev:dynamic-serving-certificate-metrics
Nov 23, 2026
Merged

cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
efadeev:dynamic-serving-certificate-metrics

Conversation

@efadeev

@efadeev efadeev commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

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 DynamicAuthority and labelled with the Secret storing the CA (secret_namespace, secret_name). The two certificate metrics also have a server label naming the server that uses the certificate (webhook or metrics):

Metric Type Description
certmanager_dynamic_serving_ca_expiration_timestamp_seconds gauge expiry of the CA currently in use
certmanager_dynamic_serving_certificate_expiration_timestamp_seconds gauge expiry of the most recently signed serving certificate
certmanager_dynamic_serving_certificate_signings_total counter signing attempts, status = success/failure

The 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. The server label 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:

certmanager_dynamic_serving_ca_expiration_timestamp_seconds{secret_name="cert-manager-webhook-ca",secret_namespace="cert-manager"} 1.82282889e+09
certmanager_dynamic_serving_certificate_expiration_timestamp_seconds{secret_name="cert-manager-webhook-ca",secret_namespace="cert-manager",server="webhook"} 1.79189769e+09
certmanager_dynamic_serving_certificate_signings_total{secret_name="cert-manager-webhook-ca",secret_namespace="cert-manager",server="webhook",status="success"} 2

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 ran make 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

Added the `certmanager_dynamic_serving_ca_expiration_timestamp_seconds`, `certmanager_dynamic_serving_certificate_expiration_timestamp_seconds` and `certmanager_dynamic_serving_certificate_signings_total` metrics to the webhook, cainjector and controller, which make it possible to alert on dynamically generated serving certificates (e.g. the webhook certificate) that are about to expire or fail to be renewed.

@cert-manager-prow cert-manager-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. area/monitoring Indicates a PR or issue relates to monitoring needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 6, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@cert-manager-prow cert-manager-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 6, 2026
@efadeev
efadeev force-pushed the dynamic-serving-certificate-metrics branch from 6c55a31 to 2b25542 Compare October 6, 2026 13:36
@cert-manager-prow cert-manager-prow Bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. and removed dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. labels Oct 6, 2026
@wallrj
wallrj requested review from wallrj and a balanced review from Copilot October 10, 2026 06:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment on lines +79 to +83
if err := reg.Register(c); err != nil {
if !errors.As(err, &prometheus.AlreadyRegisteredError{}) {
return err
}
}

@efadeev efadeev Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@efadeev
efadeev force-pushed the dynamic-serving-certificate-metrics branch from 2b25542 to 8954dac Compare October 10, 2026 18:42
Comment thread pkg/server/tls/authority/metrics.go Outdated
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_seconds switches between the two leaf certificates, depending on which was signed last.
  • certificate_signings_total adds 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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b073a79: the test now creates one series in each vector before linting.

@wallrj

wallrj commented Nov 22, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@cert-manager-prow cert-manager-prow Bot added ok-to-test and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Nov 22, 2026
// 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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() and rate() 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]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a follow-up: #9644.

@wallrj

wallrj commented Nov 23, 2026

Copy link
Copy Markdown
Member

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 failure series at start-up. It does not block the merge.

Two things to do after this merges:

  • The PR description still shows the metrics without the server label. Please update the table and the example output, because people will read it from the release notes.
  • Please add the three metrics to the metrics page on cert-manager.io, as you offered.

/lgtm
/approve

[Claude Fable 5.2]

@cert-manager-prow cert-manager-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Nov 23, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 23, 2026
@wallrj

wallrj commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

/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]

@cert-manager-prow
cert-manager-prow Bot merged commit 8600abe into cert-manager:master Nov 23, 2026
6 checks passed
@cert-manager-prow cert-manager-prow Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 23, 2026
@efadeev

efadeev commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Both follow-ups from #9432 (comment) are done:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/monitoring Indicates a PR or issue relates to monitoring dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. ok-to-test release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Metrics for webhook certificate

3 participants