Skip to content

Commit b9db3fc

Browse files
authored
Merge pull request #40200 from cici37/mc
KEP-3488 ValidatingAdmissionPolicy: MatchConditions
2 parents f48a415 + d9a2c85 commit b9db3fc

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

content/en/docs/reference/access-authn-authz/validating-admission-policy.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ For example, `int` in the word “sprint” would not be escaped.
323323

324324
Examples on escaping:
325325

326-
|property name | rule with escaped property name |
327-
| ----------------| ----------------------- |
328-
| namespace | `self.__namespace__ > 0` |
329-
| x-prop | `self.x__dash__prop > 0` |
330-
| redact__d | `self.redact__underscores__d > 0` |
331-
| string | `self.startsWith('kube')` |
326+
|property name | rule with escaped property name |
327+
| ----------------|-----------------------------------|
328+
| namespace | `object.__namespace__ > 0` |
329+
| x-prop | `object.x__dash__prop > 0` |
330+
| redact__d | `object.redact__underscores__d > 0` |
331+
| string | `object.startsWith('kube')` |
332332
333333
Equality on arrays with list type of 'set' or 'map' ignores element order, i.e. [1, 2] == [2, 1].
334334
Concatenation on arrays with x-kubernetes-list-type use the semantics of the list type:
@@ -365,3 +365,24 @@ HTTP response code, are used in the HTTP response to the client.
365365
The currently supported reasons are: `Unauthorized`, `Forbidden`, `Invalid`, `RequestEntityTooLarge`.
366366
If not set, `StatusReasonInvalid` is used in the response to the client.
367367

368+
### Matching requests: `matchConditions`
369+
370+
You can define _match conditions_ for a `ValidatingAdmissionPolicy` if you need fine-grained request filtering. These
371+
conditions are useful if you find that match rules, `objectSelectors` and `namespaceSelectors` still
372+
doesn't provide the filtering you want. Match conditions are
373+
[CEL expressions](/docs/reference/using-api/cel/). All match conditions must evaluate to true for the
374+
resource to be evaluated.
375+
376+
Here is an example illustrating a few different uses for match conditions:
377+
378+
{{< codenew file="access/validating-admission-policy-match-conditions.yaml" >}}
379+
380+
Match conditions have access to the same CEL variables as validation expressions.
381+
382+
In the event of an error evaluating a match condition the policy is not evaluated. Whether to reject
383+
the request is determined as follows:
384+
385+
1. If **any** match condition evaluated to `false` (regardless of other errors), the API server skips the policy.
386+
2. Otherwise:
387+
- for [`failurePolicy: Fail`](#failure-policy), reject the request (without evaluating the policy).
388+
- for [`failurePolicy: Ignore`](#failure-policy), proceed with the request but skip the policy.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: admissionregistration.k8s.io/v1alpha1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: "demo-policy.example.com"
5+
spec:
6+
failurePolicy: Fail
7+
matchConstraints:
8+
resourceRules:
9+
- apiGroups: ["*"]
10+
apiVersions: ["*"]
11+
operations: ["CREATE", "UPDATE"]
12+
resources: ["*"]
13+
matchConditions:
14+
- name: 'exclude-leases' # Each match condition must have a unique name
15+
expression: '!(request.resource.group == "coordination.k8s.io" && request.resource.resource == "leases")' # Match non-lease resources.
16+
- name: 'exclude-kubelet-requests'
17+
expression: '!("system:nodes" in request.userInfo.groups)' # Match requests made by non-node users.
18+
- name: 'rbac' # Skip RBAC requests.
19+
expression: 'request.resource.group != "rbac.authorization.k8s.io"'
20+
validations:
21+
- expression: "!object.metadata.name.contains('demo') || object.metadata.namespace == 'demo'"
22+

0 commit comments

Comments
 (0)