Skip to content

chore(e2e): migrating kyverno cluster policy - #8886

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
hjoshi123:chore/migrate-cluster-policy
Jul 8, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
hjoshi123:chore/migrate-cluster-policy

Conversation

@hjoshi123

Copy link
Copy Markdown
Collaborator

Pull Request Motivation

As we approach the deadline of when ClusterPolicy is going to removed, it is important that we migrate these to ValidatingPolicies. We do have to drop one of the validating policies because the baseline here doesn't include apparmor.

Kind

/kind cleanup

Release Note

NONE

@cert-manager-prow cert-manager-prow Bot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 17, 2026
@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch from 7371b93 to 88f3a60 Compare June 17, 2026 16:01
@erikgb
erikgb requested a review from Copilot June 17, 2026 16:07
@erikgb

erikgb commented Jun 17, 2026

Copy link
Copy Markdown
Member

/cc

Thanks @hjoshi123! Did you consider using a VAP instead? The syntax is very similar. That could remove our dependency on Kyverno.

@cert-manager-prow
cert-manager-prow Bot requested a review from erikgb June 17, 2026 16:10

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

This PR migrates the Kyverno policy bundle used by cert-manager’s E2E environment from legacy ClusterPolicy resources to CEL-based ValidatingPolicy resources, aligning with Kyverno’s newer policy APIs and updating the Kyverno image versions used in test clusters.

