Skip to content

fix(e2e): give each SelfSigned CSR spec its own Secret - #9320

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
wallrj-cyberark:fix-selfsigned-csr-e2e-secret-race
Sep 9, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
wallrj-cyberark:fix-selfsigned-csr-e2e-secret-race

Conversation

@wallrj-cyberark

Copy link
Copy Markdown
Member

Pull Request Motivation

Fixes #9318.

Two e2e specs create the same Secret, cert-manager/selfsigned-test, with the name and the namespace hard-coded. make/e2e.sh runs ginkgo with nodes=40 and --randomize-all, so the two sometimes run at the same time on different processes, and the second fails:

[FAIL] [cert-manager] CertificateSigningRequests SelfSigned Secret [It] ClusterIssuer:
       private key Secret is updated with a valid private key after the request is created

Unexpected error: secrets "selfsigned-test" already exists (409 AlreadyExists)

The spec at L192 creates the Secret partway through and deletes it in JustAfterEach. The spec at L254 creates the same object as its first step. Whichever starts second gets the 409.

The Secret has to live in the cluster resource namespace, because that is where a ClusterIssuer resolves the experimental.cert-manager.io/private-key-secret-name annotation, and the framework does not randomise that namespace. The name is the only part we control.

What changed

Each spec now generates its own Secret name in JustBeforeEach and uses the same value in the annotation. Generating it there rather than at tree-construction time also means a retry gets a fresh name — the losing spec holds the Secret for about a second, so a retry that reuses the same name is certain to lose again.

I also asserted the error on two Secret creates that were discarding it. That is not tidying; it changes how often we can even see this bug. client-go's generic Create returns a non-nil zero object alongside the error (gentype/type.go:244):

func (c *Client[T]) Create(ctx context.Context, obj T, opts metav1.CreateOptions) (T, error) {
	result := c.newObject()
	err := c.client.Post().
		...
		Into(result)
	return result, err
}

So the spec that lost the race carried on with a nameless Secret, waited 20 seconds for a signature that was never coming, and then failed teardown with resource name may not be empty. Only one of the two specs could produce the clean 409 above. Collisions that went the other way looked like an unrelated signing timeout, so this flake is rarer in the record than it is in reality — worth knowing before triaging #9318 on the basis of "we have only seen it once".

The two Issuer: specs keep the fixed name. They write into the framework's per-spec namespace, so their names are already unique.

How this was tested

  • go build -tags e2e_test ./... and go vet -tags e2e_test ./suite/certificatesigningrequests/... pass in the test/e2e module, and go fmt reports no change.
  • I checked the sibling test/e2e/suite/certificaterequests/selfsigned/secret.go suite, which has ClusterIssuer specs of the same shape. It is not affected: a CertificateRequest is namespaced, so its private key Secret is resolved in the CertificateRequest's own namespace even when the issuer is cluster-scoped, and every Secret there already goes into f.Namespace.Name. Lines 248, 267 and 318 of the file changed here are the only places in the e2e tree that touch Secrets("cert-manager").

Please be clear about what a green e2e run proves here. It shows the specs still pass and that the annotation and the Secret still agree. It does not prove the race is gone, because the race is rare and a single run would very likely have passed before this change too. The argument is structural: after this, no two specs can ask for the same Secret name.

Kind

/kind flake

Release Note

NONE

The two ClusterIssuer specs in the "CertificateSigningRequests SelfSigned
Secret" suite both created cert-manager/selfsigned-test, with the name and
the namespace hard-coded. make/e2e.sh runs ginkgo with nodes=40 and
--randomize-all, so the two sometimes run at the same time on different
processes, and the second fails with:

    secrets "selfsigned-test" already exists

The Secret has to live in the cluster resource namespace, because that is
where a ClusterIssuer resolves the private-key-secret-name annotation, and
the framework does not randomise that namespace. Randomising the name is
the only part we control, so generate it per spec in JustBeforeEach and use
the same value in the annotation.

Generating it there rather than at tree-construction time also means a
retry gets a fresh name. The spec that loses holds the Secret for about a
second, so a retry that reuses the same name is certain to lose again.

Two Secret creates also discarded their error, so only one of the two specs
could report the collision at all. client-go's generic Create returns a
non-nil zero object alongside the error (gentype/type.go:244), so the
losing spec carried on with a nameless Secret, waited 20s for a signature
that never came, and then failed teardown with "resource name may not be
empty". Collisions that went that way looked nothing like a 409, which
makes the flake rarer in the record than it is in reality. Both creates now
assert.

