kyverno: [Bug] background controller update request loop when generating a resource that has list keys and synchronize enabled
Kyverno Version
1.11.3
Kubernetes Version
1.25.x
Kubernetes Platform
GKE
Kyverno Rule Type
Generate
Description
Hey folks 👋🏼
Bit a strange one that started after upgrade to 1.11.3, but were finally able to narrow it down.
We have a data-type generate policy with 2 rules that triggers on a v1/ServiceAccount, it generates two namespaced custom resources:
generate:
kind: Policy
apiVersion: vault.fastly.com/v1beta1
generate:
kind: KubernetesRole
apiVersion: vault.fastly.com/v1beta1
These resources are part of an internal vault controller we developed.
Once triggered, we can see 2 URs fired for each rule and the downstream resource are correctly created, however immediately after we see a non-stop loop of URs getting created, switching to Pending, then Complete and deleted.
combing through the background controller logs we found this:
kyverno-background-controller-7d76896c4-fp544 controller E0115 21:09:00.693137 1 controller.go:223] "failed to get policy" err="policy.kyverno.io \"k8s-my-app-my-app\" not found" logger="EventGenerator" name="k8s-my-app-my-app"
kyverno-background-controller-7d76896c4-fp544 controller I0115 21:09:00.693150 1 controller.go:179] "retrying event generation" logger="EventGenerator" key={"Kind":"Policy","Name":"k8s-my-app-my-app","Namespace":"my-app","RelatedAPIVersion":"","RelatedKind":"","RelatedName":"","RelatedNamespace":"","Reason":"PolicyApplied","Message":"Created Policy k8s-my-app-my-app as a result of applying policy generate-sa-vault-policy-and-k8srole/vault-policy","Action":"None","Source":"kyverno-generate"} reason="policy.kyverno.io \"k8s-my-app-my-app\" not found"
It appears to me that there’s a conflict with Kyverno’s Policy resource and our custom Policy (apiVersion: vault.fastly.com/v1beta1)
I tried omitting the PolicyApplied events, but it didn’t help. It seems that internally there’s a non-deterministic lookup for the generated resource which relies only on the resource kind?
remove the generate rule that creates kind: Policy resource “fixes” the issue, though obviously not acceptable workaround.
Here’s the cluster policy in question:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: generate-sa-vault-policy-and-k8srole
spec:
background: false
validationFailureAction: Audit
generateExisting: true
rules:
- name: vault-policy
match:
all:
- resources:
kinds:
- v1/ServiceAccount
selector:
matchLabels:
gen-vault-policy-k8srole: "true"
namespaceSelector:
matchExpressions:
- key: namespaceKind
operator: In
values:
- workloads
preconditions:
all:
- key: "{{ saname }}"
operator: NotEquals
value: default
context:
- name: saname
variable:
jmesPath: request.object.metadata.name
- name: sauid
variable:
jmesPath: request.object.metadata.uid
- name: sanamespace
variable:
jmesPath: request.object.metadata.namespace
generate:
kind: Policy
apiVersion: vault.fastly.com/v1beta1
name: "k8s-{{ sanamespace }}-{{ saname }}"
namespace: "{{ sanamespace }}"
synchronize: true
data:
metadata:
ownerReferences:
- apiVersion: v1
kind: ServiceAccount
name: "{{ saname }}"
uid: "{{ sauid }}"
spec:
rules:
- path: "secret/{{ sanamespace }}/shared/*"
capabilities: [read, list]
- path: "secret/{{ sanamespace }}/shared"
capabilities: [read, list]
- path: "secret/{{ sanamespace }}/{{ saname }}/*"
capabilities: [read, list]
- path: "secret/{{ sanamespace }}/{{ saname }}"
capabilities: [read, list]
- path: "{{ sanamespace }}/data/shared/*"
capabilities: [read, list]
- path: "{{ sanamespace }}/data/shared"
capabilities: [read, list]
- path: "{{ sanamespace }}/data/{{ saname }}/*"
capabilities: [read, list]
- path: "{{ sanamespace }}/data/{{ saname }}"
capabilities: [read, list]
- name: vault-k8srole
match:
all:
- resources:
kinds:
- v1/ServiceAccount
selector:
matchLabels:
gen-vault-policy-k8srole: "true"
namespaceSelector:
matchExpressions:
- key: namespaceKind
operator: In
values:
- workloads
preconditions:
all:
- key: "{{ saname }}"
operator: NotEquals
value: default
context:
- name: saname
variable:
jmesPath: request.object.metadata.name
- name: sauid
variable:
jmesPath: request.object.metadata.uid
- name: sanamespace
variable:
jmesPath: request.object.metadata.namespace
generate:
kind: KubernetesRole
apiVersion: vault.fastly.com/v1beta1
name: "k8s-{{ sanamespace }}-{{ saname }}"
namespace: "{{ sanamespace }}"
synchronize: true
data:
metadata:
ownerReferences:
- apiVersion: v1
kind: ServiceAccount
name: "{{ saname }}"
uid: "{{ sauid }}"
spec:
boundServiceAccountNames: ["{{ saname }}"]
boundServiceAccountNamespaces: ["{{ sanamespace }}"]
policies: ["k8s-{{ sanamespace }}-{{ saname }}"]
Policy CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
name: policies.vault.fastly.com
spec:
group: vault.fastly.com
names:
kind: Policy
listKind: PolicyList
plural: policies
singular: policy
scope: Namespaced
versions:
- name: v1beta1
schema:
openAPIV3Schema:
description: Policy is the Schema for the policies API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: PolicySpec defines the desired state of Policy
properties:
rules:
items:
properties:
allowed_parameters:
additionalProperties:
items:
type: string
type: array
type: object
capabilities:
items:
enum:
- read
- list
- create
- update
- delete
- sudo
type: string
maxItems: 6
minItems: 1
type: array
max_wrapping_ttl:
type: string
min_wrapping_ttl:
type: string
path:
minLength: 1
type: string
required_parameters:
items:
type: string
type: array
required:
- capabilities
- path
type: object
minItems: 1
type: array
required:
- rules
type: object
status:
description: PolicyStatus defines the observed state of Policy
type: object
type: object
served: true
storage: true
Steps to reproduce
- Need a custom resource with Kind=Policy
- deploy the cluster policy
- create a service account to match the policy
- observe the background controller logs
Expected behavior
Kyverno should be able to generate a kind=Policy resource
Screenshots
No response
Kyverno logs
kyverno-background-controller-7d76896c4-fp544 controller E0115 21:09:00.693137 1 controller.go:223] "failed to get policy" err="policy.kyverno.io \"k8s-my-app-my-app\" not found" logger="EventGenerator" name="k8s-my-app-my-app"
kyverno-background-controller-7d76896c4-fp544 controller I0115 21:09:00.693150 1 controller.go:179] "retrying event generation" logger="EventGenerator" key={"Kind":"Policy","Name":"k8s-my-app-my-app","Namespace":"my-app","RelatedAPIVersion":"","RelatedKind":"","RelatedName":"","RelatedNamespace":"","Reason":"PolicyApplied","Message":"Created Policy k8s-my-app-my-app as a result of applying policy generate-sa-vault-policy-and-k8srole/vault-policy","Action":"None","Source":"kyverno-generate"} reason="policy.kyverno.io \"k8s-my-app-my-app\" not found"
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
- Comments: 30 (27 by maintainers)
Also relates to request #7524
Hey @realshuting, I’m fairly confident at this point to say that with Kyverno 1.12.5 &
userServerSideApplythis issue has been resolved.going to close this now and thanks all for helping with the resolution!
Yes. A few critical issues were found for 1.12.0 and we were working on hot fixes.
I will revisit this in 1.12.2/1.13.0 timeframe.
@jlcrow, this doesn’t seem like the same thing as what’s described in this issue as you’re cloning something and a Secret resource does not have list elements. Can you open a discussion and link that number back here? We can discuss there and determine where the appropriate place would be if it is a qualified bug.
@dkulchinsky - thanks for the detailed test results.
One potential cause I suspect for UR loops (test scenario 2 above) is that the
policy.spec.rules(apiVersion: vault.fastly.com/v1beta1) takes a list, and the ordering matters ifsync=true. Kyverno will try to “revert” changes (list items are the same but in a different order) with synchronization on.I will try to dig into this.
folks, did a few more tests with different combinations:
synchronize: false,generateExisting: true, withoutoperations: ['CREATE', 'UPDATE']:🟢 No UR loops 🟢 Generate on admission works as expected 🟢 generate on existing resources works 🟠 synchronization disabled
synchronize: true,generateExisting: true, withoutoperations: ['CREATE', 'UPDATE']:🔴 UR loops 🟢 Generate on admission works as expected 🟢 generate on existing resources works 🟢 synchronization enabled
synchronize: true,generateExisting: true, withoperations: ['CREATE', 'UPDATE']:🟢 No UR loops 🟢 Generate on admission works as expected 🔴 generate on existing resources didn’t work, @chipzoller it appears that when I add the
operations: ['CREATE', 'UPDATE']to the resources match,generateExistingstops working. 🟢 synchronization enabledsynchronize: true,generateExisting: false, withoutoperations: ['CREATE', 'UPDATE']:🔴 UR loops 🔴 Generate on admission works as expected, but triggers a UR loop 🟠 generate on existing resources disabled 🟢 synchronization enabled
synchronize: true,generateExisting: false, withoperations: ['CREATE', 'UPDATE']:🟢 No UR loops 🟢 Generate on admission works as expected 🟠 generate on existing resources disabled 🟢 synchronization enabled
For now, combination (1) is the best workaround.
side note, I’m still seeing these errors even when there’s no UR loop: