kyverno: [BUG] Kyverno updates ClusterPolicy needlessly when auto-generation is enabled
Software Environment
- Kubernetes version: 1.19.9
- Kubernetes platform: kOps in AWS EC2
- Kyverno version: 1.5.0
Description
For a ClusterPolicy that lacks a “pod-policies.kyverno.io/autogen-controllers” annotation with value “none”—meaning that the annotation isn’t present in my Kubernetes manifest at all—Kyverno repeatedly updates the ClusterPolicy object, incrementing the generation and changing the resource version, presumably due to writing to the “spec.rules” field.
Reproduction
Steps to reproduce the behavior:
- Create a ClusterPolicy that never mentions any of the following API kinds: Pod, DaemonSet, Deployment, Job, StatefulSet, or CronJob. In my case, it only matches a custom resource.
- Omit the “pod-policies.kyverno.io/autogen-controllers” annotation from the Kubernetes manifest used to create the object.
- Apply the manifest and confirm that the ClusterPolicy object exists.
Use a reconciling agent like Flux that will fight back in an “edit war.” - Watch the object for changes.
- Observe that each time Flux detects that the object has changed, Flux patches the object, then Kyverno updates the object again immediately afterward, such that the generation increases by two.
- Inspect the managed fields on the object.
Note that Kyverno “owns” the “spec.rules” field.
managedFields:
- apiVersion: kyverno.io/v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:labels:
f:bisontrails.co/owner: {}
f:kustomize.toolkit.fluxcd.io/name: {}
f:kustomize.toolkit.fluxcd.io/namespace: {}
f:spec:
f:background: {}
manager: kustomize-controller
operation: Apply
time: "2021-11-10T16:00:06Z"
- apiVersion: kyverno.io/v1
fieldsType: FieldsV1
fieldsV1:
f:spec:
f:rules: {}
f:status:
.: {}
f:ready: {}
manager: kyverno
operation: Update
time: "2021-11-10T16:00:06Z"
- Add the “pod-policies.kyverno.io/autogen-controllers” annotation to the object with value “none.”
Include this annotation in the manifest that Flux applies. - Observe that Kyverno stops updating the object, and therefore so does Flux.
Expected Behavior
Kyverno should not write to the “spec.rules” field of a ClusterPolicy that doesn’t involve any of the API kinds covered by automatic rule generation. Even with that feature enabled for this object, Kyverno should not write any changes to the “spec.rules” field, as there’s nothing there that needs to change.
The user should not need to disable the automatic rule generation feature just to get Kyverno to stop updating the object needlessly.
Despite trying many times, I have not been able to catch the ClusterPolicy in the state written by Flux for comparison against the state written by Kyverno, so I can’t tell how they differ, if they even do differ. Flux uses SSA for its patching, so it may be that it notices that the managed fields no longer claim that it’s the owner of the “spec.rules” field, and it’s reacting to that loss.
Additional Context
I brought up this problem in two Slack discussions:
- “kyverno” channel of the “Kubernetes” Slack workspace
- “flux” channel of the CNCF Slack workspace
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 25 (15 by maintainers)
I made a proposal for revisiting the autogen rules implementation Kyverno Design Proposal - Internal Auto-Gen.
It’s publicly accessible so don’t hesitate to share your feedback.
Kyverno should not have permission to update or patch the “clusterpolicy” and “policy” resources. It should write only to a “clusterpolicy/status” and a “policy/status” subresource.
If we’d like these generated rules in a proper object, we should introduce new resources like “generatedclusterpolicy” and “generatedpolicy” that Kyverno “owns.”
A proper solution would be for the controller to generate the rules specified by the autogen annotation internally when running the policy. In my opinion, it’s bad practice for a controller to write back to the spec of a resource.
@ojhaarjun1 This is still just a workaround rather than resolving the root cause, autogen shouldn’t ever add anything to spec.