Changes:

  • Bump Kyverno and kyvernopre images used during E2E setup to v1.18.1 (amd64/arm64).
  • Replace the generated Kyverno policy bundle (policy.yaml) with policies.kyverno.io/v1 ValidatingPolicy resources.
  • Update the Kyverno kustomization to source the new pod-security-vpol/* bundles and add a locally maintained restrict-automount-sa-token ValidatingPolicy.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
make/e2e-setup.mk Updates Kyverno/kyvernopre image references used by E2E setup.
make/config/kyverno/kustomization.yaml Switches to ValidatingPolicy sources and patches (apiVersion/actions/matchConditions).
make/config/kyverno/restrict-automount-sa-token.yaml Adds a local ValidatingPolicy for automount SA token restriction.
make/config/kyverno/policy.yaml Updates the generated policy bundle to ValidatingPolicy/CEL equivalents.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/policy.yaml Resolved
Comment thread make/config/kyverno/policy.yaml Resolved
Comment thread make/config/kyverno/policy.yaml Resolved
@hjoshi123

Copy link
Copy Markdown
Collaborator Author

@erikgb I did think of it but we are deploying the baseline policies which adds a lot of value which we would have to rewrite if we move to VAP. I didnt think it was worth it to remove the dependency right now but definitely something to look into as a separate card. I am not a big fan of how kyverno aggressively deprecated CPol

Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
@wallrj-cyberark
wallrj-cyberark self-requested a review June 18, 2026 16:17
Comment thread make/config/kyverno/kustomization.yaml Resolved
@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch from 88f3a60 to 381e75b Compare June 20, 2026 20:55
@hjoshi123
hjoshi123 requested review from SgtCoDFish and erikgb June 20, 2026 20:55

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

Sorry for the delayed review, @hjoshi123! I had to take a closer look, and added a couple of concrete suggestions now. 👍

Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
Comment thread make/config/kyverno/kustomization.yaml Resolved
@wallrj-cyberark

Copy link
Copy Markdown
Member

Thank you for this work, @hjoshi123.

I want to share some context on the design of the make/config/kyverno/ mechanism, since I created it and wrote most of its git history (commits 767b281d8, c8640908e, 80896bce3, 0ffc3c255, 541798c9e, among others).

The kustomization.yaml is a build input, and policy.yaml is a generated artifact

The intended workflow, documented in the kustomization.yaml header, is:

kustomize build . > policy.yaml

The kustomization.yaml pulls policies from the upstream kyverno/policies repository. A developer runs kustomize build periodically to regenerate policy.yaml with the latest upstream policies. The resulting policy.yaml is committed and goes through code review — that is the safety net.

The resource URLs in the kustomization.yaml have been deliberately unpinned (tracking main / HEAD) since the mechanism was introduced. This is by design: it allows a developer to regenerate policy.yaml to pick up upstream improvements simply by re-running the kustomize build command and reviewing the diff.

I would recommend reverting the SHA pins that were added to the raw/ URLs. With SHA pinning, kustomize build always produces the same output, which defeats the purpose. To update policies, you would need to manually look up the latest SHA, edit the kustomization.yaml, then rebuild — adding friction to a workflow that was designed to be low-friction. The pod-security-vpol/baseline and pod-security-vpol/restricted resources are already unpinned; I would suggest keeping the restrict-sa-automount-sa-token and require-ro-rootfs resources unpinned too, for consistency and to preserve the original workflow.

The Kyverno issue #2408 workaround is resolved by this migration

The old kustomization.yaml comment referenced kyverno/kyverno#2408 and described the patching approach as "a bit of a hack, because it is difficult to get Kustomize to patch all the rules in the Kyverno ClusterPolicy custom resource." The JSON patch spec/rules/0/exclude could only target the first rule.

The migration to ValidatingPolicy resolves this nicely. The top-level spec.matchConditions applies uniformly to all validations, so a single kustomize patch adds the namespace exclusion for all policies at once. This is a genuine improvement. The updated kustomization.yaml comment should reflect that the mechanism is now clean — there is no longer a need to reference the old workaround.

Other suggestions

  1. Helm chart version: the PR bumps Kyverno images to v1.18.1 but the Helm chart version in make/e2e-setup.mk remains at 3.6.2. The latest stable chart is 3.8.1. It would be worth bumping the chart version to match, to avoid compatibility issues.

  2. Make target for regeneration: currently the kustomize build . > policy.yaml step is documented only in a comment. It would be useful to add a make target (e.g., make generate-kyverno-policy) or a verify step that checks policy.yaml is up to date with the kustomization. This would make the workflow discoverable and prevent drift.

  3. apiVersion patching is temporary: the upstream pod-security-vpol/baseline and pod-security-vpol/restricted policies still use policies.kyverno.io/v1alpha1, so the apiVersion patch to v1 is currently necessary. A comment noting this is temporary (to be removed when upstream moves to v1) would help future maintainers, as @erikgb suggested.

@wallrj-cyberark wallrj-cyberark 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.

@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch from 381e75b to 73638d9 Compare July 7, 2026 15:12
@hjoshi123

Copy link
Copy Markdown
Collaborator Author

@erikgb @wallrj-cyberark could you re-review the PR? Based on your feedback I reverted the SHA pinning and now its looking at main for the manifests... and also I dont think helm version change should be a part of this PR since it doesnt directly affect the work in this PR.

@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch from 73638d9 to 3869a2a Compare July 7, 2026 15:13
@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch 2 times, most recently from 687cabe to b5c5b34 Compare July 7, 2026 17:24
@hjoshi123
hjoshi123 force-pushed the chore/migrate-cluster-policy branch from b5c5b34 to 9d364f3 Compare July 8, 2026 03:56

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

/approve

Also LGTM, but since @wallrj has requested changes I don't want to put my LGTM to block Prow on the project. PTAL @wallrj!

@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 Jul 8, 2026

@wallrj-cyberark wallrj-cyberark 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.

Thank you for addressing the feedback, @hjoshi123.

Verified the key changes since the previous review:

  • SHA pinning reverted: kustomization.yaml now tracks main, consistent with the original design where kustomization.yaml is the build input and policy.yaml is the generated artifact.
  • matchConstraints/namespaceSelector: Adopted erikgb's suggestion — namespace exclusion now uses spec.matchConstraints.namespaceSelector.matchExpressions instead of matchConditions, which is cleaner and integrates with the Kubernetes admission framework natively.
  • Helm chart version bumped: 3.6.2 → 3.8.1, consistent with the Kyverno image bump v1.16.2 → v1.18.1.
  • Generated artifact verified: Ran kustomize build . > policy.yaml locally and confirmed the output matches the committed policy.yaml exactly.

@wallrj-cyberark
wallrj-cyberark requested a review from wallrj July 8, 2026 10:33
@wallrj

wallrj commented Jul 8, 2026

Copy link
Copy Markdown
Member

/approve
/lgtm

@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: erikgb, wallrj, wallrj-cyberark

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 lgtm Indicates that a PR is ready to be merged. label Jul 8, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 2e1fb43 into cert-manager:master Jul 8, 2026
wallrj-cyberark added a commit to wallrj-cyberark/cert-manager that referenced this pull request Jul 22, 2026
…igration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in cert-manager#8886 bumped the chart
to 3.8.1. Two distinct problems, both introduced by that migration:

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk.

2. ServiceAccount automount policy regression: fixing (1) let the
   suite actually run, surfacing that cert-manager#8886 also changed
   restrict-sa-automount-sa-token from validating Pods
   (spec.automountServiceAccountToken) to validating ServiceAccounts
   themselves (object.automountServiceAccountToken) - a different
   resource and field, not an equivalent rewrite. This denies creation
   of any e2e-created ServiceAccount that doesn't explicitly set the
   field, breaking every RBAC-boundary, ACME HTTP01, and Vault issuer
   suite (161 specs, 100% traced to this one policy denial):

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by setting automountServiceAccountToken: false on the four
   ServiceAccounts the e2e suite creates for its own test scaffolding
   (RBAC-boundary helper, CertificateRequest approval/userinfo tests,
   and the Vault Kubernetes-auth bound ServiceAccount). This complies
   with the policy exactly as a real cluster operator would, rather
   than exempting the namespaces where cert-manager's own operational
   surface (e.g. ACME HTTP01 solver pods) is what the job exists to
   check.

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>
wallrj-cyberark added a commit to wallrj-cyberark/cert-manager that referenced this pull request Jul 22, 2026
…igration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in cert-manager#8886 bumped the chart
to 3.8.1. Two distinct problems, both introduced by that migration:

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk.

2. ServiceAccount automount policy regression: fixing (1) let the
   suite actually run, surfacing that cert-manager#8886 also changed
   restrict-sa-automount-sa-token from validating Pods
   (spec.automountServiceAccountToken) to validating ServiceAccounts
   themselves (object.automountServiceAccountToken) - a different
   resource and field, not an equivalent rewrite. This denies creation
   of any e2e-created ServiceAccount that doesn't explicitly set the
   field, breaking every RBAC-boundary, ACME HTTP01, and Vault issuer
   suite (161 specs, 100% traced to this one policy denial):

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by setting automountServiceAccountToken: false on the four
   ServiceAccounts the e2e suite creates for its own test scaffolding
   (RBAC-boundary helper, CertificateRequest approval/userinfo tests,
   and the Vault Kubernetes-auth bound ServiceAccount). This complies
   with the policy exactly as a real cluster operator would, rather
   than exempting the namespaces where cert-manager's own operational
   surface (e.g. ACME HTTP01 solver pods) is what the job exists to
   check.

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>
wallrj-cyberark added a commit to wallrj-cyberark/cert-manager that referenced this pull request Jul 22, 2026
…igration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in cert-manager#8886 bumped the chart
to 3.8.1. Three distinct problems surfaced, one after another as each
was fixed and the suite ran further:

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk. The retry loop
   also now captures combined stdout+stderr and prints it on every
   failed attempt rather than only the last, so the real error is
   visible immediately instead of after the full retry budget.

2. ServiceAccount automount policy regression: fixing (1) let the
   suite actually run, surfacing that cert-manager#8886 also changed
   restrict-sa-automount-sa-token from validating Pods
   (spec.automountServiceAccountToken) to validating ServiceAccounts
   themselves (object.automountServiceAccountToken) - a different
   resource and field, not an equivalent rewrite. This denies creation
   of any e2e-created ServiceAccount that doesn't explicitly set the
   field, breaking every RBAC-boundary, ACME HTTP01, and Vault issuer
   suite (161 specs, 100% traced to this one policy denial):

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by setting automountServiceAccountToken: false on the four
   ServiceAccounts the e2e suite creates for its own test scaffolding
   (RBAC-boundary helper, CertificateRequest approval/userinfo tests,
   and the Vault Kubernetes-auth bound ServiceAccount). This complies
   with the policy exactly as a real cluster operator would, rather
   than exempting the namespaces where cert-manager's own operational
   surface (e.g. ACME HTTP01 solver pods) is what the job exists to
   check.

3. Namespace default-ServiceAccount race: fixing (1) and (2) let the
   suite run further still, surfacing a pre-existing, unrelated race
   in the e2e framework itself: CreateKubeNamespace creates the test
   Namespace and returns immediately, without waiting for Kubernetes
   to finish asynchronously provisioning its "default" ServiceAccount.
   Any Pod created without an explicit ServiceAccount very soon after
   the Namespace - such as cert-manager's own ACME HTTP01 solver pods
   - can be admitted against that not-yet-created "default" SA and get
   rejected:

     pods "cm-acme-http-solver-" is forbidden: error looking up
     service account e2e-tests-.../default: serviceaccount "default"
     not found

   This race has presumably always existed, but Kyverno's additional
   admission-webhook overhead on every API request appears to widen it
   enough to hit consistently in this job (51 specs). Fixed by waiting
   for the "default" ServiceAccount to exist before CreateKubeNamespace
   returns, matching the polling idiom already used elsewhere in the
   e2e framework (wait.PollUntilContextTimeout).

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>
wallrj-cyberark added a commit to wallrj-cyberark/cert-manager that referenced this pull request Jul 22, 2026
…igration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in cert-manager#8886 bumped the chart
to 3.8.1. Three distinct problems surfaced, one after another as each
was fixed and the suite ran further:

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk. The retry loop
   also now captures combined stdout+stderr and prints it on every
   failed attempt rather than only the last, so the real error is
   visible immediately instead of after the full retry budget.

2. ServiceAccount automount policy regression: fixing (1) let the
   suite actually run, surfacing that cert-manager#8886 also changed
   restrict-sa-automount-sa-token from validating Pods
   (spec.automountServiceAccountToken) to validating ServiceAccounts
   themselves (object.automountServiceAccountToken) - a different
   resource and field, not an equivalent rewrite. This denies creation
   of any e2e-created ServiceAccount that doesn't explicitly set the
   field, breaking every RBAC-boundary, ACME HTTP01, and Vault issuer
   suite (161 specs, 100% traced to this one policy denial):

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by setting automountServiceAccountToken: false on the four
   ServiceAccounts the e2e suite creates for its own test scaffolding
   (RBAC-boundary helper, CertificateRequest approval/userinfo tests,
   and the Vault Kubernetes-auth bound ServiceAccount). This complies
   with the policy exactly as a real cluster operator would, rather
   than exempting the namespaces where cert-manager's own operational
   surface (e.g. ACME HTTP01 solver pods) is what the job exists to
   check.

3. Namespace default-ServiceAccount race: fixing (1) and (2) let the
   suite run further still, surfacing a pre-existing, unrelated race
   in the e2e framework itself: CreateKubeNamespace creates the test
   Namespace and returns immediately, without waiting for Kubernetes
   to finish asynchronously provisioning its "default" ServiceAccount.
   Any Pod created without an explicit ServiceAccount very soon after
   the Namespace - such as cert-manager's own ACME HTTP01 solver pods
   - can be admitted against that not-yet-created "default" SA and get
   rejected:

     pods "cm-acme-http-solver-" is forbidden: error looking up
     service account e2e-tests-.../default: serviceaccount "default"
     not found

   Comparing this job's last clean run before the migration
   (2026-07-08: 479 passed, 0 failed, 226s wall-clock) against a run
   with fixes (1) and (2) applied (428 passed, 51 failed, 1925s
   wall-clock - 8.5x slower) confirms this race never happened before
   Kyverno 3.8.1 was introduced, and points to general cluster-wide
   slowdown from CEL policy evaluation on every request (not the
   admission latency of the specific failing request, which happens
   after the built-in ServiceAccount check already rejected it) as the
   likely reason it surfaces now. Fixed by waiting for the "default"
   ServiceAccount to exist before CreateKubeNamespace returns, mirroring
   the equivalent, long-standing wait in Kubernetes' own e2e framework
   (WaitForDefaultServiceAccountInNamespace, called from CreateTestingNS)
   which exists for the same reason on any sufficiently slow cluster.

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>
wallrj-cyberark added a commit to wallrj-cyberark/cert-manager that referenced this pull request Jul 22, 2026
…igration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in cert-manager#8886 bumped the chart
to 3.8.1. Two distinct problems, fixed below.

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk. The retry loop
   also captures combined stdout+stderr and prints it on every failed
   attempt rather than only the last, so the real error is visible
   immediately instead of after the full retry budget.

2. ServiceAccount-targeting policy is a different policy, not a CEL
   rewrite of the original: fixing (1) let the suite actually run,
   surfacing that cert-manager#8886 also changed the effective automount policy
   from validating Pods (spec.automountServiceAccountToken) to
   validating ServiceAccounts themselves
   (object.automountServiceAccountToken). This is because the
   pre-migration policy, other/restrict-automount-sa-token, has no
   like-for-like CEL/ValidatingPolicy equivalent upstream - the only
   automount-related entry in kyverno's other-vpol catalog is
   restrict-sa-automount-sa-token, a differently-scoped policy that
   happens to have a similar name.

   Enforcing that policy as published denies Kubernetes' own creation
   of the "default" ServiceAccount for every namespace, since nothing
   sets automountServiceAccountToken on it. kube-controller-manager's
   serviceaccounts_controller retries forever and is denied every
   time (confirmed via its own logs: 4576 denials across 852 e2e
   namespaces in one run), so this is a permanent failure, not a
   race - any Pod anywhere that doesn't specify a custom
   ServiceAccount is unable to run. It reached 161 failing e2e specs
   here (RBAC-boundary, ACME HTTP01, Vault issuer suites - i.e. every
   suite that creates a Pod or ServiceAccount without setting the
   field), traced via 110 direct denial events in the apply log plus
   the default-ServiceAccount denials above:

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by patching the migrated ValidatingPolicy in kustomization.yaml
   to restore the original Pod-targeting semantics (resourceRules on
   pods, CEL expression on spec.automountServiceAccountToken) instead
   of the ServiceAccount-targeting resource kyverno currently
   publishes. This matches the policy already in effect on 2026-07-08
   before the migration, under which the entire e2e suite already
   passed cleanly (479/479, 0 failures) - cert-manager's own ACME
   HTTP01 solver pod already sets automountServiceAccountToken: false
   (pkg/issuer/acme/http/pod.go), so no further e2e or product code
   changes are needed. Also fixes a related, unrelated-looking symptom
   this surfaced: CreateKubeNamespace in the e2e framework created the
   test Namespace and returned immediately, without waiting for its
   "default" ServiceAccount to be provisioned - a latent race any Pod
   created immediately afterwards (e.g. an ACME HTTP01 solver pod)
   could hit. Kept as defence in depth, mirroring the equivalent,
   long-standing wait in Kubernetes' own e2e framework
   (WaitForDefaultServiceAccountInNamespace, called from
   CreateTestingNS), even though it no longer fires in practice once
   the ServiceAccount-targeting denial above is fixed.

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>
cert-manager-prow Bot pushed a commit that referenced this pull request Jul 23, 2026
…igration (#9041)

* fix(e2e): resolve bestpractice-install failures after Kyverno 3.8.1 migration

ci-cert-manager-master-e2e-v1-36-bestpractice-install has failed every
periodic run since 2026-07-09, immediately after the Kyverno
ClusterPolicy to ValidatingPolicy migration in #8886 bumped the chart
to 3.8.1. Two distinct problems, fixed below.

1. Webhook startup race: Kyverno registers its own admission webhooks
   from inside the running pod, after `helm upgrade --wait` already
   considers the Deployment ready. The very next step, `kubectl apply`
   of policy.yaml, lands in that startup window and fails with a
   webhook connection-refused error, aborting e2e-setup-kyverno before
   a single test runs:

     Error from server (InternalError): Internal error occurred: failed
     calling webhook "validate-policy.kyverno.svc": failed to call
     webhook: Post "https://kyverno-svc.kyverno.svc:443/policyvalidate
     ?timeout=10s": dial tcp 10.0.255.124:443: connect: connection
     refused

   Fixed by retrying the apply with a short backoff, mirroring the
   retry already used for tool downloads via curl's
   --retry-connrefused in make/_shared/tools/00_mod.mk. The retry loop
   also captures combined stdout+stderr and prints it on every failed
   attempt rather than only the last, so the real error is visible
   immediately instead of after the full retry budget.

2. ServiceAccount-targeting policy is a different policy, not a CEL
   rewrite of the original: fixing (1) let the suite actually run,
   surfacing that #8886 also changed the effective automount policy
   from validating Pods (spec.automountServiceAccountToken) to
   validating ServiceAccounts themselves
   (object.automountServiceAccountToken). This is because the
   pre-migration policy, other/restrict-automount-sa-token, has no
   like-for-like CEL/ValidatingPolicy equivalent upstream - the only
   automount-related entry in kyverno's other-vpol catalog is
   restrict-sa-automount-sa-token, a differently-scoped policy that
   happens to have a similar name.

   Enforcing that policy as published denies Kubernetes' own creation
   of the "default" ServiceAccount for every namespace, since nothing
   sets automountServiceAccountToken on it. kube-controller-manager's
   serviceaccounts_controller retries forever and is denied every
   time (confirmed via its own logs: 4576 denials across 852 e2e
   namespaces in one run), so this is a permanent failure, not a
   race - any Pod anywhere that doesn't specify a custom
   ServiceAccount is unable to run. It reached 161 failing e2e specs
   here (RBAC-boundary, ACME HTTP01, Vault issuer suites - i.e. every
   suite that creates a Pod or ServiceAccount without setting the
   field), traced via 110 direct denial events in the apply log plus
   the default-ServiceAccount denials above:

     error creating ServiceAccount for Kubernetes auth: admission
     webhook "vpol.validate.kyverno.svc-fail" denied the request:
     Policy restrict-sa-automount-sa-token failed: ServiceAccounts
     must set automountServiceAccountToken to false.

   Fixed by patching the migrated ValidatingPolicy in kustomization.yaml
   to restore the original Pod-targeting semantics (resourceRules on
   pods, CEL expression on spec.automountServiceAccountToken) instead
   of the ServiceAccount-targeting resource kyverno currently
   publishes. This matches the policy already in effect on 2026-07-08
   before the migration, under which the entire e2e suite already
   passed cleanly (479/479, 0 failures) - cert-manager's own ACME
   HTTP01 solver pod already sets automountServiceAccountToken: false
   (pkg/issuer/acme/http/pod.go), so no further e2e or product code
   changes are needed. Also fixes a related, unrelated-looking symptom
   this surfaced: CreateKubeNamespace in the e2e framework created the
   test Namespace and returned immediately, without waiting for its
   "default" ServiceAccount to be provisioned - a latent race any Pod
   created immediately afterwards (e.g. an ACME HTTP01 solver pod)
   could hit. Kept as defence in depth, mirroring the equivalent,
   long-standing wait in Kubernetes' own e2e framework
   (WaitForDefaultServiceAccountInNamespace, called from
   CreateTestingNS), even though it no longer fires in practice once
   the ServiceAccount-targeting denial above is fixed.

Fixes: https://testgrid.k8s.io/cert-manager-periodics-master#ci-cert-manager-master-e2e-v1-36-bestpractice-install
Signed-off-by: Richard Wall <[email protected]>

* adding changes to description and subject

Signed-off-by: Hemant Joshi <[email protected]>

---------

Signed-off-by: Richard Wall <[email protected]>
Signed-off-by: Hemant Joshi <[email protected]>
Co-authored-by: Hemant Joshi <[email protected]>
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/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. 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.

6 participants