kyverno: [Bug] [CLI] Negative tests for generate policy with exclusions fail
Kyverno CLI Version
1.11.0
Description
I have a generate policy that is based on sync-secrets, but I’ve extended the match to include the presence of a specific label. To verify the proper function of the label matching I included a negative test case, i.e. a result: skip on a Namespace without the label. In v1.10.x this test worked, but now in v1.11.0 it fails.
Steps to reproduce
sync-secret.yaml
# Based on: https://github.com/kyverno/policies/tree/main/other/s-z/sync-secrets
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: sync-secret
annotations:
policies.kyverno.io/title: Sync Secret
policies.kyverno.io/category: Sample
policies.kyverno.io/subject: Namespace
policies.kyverno.io/minversion: 1.6.0
policies.kyverno.io/description: >-
Synchronizes a secret from the default namespace
to every created namespace that has a matching label.
spec:
generateExisting: true
rules:
- name: sync-my-secret
match:
any:
- resources:
kinds:
- Namespace
selector:
matchLabels:
sync-the-secret: "true"
generate:
apiVersion: v1
kind: Secret
name: my-secret
namespace: "{{request.object.metadata.name}}"
synchronize: true
clone:
namespace: default
name: my-secret
resources.yaml
apiVersion: v1
kind: Namespace
metadata:
name: hello-world-namespace
labels:
sync-the-secret: "true"
---
apiVersion: v1
kind: Namespace
metadata:
name: foo-bar-namespace
cloneSourceResource.yaml
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: default
type: Opaque
data:
api-key: 5eb63bbbe01eeed093cb22bb8f5acdc3
generatedResource.yaml
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: hello-world-namespace
type: Opaque
data:
api-key: 5eb63bbbe01eeed093cb22bb8f5acdc3
kyverno-test.yaml
name: sync-secret
policies:
- sync-secret.yaml
resources:
- resources.yaml
results:
# Expected PASS since label is matched
- policy: sync-secret
rule: sync-my-secret
resource: hello-world-namespace
generatedResource: generatedResource.yaml
cloneSourceResource: cloneSourceResource.yaml
kind: Namespace
result: pass
# Expected SKIP since missing label on Namespace
- policy: sync-secret
rule: sync-my-secret
resource: foo-bar-namespace
generatedResource: empty.yaml # blank file
cloneSourceResource: empty.yaml
kind: Namespace
result: skip
Result
│────│─────────────│────────────────│─────────────────────────────────│────────│───────────│
│ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON │
│────│─────────────│────────────────│─────────────────────────────────│────────│───────────│
│ 1 │ sync-secret │ sync-my-secret │ Namespace/hello-world-namespace │ Pass │ Ok │
│ 2 │ sync-secret │ sync-my-secret │ Namespace/foo-bar-namespace │ Fail │ Not found │
│────│─────────────│────────────────│─────────────────────────────────│────────│───────────│
Test Summary: 1 tests passed and 1 tests failed
Expected behavior
Executing sync-secret...
applying 1 policy to 2 resources...
│───│─────────────│────────────────│──────────────────────────────────│────────│
│ # │ POLICY │ RULE │ RESOURCE │ RESULT │
│───│─────────────│────────────────│──────────────────────────────────│────────│
│ 1 │ sync-secret │ sync-my-secret │ /Namespace/hello-world-namespace │ Pass │
│ 2 │ sync-secret │ sync-my-secret │ /Namespace/foo-bar-namespace │ Pass │
│───│─────────────│────────────────│──────────────────────────────────│────────│
Test Summary: 2 tests passed and 0 tests failed
Screenshots
No response
Kyverno logs
I1117 11:20:40.159005 28103 vars.go:375] "msg"="variable substituted" "path"="/generate/namespace" "value"=null "variable"="{{request.object.metadata.name}}"
I1117 11:20:40.159575 28103 vars.go:375] kubectl-kyverno "msg"="variable substituted" "path"="/generate/namespace" "value"="hello-world-namespace" "variable"="{{request.object.metadata.name}}"
I1117 11:20:40.159648 28103 generate.go:409] kubectl-kyverno "msg"="applying generate rule" "mode"="CREATE" "target"="v1/Secret/hello-world-namespace/my-secret"
I1117 11:20:40.159696 28103 generate.go:437] kubectl-kyverno "msg"="created generate target resource" "target"="v1/Secret/hello-world-namespace/my-secret"
I1117 11:20:40.159836 28103 background.go:83] engine.background "msg"="rule not matched" "new.kind"="Namespace" "new.name"="foo-bar-namespace" "new.namespace"="default" "policy.apply"="All" "policy.name"="sync-secret" "policy.namespace"="" "reason"="resource is empty" "rule.name"="sync-my-secret"
Slack discussion
No response
Troubleshooting
- I have read and followed the troubleshooting guide.
- I have searched other issues in this repository and mine is not recorded.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 18 (17 by maintainers)
Any news please @realshuting @eddycharly? This is blocking our migration from v1.9. Thank you I feel like this should be patched on v1.11
Match/Exclusions don’t generate a skip anymore. I’m working on providing a similar mechanism.
We will fix it in 1.11.2.
This prevents me from migrating to v1.11.0. Any chance this gets addressed in a patch release or should I wait for v1.12?