Repository navigation
Add spec.deletionPolicy to Certificate to control whether the Secret is deleted with it - #9481
ruslan-shaydullin wants to merge 4 commits into
Conversation
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]>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
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 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. |
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:
Deletemakes the Certificate the controller owner of its Secret,Orphanleaves the Secret alone, and an unset field keeps today's behaviour: the controller-wide default, which isDeletewith--enable-certificate-owner-ref=trueandOrphanotherwise. 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.UpdateDataand the two post-issuance checksSecretOwnerReferenceMismatchandSecretOwnerReferenceManagedFieldMismatch. 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 afterkubectl patch certificate internal-mtls --type merge -p '{"spec":{"deletionPolicy":"Orphan"}}', andstatus.revisionstays 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-nameannotation names it, or that carries no annotation yet. A Certificate whosesecretNamepoints 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 withdeletionPolicy: Deleteat an existing Secret and have it garbage collected on deletion. Sharing asecretNamebetween Certificates stays unsupported (#9029); this PR neither fixes nor worsens the update loop there.A few things worth knowing while reviewing.
Deleteremoves the whole Secret, including keys that other tools added to it, and also the previous Secret ifsecretNamechanges later; that is the point of the policy, and the docs PR I will open in cert-manager/website says so.Orphanremoves 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 recentrenewalfield (#8258) shipped without one. If you would rather have an operator switch for multi-tenant clusters, I have aCertificateDeletionPolicygate (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=falseas leaving existing owner references alone (not true since 1.8), it referenced a check that has since been split and renamed, and its example showedblockOwnerDeletion: falsewhereNewControllerRefsets true. I am happy to split that into its own PR if you prefer.A second PR adds
--default-secret-deletion-policyand 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