kyverno: [Bug] ensure webhooks are removed on helm uninstall (PROPOSAL)

Kyverno Version

1.11.4

Description

Background

Kyverno often has very privileged validation webhooks. Depending on the ClusterPolicies the user has applied, they can impact literally all resource actions on a cluster (outside of the excluded namespaces).

By default, the webhooks have a failurePolicy of Fail, this means that when Kyverno is down, in many cases the entire cluster grinds to a halt.

Under normal operation, this is usually the expected behaviour (as Kyverno is a validation system), and when it’s not, we provide the --forceFailurePolicyIgnore controller flag which makes the webhooks failurePolicy become Ignore (fail open).

Most users are installing Kyverno with the Kyverno Helm Chart.

The Problem

We need to GUARANTE that the webhooks are deleted when helm uninstall is run (or when the Argo CD app is deleted) BEFORE taking the webhook backend down.

Currently, the Kyverno helm chart uses a pre-delete hook Job to clean up the ValidatingWebhookConfiguration and MutatingWebhookConfiguration resources:

https://github.com/kyverno/kyverno/blob/6c88cf5ee12dcf0c3ecfc29b893864216a05b828/charts/kyverno/templates/hooks/pre-delete.yaml#L35-L38

Why the hook does NOT work?

There are two very significant problems with the current pre-delete hook:

  1. ArgoCD don’t support pre-delete hooks:
    • This means that 100% of the time, when uninstalling Kyverno that was installed with ArgoCD, the webhooks are left in the cluster, which results in potentially blocking all cluster requests unless --forceFailurePolicyIgnore was used.
    • Note, there is an unreleased feature in ArgoCD for post-delete hook support, but NOT pre-delete (https://github.com/argoproj/argo-cd/pull/16595)
  2. There is a race condition in the current Job code:
    • By first scaling the deployments to 0, and then waiting 30 seconds before removing the webhooks, there is a race condition where the controller might actually re-create the webhooks if the scale takes more than 30 seconds.

What have we already tried?

As a “solution” to the original issue https://github.com/kyverno/kyverno/issues/8390, we introduced a configs.webhooksLabels value for Kyverno 1.12 in PR https://github.com/kyverno/kyverno/pull/9015. The idea was to set the app.kubernetes.io/instance label using configs.webhooksLabels so that Helm/ArgoCD sees itself as the owner of the webhooks.

However, this does NOT fully solve the problem:

  1. It requires users to set the new config label correctly:
    • It’s not always clear what value the label should have.
    • This is not how Helm tracks resources, so only helps ArgoCD users.
  2. It won’t work for users who use the ArgoCD “annotation tracking” mode:
    • Note, we can’t use configs.webhooksAnnotations because the annotation tracking mode uses values like my-app:apps/Deployment:default/nginx-deployment (which include the resource name/type, so would need to be set per-webhook).

Solution 1: Include webhook resources in the Helm chart

Currently, the webhook resources YAML are NOT actually in the helm chart. They are created/managed by the Kyverno controllers. If we include them in the helm chart, they will automatically get whatever tracking system ArgoCD/Helm needs, so they would be removed on helm uninstall.

For example, we might include an “empty” webhook config for each one:

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  name: kyverno-resource-mutating-webhook-cfg
# for example, including them with no `webhooks` set
webhooks: []

Considerations for this solution:

  • The Kyverno controllers need to be updated to not remove any labels/annotations that already exist on the webhooks when reconciling them.
  • Each helm upgrade / argocd sync will very briefly revert the webhook to its “empty” state, until the controller re-reconciles the configs.

Solution 2: Set metadata.ownerReferences to a Kyverno cluster resource

This will not work, because ownerReference only affects cascading deletes, so would not guarantee the removal.

We could have the Kyverno controller set the metadata.ownerReferences to one of the cluster-scoped resources in the chart (e.g. a ClusterRole used by Kyverno). This will guarantee that when that ClusterRole is deleted, the Kubernetes cluster will automatically delete the webhooks.

Considerations for this solution:

  • ArgoCD / Helm will not know that the webhooks are part of Kyverno, so won’t show them to the user in the UI.
  • We would need to carefully decide which cluster-scoped resource should be the “owner” of the webhooks (because Kubernetes does not let namespaced-scoped resources be the owner of webhooks, which are themselves cluster-scoped).

Conclusion

My preference is to try and find a way to get “solution 1” working, because it will allow users to “see” the webhook configs in their ArgoCD UI (otherwise they might not even know they are there).

This is also how most other webhook-using apps work (e.g. cert-manager, istio). However, those other apps don’t have “dynamic” webhook configurations, so they are not forced to include an empty webhooks spec (and therefore don’t have the webhook disabled for a split second during each helm upgrade).

But either way, we MUST find a solution to this problem, because it’s not acceptable for users to deadlock all requests on their cluster when uninstalling Kyverno.

Related Issues

Slack discussion

No response

Troubleshooting

  • I have read and followed the documentation AND the troubleshooting guide.
  • I have searched other issues in this repository and mine is not recorded.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 22 (15 by maintainers)

Most upvoted comments

@thesuperzapper - thanks for raising the issue and I agree we should properly remove managed webhooks. Added to the next maintainers meeting:

https://docs.google.com/document/d/1I_GWsz32gLw8sQyuu_Wv0-WQrtRLjn9FuX2KGNkvUY4/edit

Hi, same issue observed in helm-controller managed helm releases, when kyverno got deleted, its dynamically generated webhooks didn’t get removed properly which blocks other helm release uninstallation process. @realshuting Any update on this issue?

@vishal-chdhry I think you are misunderstanding what the problem is here.

We already have a (fragile, but ok) solution for uninstalling the webhooks with native helm, in the form of a helm.sh/hook: pre-delete hook which deletes the ValidatingWebhookConfiguration and MutatingWebhookConfiguration resources on helm uninstall.

The problem is that ArgoCD (which is very, very popular) does not support pre-delete hooks. This means that 100% of the time, the cluster will be deadlocked when uninstalling Kyverno since the webhooks are not part of the chart, so will be left when ArgoCD uninstalls.

There is also a slight race condition on helm uninstall where if the scale down takes longer than 30 seconds, the webhooks might be recreated by the controller (because the job does not actually wait for the scale down to succeed).

Therefore, my proposal is as follows:

  1. Include the ValidatingWebhookConfiguration and MutatingWebhookConfiguration resources in the chart itself:
    • With empty webhooks[].rules[] because the Kyverno controller will populate them depending on the policies.
    • The failurePolicy can actually be anything (see 3 for more information)
  2. Make the Kyverno controller never CREATE the webhooks, only UPDATE them:
    • This is because the responsibility to create the webhooks now lies with Helm (or kubectl with raw manifests).
    • This is to guarantee there is no race condition on uninstall/install.
    • The controller should NOT crash-loop if the webhook’s don’t exist (so we can create the webhooks after the backend pod are up), but should show warnings.
  3. Tell ArgoCD users to annotate the webhooks with argocd.argoproj.io/sync-wave: "100":
    • This will ensure the webhooks are applied AFTER their backends on install, and are removed BEFORE their backends on uninstall.
    • We can add a common helm chart value which adds annotations to all webhooks to make this easier.

If I am misunderstanding your solution, or you have a better idea, please share.

@realshuting @aslafy-z @eddycharly @chipzoller @JimBugwadia I think this is a critical issue that needs to be addressed in Kyverno 1.12, or sooner.

I am interested to hear your feedback.