The two Issuer specs keep the fixed name. They write into the framework's
per-spec namespace, so their names are already unique.

Signed-off-by: Richard Wall <[email protected]>
@cert-manager-prow cert-manager-prow Bot added release-note-none Denotes a PR that doesn't merit a release note. kind/flake Categorizes issue or PR as related to a flaky test. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. area/testing Issues relating to testing size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 9, 2026
@wallrj-cyberark

Copy link
Copy Markdown
Member Author

/test pull-cert-manager-master-e2e-v1-34

// resource namespace, which the framework does not randomise, so a
// fixed name would make them collide when Ginkgo runs them in
// parallel.
secretName = "selfsigned-test-" + rand.String(10)

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.

I tend to use GenerateName instead of rand in tests, but that only works with a real control plane, such as envtest or a real cluster. Can we use it here and read out the name after the secret is created? It is a matter of preference, so not a blocker.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea, and there is a direct precedent for it that I should acknowledge: suite/conformance/certificatesigningrequests/selfsigned/selfsigned.go#L68 does exactly this — GenerateName, same annotation, same cluster resource namespace, name read back off the created object.

It does not quite work here though, and the reason is ordering rather than preference.

The ClusterIssuer: private key Secret is updated with a valid private key… spec creates the Secret first and the CSR afterwards, so GenerateName would work there, just like the conformance helper.

Its neighbour, ClusterIssuer: the private key Secret is created after the request is created…, goes the other way round. It creates the CSR with the annotation already set, waits for a SecretNotFound warning event, and only then creates the Secret. The annotation has to name a Secret that does not exist yet — that absence is the thing under test — so there is no created object to read a generated name back from.

That would leave GenerateName in one spec and rand.String in the one twelve lines below it. I would rather both ClusterIssuer specs read the same way, so I have kept rand.String for both. rand.String is well established here too — 42 uses across 13 files in test/e2e, including suite/issuers/ca/clusterissuer.go#L39, which names a cluster-scoped issuer's Secret the same way.

Happy to switch the one spec that can use it if you would still prefer that.

@erikgb erikgb 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.

/lgtm
/approve
/hold

Adding a hold in case you want to do something with my proposal. But feel free to unhold.

@cert-manager-prow cert-manager-prow Bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Sep 9, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: erikgb

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 Sep 9, 2026
@wallrj-cyberark

Copy link
Copy Markdown
Member Author

/unhold

Thanks @erikgb. I have left both ClusterIssuer specs on rand.String for the reason in the thread above: the …Secret is created after the request is created… spec sets the annotation before the Secret exists, so there is no created object to read a generated name from, and using GenerateName in only its neighbour would split the style within the file. Happy to revisit if you would still rather have it in the one spec that can take it.

@cert-manager-prow cert-manager-prow Bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 9, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit fe3cba7 into cert-manager:master Sep 9, 2026
7 checks passed
@wallrj-cyberark
wallrj-cyberark deleted the fix-selfsigned-csr-e2e-secret-race branch September 9, 2026 14:53
@wallrj-cyberark

Copy link
Copy Markdown
Member Author

/cherry-pick release-1.21

Backporting for v1.21.2 (tracker: #9237). This is a test-only change. It stops the two CertificateSigningRequests SelfSigned Secret ClusterIssuer specs racing on the shared Secret cert-manager/selfsigned-test, which fails the e2e suite with secrets "selfsigned-test" already exists when Ginkgo happens to run them at the same time. It also asserts two Secret creates that were discarding their error, so both sides of the race now fail loudly rather than only one.

The file on release-1.21 is currently identical to what this changed on master, so it should apply cleanly.

@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj-cyberark: new pull request created: #9321

Details

In response to this:

/cherry-pick release-1.21

Backporting for v1.21.2 (tracker: #9237). This is a test-only change. It stops the two CertificateSigningRequests SelfSigned Secret ClusterIssuer specs racing on the shared Secret cert-manager/selfsigned-test, which fails the e2e suite with secrets "selfsigned-test" already exists when Ginkgo happens to run them at the same time. It also asserts two Secret creates that were discarding their error, so both sides of the race now fail loudly rather than only one.

The file on release-1.21 is currently identical to what this changed on master, so it should apply cleanly.

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.

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/testing Issues relating to testing dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/flake Categorizes issue or PR as related to a flaky test. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky e2e: the two SelfSigned CSR ClusterIssuer specs race on a shared Secret

3 participants