mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 02:18:15 +00:00
fix: namespace label matching for Namespace (#7837)
* Feat: namespaceLabel matching for ns Signed-off-by: ShutingZhao <shuting@nirmata.com> * Fix: update kuttl tests Signed-off-by: ShutingZhao <shuting@nirmata.com> --------- Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
parent
5a8caaf4fe
commit
537612b609
7 changed files with 40 additions and 9 deletions
|
@ -115,14 +115,17 @@ func doesResourceMatchConditionBlock(
|
|||
}
|
||||
}
|
||||
|
||||
if conditionBlock.NamespaceSelector != nil && resource.GetKind() != "Namespace" &&
|
||||
(resource.GetKind() != "" || slices.Contains(conditionBlock.Kinds, "*") && wildcard.Match("*", resource.GetKind())) {
|
||||
hasPassed, err := matchutils.CheckSelector(conditionBlock.NamespaceSelector, namespaceLabels)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to parse namespace selector: %v", err))
|
||||
} else {
|
||||
if !hasPassed {
|
||||
errs = append(errs, fmt.Errorf("namespace selector does not match labels"))
|
||||
if conditionBlock.NamespaceSelector != nil {
|
||||
if resource.GetKind() == "Namespace" {
|
||||
errs = append(errs, fmt.Errorf("namespace selector is not applicable for namespace resource"))
|
||||
} else if resource.GetKind() != "" || slices.Contains(conditionBlock.Kinds, "*") && wildcard.Match("*", resource.GetKind()) {
|
||||
hasPassed, err := matchutils.CheckSelector(conditionBlock.NamespaceSelector, namespaceLabels)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to parse namespace selector: %v", err))
|
||||
} else {
|
||||
if !hasPassed {
|
||||
errs = append(errs, fmt.Errorf("namespace selector does not match labels"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: pod-reader-fake
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- pods
|
||||
verbs:
|
||||
- get
|
||||
- list
|
||||
- watch
|
|
@ -0,0 +1,7 @@
|
|||
# Specifying the kind as `TestStep` performs certain behaviors like this delete operation.
|
||||
apiVersion: kuttl.dev/v1beta1
|
||||
kind: TestStep
|
||||
delete:
|
||||
- apiVersion: v1
|
||||
kind: Namespace
|
||||
name: test-wildcard
|
|
@ -0,0 +1,7 @@
|
|||
# Specifying the kind as `TestStep` performs certain behaviors like this delete operation.
|
||||
apiVersion: kuttl.dev/v1beta1
|
||||
kind: TestStep
|
||||
delete:
|
||||
- apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRole
|
||||
name: pod-reader-fake
|
|
@ -9,4 +9,5 @@ The pod `test-validate/nginx-block` is blocked, and the pod `default/nginx-pass`
|
|||
|
||||
## Reference Issue(s)
|
||||
|
||||
https://github.com/kyverno/kyverno/issues/6015
|
||||
https://github.com/kyverno/kyverno/issues/6015
|
||||
https://github.com/kyverno/kyverno/issues/7771
|
Loading…
Add table
Reference in a new issue