Skip to content

Commit 457c26b

Browse files
committed
Adding MatchConditions into ValidatingAdmissionPolicy
1 parent 29f0dd8 commit 457c26b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.startWith('demo')"
22+

0 commit comments

Comments
 (0)