1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

fix ns labels matching (#6022)

Signed-off-by: ShutingZhao <shuting@nirmata.com>

Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
shuting 2023-01-18 20:46:51 +08:00 committed by GitHub
parent 2b84a93468
commit 2506faf91a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 93 additions and 1 deletions

View file

@ -169,7 +169,8 @@ func doesResourceMatchConditionBlock(subresourceGVKToAPIResource map[string]*met
}
}
if conditionBlock.NamespaceSelector != nil && resource.GetKind() != "Namespace" && resource.GetKind() != "" {
if conditionBlock.NamespaceSelector != nil && resource.GetKind() != "Namespace" &&
(resource.GetKind() != "" || slices.Contains(conditionBlock.Kinds, "*") && wildcard.Match("*", resource.GetKind())) {
hasPassed, err := checkSelector(conditionBlock.NamespaceSelector, namespaceLabels)
if err != nil {
errs = append(errs, fmt.Errorf("failed to parse namespace selector: %v", err))

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- ns.yaml
assert:
- ns.yaml

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,7 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: pod-fail.yaml
shouldFail: true
- file: pod-pass.yaml
shouldFail: false

View file

@ -0,0 +1,12 @@
# ## Description
This test validates that the namespaceSelector is applied to a wildcard policy successfully.
## Expected Behavior
The pod `test-validate/nginx-block` is blocked, and the pod `default/nginx-pass` is created.
## Reference Issue(s)
https://github.com/kyverno/kyverno/issues/6015

View file

@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
name: test-wildcard
labels:
freeze: "true"

View file

@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
labels:
app: nginx
name: nginx-block
namespace: test-wildcard
spec:
containers:
- image: nginx
name: test

View file

@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
labels:
app: nginx
name: nginx-pass
namespace: default
spec:
containers:
- image: nginx
name: test

View file

@ -0,0 +1,9 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: freeze-policy
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,23 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: freeze-policy
spec:
validationFailureAction: Enforce
background: false
rules:
- name: freeze-rule
match:
any:
- resources:
kinds:
- "*"
namespaceSelector:
matchExpressions:
- key: freeze
operator: In
values:
- "true"
validate:
message: "Namespace is frozen."
deny: {}