1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 10:55:05 +00:00

fix mutate wildcard issue (#3193)

Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Vyankatesh Kudtarkar 2022-02-18 16:02:10 +05:30 committed by GitHub
parent 0a5aad39cf
commit 04e5f50cde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 107 additions and 3 deletions

View file

@ -150,9 +150,11 @@ func (ws *WebhookServer) applyMutation(request *v1beta1.AdmissionRequest, policy
return nil, nil, fmt.Errorf("failed to apply policy %s rules %v", policyContext.Policy.Name, engineResponse.GetFailedRules())
}
err := ws.openAPIController.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetAPIVersion(), engineResponse.PatchedResource.GetKind())
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to validate resource mutated by policy %s", policyContext.Policy.Name)
if engineResponse.PatchedResource.GetKind() != "*" {
err := ws.openAPIController.ValidateResource(*engineResponse.PatchedResource.DeepCopy(), engineResponse.PatchedResource.GetAPIVersion(), engineResponse.PatchedResource.GetKind())
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to validate resource mutated by policy %s", policyContext.Policy.Name)
}
}
return engineResponse, policyPatches, nil

View file

@ -0,0 +1,18 @@
name: wildcard-support-in-matchlabels
policies:
- policy.yaml
resources:
- resources.yaml
results:
- policy: mutate-wildcard
rule: mutate-wildcard
resource: wildcard-mutate
patchedResource: patchedResource.yaml
kind: Pod
result: pass
- policy: mutate-wildcard
rule: mutate-wildcard
resource: wildcard-mutate-fail
patchedResource: patchedResource1.yaml
kind: Pod
result: fail

View file

@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
test: app
name: wildcard-mutate
namespace: default
spec:
containers:
- image: nginx:latest
name: nginx

View file

@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: wildcard-mutate
namespace: default
spec:
containers:
- image: nginx:latest
name: nginx

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: mutate-wildcard
spec:
background: false
failurePolicy: Ignore
rules:
- name: mutate-wildcard
match:
all:
- resources:
kinds:
- "*"
mutate:
patchStrategicMerge:
metadata:
annotations:
test: "app"

View file

@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: wildcard-mutate
spec:
containers:
- name: nginx
image: nginx:latest
---
apiVersion: v1
kind: Pod
metadata:
name: wildcard-mutate-fail
spec:
containers:
- name: nginx
image: nginx:latest

19
test/policy.yaml Normal file
View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: who-created-this
spec:
background: false
failurePolicy: Ignore
rules:
- name: who-created-this
match:
all:
- resources:
kinds:
- "*"
mutate:
patchStrategicMerge:
metadata:
annotations:
test: "app"

8
test/resources.yaml Normal file
View file

@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: test-require-image-tag-pass
spec:
containers:
- name: nginx
image: nginx:latest