kyverno: [Bug] Background mutation breaks validation on Create
Kyverno Version
1.13.4
Kubernetes Version
1.32.x
Kubernetes Platform
Other (specify in description)
Kyverno Rule Type
Mutate
Description
There seems to be a but in the mutation logic, when background mutation is enabled.
We have a mutation - with background mutation enabled - adding a label when it does not exist to e.g. an Deployment. We also have a validation which checks this label against a whitelist.
- When background mutation is not enabled (no
target/mutateExistingOnPolicyUpdatepresent in the mutation rule), everything works fine. The label is added and the validation succeeds. - When background mutation is enabled (see code below), then the label is NOT present during the validation message:
validate-label:
validate-label: |
null not in msggroup whitelist: ["label1","label2","atestvalue"].
The mutation is actually working, when no validation is deployed.
It seems that mutation with enabled background mutations, are NOT mutated in the AdmissionReview process before the validation.
(This behavior is observed on K8s 1.32 on Tanzu and EKS).
Steps to reproduce
Example muation:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: enforce-label
spec:
rules:
- match:
any:
- resources:
kinds:
- Deployment
namespaces:
- default
mutate:
mutateExistingOnPolicyUpdate: true
patchStrategicMerge:
metadata:
labels:
+(atestlabel): atestvalue
targets:
- apiVersion: apps/v1
kind: Deployment
namespace: default
name: enforce-label
Example validation:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: enforce-label
spec:
rules:
- match:
any:
- resources:
kinds:
- Deployment
namespaces:
- default
mutate:
mutateExistingOnPolicyUpdate: true
patchStrategicMerge:
metadata:
labels:
+(atestlabel): atestvalue
targets:
- apiVersion: apps/v1
kind: Deployment
namespace: default
name: enforce-label
Example deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
Expected behavior
The mutation should add the label on resource creation also if background muations are enabled.
Screenshots
No response
Kyverno logs
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: open
- Created 2 years ago
- Reactions: 14
- Comments: 15 (8 by maintainers)
Is this really the the expected behavior? As I understand the docs and the logic of the configuration, I expect a mutation to work in both cases at Admission-time and in the Background. Why would this be decoupled? Is this designed on-purpose or is the suggested solution just a workaround?
In case this is works-as-designed, this should be made clearer in the docs and also in the parameter setup. For Background only mutation, the match field also is more or less superfluous - I’d think.
@Mohdcode but this is not the expected behavior, I guess. I understand it as “on-top” - so next to the normal admission handling it also does background handling.