Skip to content

Add spec.deletionPolicy to Certificate to control whether the Secret is deleted with it - #9481

Open
ruslan-shaydullin wants to merge 4 commits into
cert-manager:masterfrom
ruslan-shaydullin:deletion-policy-1-field
Open

ruslan-shaydullin wants to merge 4 commits into
cert-manager:masterfrom
ruslan-shaydullin:deletion-policy-1-field

Conversation

@ruslan-shaydullin

Copy link
Copy Markdown

Pull Request Motivation

This implements the merged design design/20220720-per-certificate-owner-ref.md (#5324) and fixes #9085.

Today a single controller flag, --enable-certificate-owner-ref, decides for every Certificate in the cluster whether its Secret is garbage collected when the Certificate goes away. That doesn't work when one installation serves both public ACME certificates, where you very much want the Secret to survive a deleted Certificate (re-issuing costs rate limits and downtime), and short-lived internal certificates that pile up in long-lived namespaces. And in managed or multi-tenant clusters the people who create Certificates can't change controller flags at all.

With this change the Certificate says it itself:

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: internal-mtls
spec:
  secretName: internal-mtls-tls
  deletionPolicy: Delete
  # ...

Delete makes the Certificate the controller owner of its Secret, Orphan leaves the Secret alone, and an unset field keeps today's behaviour: the controller-wide default, which is Delete with --enable-certificate-owner-ref=true and Orphan otherwise. Nothing changes for an existing installation until one of its Certificates sets the field.

How it works: the question "does this Secret need an owner reference" used to be answered by the flag in three places, SecretsManager.UpdateData and the two post-issuance checks SecretOwnerReferenceMismatch and SecretOwnerReferenceManagedFieldMismatch. They now ask one function, certificates.SecretOwnerReferenceExpected, which combines the field with the controller default. Because those checks already run on every reconcile outside an issuance (since #4888), changing the field on a live Certificate adds or removes the owner reference through the existing re-apply path, with no new CertificateRequest. On a kind cluster the reference shows up or disappears about a second after kubectl patch certificate internal-mtls --type merge -p '{"spec":{"deletionPolicy":"Orphan"}}', and status.revision stays where it was.

One rule the design did not spell out, and I want to call it out because it is a safety property: the owner reference is only ever placed on the Secret the Certificate owns, that is the one whose cert-manager.io/certificate-name annotation names it, or that carries no annotation yet. A Certificate whose secretName points at another Certificate's Secret never gets an owner reference on it, whatever its policy says. Without that rule, anyone able to create a Certificate in a namespace could point a second Certificate with deletionPolicy: Delete at an existing Secret and have it garbage collected on deletion. Sharing a secretName between Certificates stays unsupported (#9029); this PR neither fixes nor worsens the update loop there.

A few things worth knowing while reviewing. Delete removes the whole Secret, including keys that other tools added to it, and also the previous Secret if secretName changes later; that is the point of the policy, and the docs PR I will open in cert-manager/website says so. Orphan removes owner references that cert-manager itself set through Server-Side Apply (1.8 and later) and leaves everything else alone, exactly as the flag does today. There is no feature gate: the design says the field doesn't need one and the recent renewal field (#8258) shipped without one. If you would rather have an operator switch for multi-tenant clusters, I have a CertificateDeletionPolicy gate (Beta, on by default) ready as a follow-up commit and can add it here.

The last commit updates the design document itself. It contradicted itself on whether a changed default applies "on the next issuance" or immediately (the code has been immediate since #4888), it described --enable-certificate-owner-ref=false as leaving existing owner references alone (not true since 1.8), it referenced a check that has since been split and renamed, and its example showed blockOwnerDeletion: false where NewControllerRef sets true. I am happy to split that into its own PR if you prefer.

A second PR adds --default-secret-deletion-policy and deprecates --enable-certificate-owner-ref, as the design proposes. I kept it out of here so this one stays about the API field and the reconciliation.

Thanks to @RomanenkoDenys for #5158, whose review threads with @munnerz and @JoshVanL settled that this has to be reconciled dynamically rather than at issuance time.

This is my first PR here, so I will need someone to /ok-to-test.

/cc @maelvls @erikgb

Kind

/kind feature

Release Note

Certificates gain a `spec.deletionPolicy` field (`Delete` or `Orphan`) that controls whether the Secret named by `secretName` is deleted together with the Certificate. Changing the field on an existing Certificate adds or removes the owner reference on its Secret without re-issuing the certificate, and a Certificate never takes ownership of a Secret annotated with another Certificate's name. When the field is unset, the behaviour follows `--enable-certificate-owner-ref` as before.

Certificates can now say what happens to the Secret named by secretName
when they are deleted: Delete makes the Certificate the controller owner
of the Secret so that the garbage collector removes it, Orphan leaves the
Secret in place. The field is optional and has no defaulting; an unset
field means the controller-wide default, which today follows
--enable-certificate-owner-ref, so existing installations keep their
behaviour until a Certificate sets the field.

The webhook rejects values outside Delete and Orphan. Generated code
(CRDs, conversion, openapi, apply configurations) is regenerated with
make generate; the chart CRD template needed a second generate-crds run
because the first pass evaluates the wildcard before controller-gen has
written the files.

Implements design/20220720-per-certificate-owner-ref.md for cert-manager#9085.

Signed-off-by: Ruslan Shaydullin <[email protected]>
SecretsManager.UpdateData and the two post-issuance checks,
SecretOwnerReferenceMismatch and SecretOwnerReferenceManagedFieldMismatch,
used to read one controller-wide bool. They now ask
certificates.SecretOwnerReferenceExpected, which combines
spec.deletionPolicy with the controller default and only answers true for
the Secret the Certificate owns: the one whose
cert-manager.io/certificate-name annotation names the Certificate, or
carries no annotation yet. A Certificate whose secretName points at a
Secret owned by another Certificate never gets an owner reference on it,
whatever its policy says, so it cannot have that Secret garbage
collected.

Because the checks run on every reconcile outside an issuance, changing
the field on a live Certificate adds or removes the owner reference
through the existing re-apply path, without a new CertificateRequest.
The controller default comes from CertificateOptions.DefaultSecretDeletionPolicy,
which maps EnableOwnerRef to Delete and everything else to Orphan.

Violation messages name the source of the decision instead of
--enable-certificate-owner-ref=%t. Existing tests keep their expectations
and only pass the policy instead of the bool; new cases cover the field,
the default, foreign owner references and Secrets owned by another
Certificate.

Signed-off-by: Ruslan Shaydullin <[email protected]>
Test_IssuingController_DeletionPolicy runs the issuing controller against
envtest with the default policy Orphan: patching spec.deletionPolicy to
Delete adds the owner reference to the Secret and patching it back to
Orphan removes it, in both cases without a CertificateRequest or a
revision bump. A second Certificate pointing at a Secret annotated with
another Certificate's name never gains an owner reference on it.

Signed-off-by: Ruslan Shaydullin <[email protected]>
The document said in one place that a change of the default policy takes
effect on the next issuance and in another that it is immediate; since
cert-manager#4888 the controller reconciles owner references on every reconcile, and
that is what the implementation does for the field as well. It also said
that --enable-certificate-owner-ref=false leaves existing owner
references alone, which stopped being true in 1.8, pointed at a check
that has since been split and renamed, and showed blockOwnerDeletion as
false where NewControllerRef sets it to true. Describe the ownership
rule that keeps a Certificate from taking over another Certificate's
Secret.

Signed-off-by: Ruslan Shaydullin <[email protected]>
@cert-manager-prow
cert-manager-prow Bot requested review from erikgb and maelvls October 15, 2026 11:18
@cert-manager-prow cert-manager-prow Bot added kind/feature Categorizes issue or PR as related to a new feature. 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. labels Oct 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign hjoshi123 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 area/api Indicates a PR directly modifies the 'pkg/apis' directory area/deploy Indicates a PR modifies deployment configuration area/testing Issues relating to testing needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Hi @ruslan-shaydullin. 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 kind/design Categorizes issue or PR as related to design. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Oct 15, 2026
@wallrj wallrj mentioned this pull request Nov 4, 2026
2 of 40 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api Indicates a PR directly modifies the 'pkg/apis' directory area/deploy Indicates a PR modifies deployment configuration area/testing Issues relating to testing dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/design Categorizes issue or PR as related to design. kind/feature Categorizes issue or PR as related to a new feature. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement per-Certificate Secret deletion policy

1 participant