Skip to content

Record a Warning event when CSR generation fails - #9140

Merged
cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
yogeshbendre:fix/csr-generation-failure-warning-event
Aug 21, 2026
Merged

cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
yogeshbendre:fix/csr-generation-failure-warning-event

Conversation

@yogeshbendre

@yogeshbendre yogeshbendre commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request Motivation

Fixes #9139

This is to fix the issue #9139.

createNewCertificateRequest (pkg/controller/certificates/requestmanager/requestmanager_controller.go)
already records a Warning event via the reasonRequestFailed reason when creating the CertificateRequest
object fails, but when the earlier pki.GenerateCSR call fails, the error is only logged
("Failed to generate CSR - will not retry") and the function returns nil — silently swallowed. Since
nothing else clears the Certificate's Issuing condition without a CertificateRequest being created, this
leaves the Certificate stuck at Ready=False indefinitely with zero user-visible signal beyond a controller
log line.

This PR adds the same c.recorder.Eventf(...) call already used for the sibling failure path a few lines
below, so both failure modes in this function are now surfaced consistently. It's a one-line functional
change plus a regression test (added to the existing table-driven TestProcessItem suite) that reproduces
the swallow via a malformed nameConstraints IP range — confirmed the added test fails without the fix and
passes with it.

Reproduced live on a kind cluster running cert-manager v1.21.1 before filing (details in the linked issue),
then verified the fix itself end-to-end: built this branch into a real controller image, swapped it into the
same live cluster in place of the stock image, and confirmed kubectl describe certificate now shows
Warning RequestFailed Failed to generate CSR: ... on the same Certificate that previously showed no events
at all.

Before Fix

There is no recoreded warning event from kubectl describe certificate

Status:
  Conditions:
    Last Transition Time:        2026-08-14T04:30:36Z
    Message:                     Issuing certificate as Secret does not exist
    Observed Generation:         1
    Reason:                      DoesNotExist
    Status:                      True
    Type:                        Issuing
    Last Transition Time:        2026-08-14T04:30:36Z
    Message:                     Issuing certificate as Secret does not exist
    Observed Generation:         1
    Reason:                      DoesNotExist
    Status:                      False
    Type:                        Ready
  Next Private Key Secret Name:  repro-root-ca-cjd6w
Events:                          <none>  <--------------------------------- NOTHING HERE

After Fix

Note generated warning event from kubectl describe certificate

Status:
  Conditions:
    Last Transition Time:        2026-08-14T04:30:36Z
    Message:                     Issuing certificate as Secret does not exist
    Observed Generation:         1
    Reason:                      DoesNotExist
    Status:                      True
    Type:                        Issuing
    Last Transition Time:        2026-08-14T04:30:36Z
    Message:                     Issuing certificate as Secret does not exist
    Observed Generation:         1
    Reason:                      DoesNotExist
    Status:                      False
    Type:                        Ready
  Next Private Key Secret Name:  repro-root-ca-cjd6w
Events:
  Type     Reason         Age   From                                       Message
  ----     ------         ----  ----                                       -------
  Warning  RequestFailed  17m   cert-manager-certificates-request-manager  Failed to generate CSR: invalid CIDR address: 10.0.0.0

Kind

/kind bug

Release Note

cert-manager now records a Warning event on a `Certificate` when CSR generation fails during (re-)issuance, instead of only logging the failure.

createNewCertificateRequest already handles the CertificateRequest-creation
failure case by recording a Warning event via the reasonRequestFailed reason,
but the earlier CSR-generation failure (pki.GenerateCSR) a few lines above it
only logs the error and returns nil, so it's silently swallowed. A Certificate
that hits this path gets stuck at Issuing=True/Ready=False indefinitely, since
no CertificateRequest is ever created to clear the condition, with no Event or
condition change to signal anything is wrong - the only trace is a controller
log line.

This mirrors the existing pattern for the sibling failure case a few lines
below, so the fix is independent of what causes GenerateCSR to fail (e.g. a
malformed nameConstraints IP range, added as the regression test here).

Signed-off-by: yogeshbendre <[email protected]>
@cert-manager-prow cert-manager-prow Bot added kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 14, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Hi @yogeshbendre. 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/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 14, 2026
@wallrj
wallrj requested a balanced review from Copilot August 20, 2026 17:42

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.

Pull request overview

Records a user-visible Warning event when CSR generation fails during certificate issuance or renewal.

Changes:

  • Emits a RequestFailed Warning event for CSR generation errors.
  • Adds regression coverage using malformed NameConstraints.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
requestmanager_controller.go Records CSR generation failures as events.
requestmanager_controller_test.go Verifies the Warning event is emitted.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@wallrj wallrj left a comment

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.

Thanks @yogeshbendre — this is a useful improvement; until now this failure was only visible in the controller logs. One suggestion inline, plus two non-blocking observations:

  1. The root cause is one level deeper: admission validation of nameConstraints checks the feature gate, isCA and permitted/excluded presence, but never parses ipRanges (certificate.go#L215-L227), so this permanently un-issuable spec is accepted at apply time. The event is still worth having — it also covers other GenerateCSR failure modes such as literal subject parse errors — but a follow-up validating the CIDRs at admission would reject the bad spec at kubectl apply. Happy to open an issue for that.

  2. The sibling failure paths just below (pki.EncodeCSR, pem.Encode) still return the error and retry forever with backoff and no event — the same invisibility problem, but pre-existing and rare; nothing to do in this PR.

with claude fable-5

)
if err != nil {
log.Error(err, "Failed to generate CSR - will not retry")
c.recorder.Eventf(crt, corev1.EventTypeWarning, reasonRequestFailed, "Failed to generate CSR: %s", err.Error())

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.

Consider matching the log line by saying "will not retry" in the event too:

Suggested change
c.recorder.Eventf(crt, corev1.EventTypeWarning, reasonRequestFailed, "Failed to generate CSR: %s", err.Error())
c.recorder.Eventf(crt, corev1.EventTypeWarning, reasonRequestFailed, "Failed to generate CSR: %s - will not retry", err.Error())

The claim is accurate: return nil here routes the item to queue.Forget, so it is never requeued with backoff. The next attempt is a spec change or the 10 hour informer resync, and since the error is derived purely from the spec, every re-attempt fails identically. Without this, someone watching kubectl describe may wait for cert-manager to recover on its own when it never will.

(The expected event string in the new test case will need the matching update.)

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.

Thank you @wallrj , this is a very good feedback.
I have made the necessary changes and retested.
Pushed the changes to the repo.
The logs now look as follows:

Events:
  Type     Reason         Age   From                                       Message
  ----     ------         ----  ----                                       -------
  Normal   Issuing        17s   cert-manager-certificates-trigger          Issuing certificate as Secret does not exist
  Normal   Generated      17s   cert-manager-certificates-key-manager      Stored new private key in temporary Secret resource "repro-root-ca-brzn6"
  Warning  RequestFailed  17s   cert-manager-certificates-request-manager  Failed to generate CSR: invalid CIDR address: 10.0.0.0 - will not retry

@wallrj

wallrj commented Aug 21, 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 Aug 21, 2026
@wallrj

wallrj commented Aug 21, 2026

Copy link
Copy Markdown
Member

Thanks @yogeshbendre, the event message and test now match the log line.

/approve
/lgtm
/label tide/merge-method-squash

with claude fable-5

@cert-manager-prow cert-manager-prow Bot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Aug 21, 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 lgtm Indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Aug 21, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 97fbeb1 into cert-manager:master Aug 21, 2026
6 checks passed
@wallrj

wallrj commented Aug 21, 2026

Copy link
Copy Markdown
Member

Follow-up issues opened for the two observations in my review above: #9181 (validate nameConstraints.ipRanges CIDRs at admission time) and #9182 (Warning event for the CSR encoding failure paths).

with claude fable-5

@yogeshbendre

yogeshbendre commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you @wallrj for your useful reviews and approvals. :)

Also thanks for the two new related issues, they make perfect sense.
Looking forward to those changes merging soon :)

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. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. 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/S Denotes a PR that changes 10-29 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When CSR generation fails, no warning event is generated, making it difficult to check why certificate is stuck with Ready=False

3 participants