Generate rule on pod creation triggers twice #1035
-
Kyverno Version1.9 Kubernetes Version1.29 Kubernetes PlatformAKS DescriptionI've got the following Kyverno policy. apiVersion: kyverno.io/v1
kind: Policy
metadata:
name: create-user
namespace: monitoring-package
spec:
mutateExistingOnPolicyUpdate: false
rules:
- name: create-user
match:
any:
- resources:
kinds:
- Pod
selector:
matchLabels:
app.kubernetes.io/name: grafana
preconditions:
all:
- key: "{{request.operation}}"
operator: Equals
value: CREATE
generate:
apiVersion: batch/v1
kind: Job
name: "create-user-{{request.object.metadata.resourceVersion}}"
namespace: monitoring-package
data:
kind: Job
spec:
template:
... The policy, in itself, works. It creates a Job that creates a user in Grafana when Grafana restarts. However, when I delete Grafana pod and a new one gets created, the policy triggers twice and two jobs are generated. In the Kyverno logs, it appears as follows:
So indeed we have two URs, with two resource versions. I would guess somehow there are two CREATE requests for the same pod because of that. Is there something that I can add to the precondition in order to prevent that? Steps to reproduce
spec:
rules:
- name: generate-on-pod-creation
match:
all:
- resources:
kinds:
- Pod
selector:
matchLabels:
foo: bar
Expected behaviorThe generate rule should only trigger once instead of twice. ScreenshotsNo response Kyverno logsNo response Slack discussionNo response Troubleshooting
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
You could use a context variable to do an API call, but I'm not 100% certain that works all the time in the 1.9 version. Moving to discussion. |
Beta Was this translation helpful? Give feedback.
https://kyverno.io/docs/writing-policies/external-data-sources/#variables-from-kubernetes-api-server-calls