You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -20,23 +20,32 @@ This page provides an overview of Validating Admission Policy.
20
20
21
21
Validating admission policies offer a declarative, in-process alternative to validating admission webhooks.
22
22
23
-
Validating admission policies use the Common Expression Language (CEL) to declare the validation rules of a policy.
24
-
Validation admission policies are highly configurable, enabling policy authors to define policies that can be parameterized and scoped to resources as needed by cluster administrators.
23
+
Validating admission policies use the Common Expression Language (CEL) to declare the validation
24
+
rules of a policy.
25
+
Validation admission policies are highly configurable, enabling policy authors to define policies
26
+
that can be parameterized and scoped to resources as needed by cluster administrators.
25
27
26
28
## What Resources Make a Policy
27
29
28
30
A policy is generally made up of three resources:
29
31
30
-
- The `ValidatingAdmissionPolicy` describes the abstract logic of a policy (think: "this policy makes sure a particular label is set to a particular value").
32
+
- The `ValidatingAdmissionPolicy` describes the abstract logic of a policy
33
+
(think: "this policy makes sure a particular label is set to a particular value").
31
34
32
-
- A `ValidatingAdmissionPolicyBinding` links the above resources together and provides scoping. If you only want to require an `owner` label to be set for `Pods`, the binding is where you would specify this restriction.
35
+
- A `ValidatingAdmissionPolicyBinding` links the above resources together and provides scoping.
36
+
If you only want to require an `owner` label to be set for `Pods`, the binding is where you would
37
+
specify this restriction.
33
38
34
-
- A parameter resource provides information to a ValidatingAdmissionPolicy to make it a concrete statement (think "the `owner` label must be set to something that ends in `.company.com`"). A native type such as ConfigMap or a CRD defines the schema of a parameter resource. `ValidatingAdmissionPolicy` objects specify what Kind they are expecting for their parameter resource.
39
+
- A parameter resource provides information to a ValidatingAdmissionPolicy to make it a concrete
40
+
statement (think "the `owner` label must be set to something that ends in `.company.com`").
41
+
A native type such as ConfigMap or a CRD defines the schema of a parameter resource.
42
+
`ValidatingAdmissionPolicy` objects specify what Kind they are expecting for their parameter resource.
35
43
44
+
At least a `ValidatingAdmissionPolicy` and a corresponding `ValidatingAdmissionPolicyBinding`
45
+
must be defined for a policy to have an effect.
36
46
37
-
At least a `ValidatingAdmissionPolicy` and a corresponding `ValidatingAdmissionPolicyBinding` must be defined for a policy to have an effect.
38
-
39
-
If a `ValidatingAdmissionPolicy` does not need to be configured via parameters, simply leave `spec.paramKind` in `ValidatingAdmissionPolicy` unset.
47
+
If a `ValidatingAdmissionPolicy` does not need to be configured via parameters, simply leave
48
+
`spec.paramKind` in `ValidatingAdmissionPolicy` unset.
40
49
41
50
## {{% heading "prerequisites" %}}
42
51
@@ -45,11 +54,13 @@ If a `ValidatingAdmissionPolicy` does not need to be configured via parameters,
45
54
46
55
## Getting Started with Validating Admission Policy
47
56
48
-
Validating Admission Policy is part of the cluster control-plane. You should write and deploy them with great caution. The following describes how to quickly experiment with Validating Admission Policy.
57
+
Validating Admission Policy is part of the cluster control-plane. You should write and deploy them
58
+
with great caution. The following describes how to quickly experiment with Validating Admission Policy.
49
59
50
60
### Creating a ValidatingAdmissionPolicy
51
61
52
62
The following is an example of a ValidatingAdmissionPolicy.
63
+
53
64
```yaml
54
65
apiVersion: admissionregistration.k8s.io/v1alpha1
55
66
kind: ValidatingAdmissionPolicy
@@ -66,10 +77,14 @@ spec:
66
77
validations:
67
78
- expression: "object.spec.replicas <= 5"
68
79
```
80
+
69
81
`spec.validations` contains CEL expressions which use the [Common Expression Language (CEL)](https://github.com/google/cel-spec)
70
-
to validate the request. If an expression evaluates to false, the validation check is enforced according to the `spec.failurePolicy` field.
82
+
to validate the request. If an expression evaluates to false, the validation check is enforced
83
+
according to the `spec.failurePolicy` field.
84
+
85
+
To configure a validating admission policy for use in a cluster, a binding is required.
86
+
The following is an example of a ValidatingAdmissionPolicyBinding.:
71
87
72
-
To configure a validating admission policy for use in a cluster, a binding is required. The following is an example of a ValidatingAdmissionPolicyBinding.:
73
88
```yaml
74
89
apiVersion: admissionregistration.k8s.io/v1alpha1
75
90
kind: ValidatingAdmissionPolicyBinding
@@ -83,8 +98,10 @@ spec:
83
98
environment: test
84
99
```
85
100
86
-
When trying to create a deployment with replicas set not satisfying the validation expression, an error will return containing message:
87
-
```
101
+
When trying to create a deployment with replicas set not satisfying the validation expression, an
The `spec.paramKind` field of the ValidatingAdmissionPolicy specifies the kind of resources used to parameterize this policy. For this example, it is configured by ReplicaLimit custom resources.
121
-
Note in this example how the CEL expression references the parameters via the CEL params variable, e.g. `params.maxReplicas`.
122
-
spec.matchConstraints specifies what resources this policy is designed to validate.
123
-
Note that the native types such like `ConfigMap` could also be used as parameter reference.
124
139
125
-
The `spec.validations` fields contain CEL expressions. If an expression evaluates to false, the validation check is enforced according to the `spec.failurePolicy` field.
140
+
The `spec.paramKind` field of the ValidatingAdmissionPolicy specifies the kind of resources used
141
+
to parameterize this policy. For this example, it is configured by ReplicaLimit custom resources.
142
+
Note in this example how the CEL expression references the parameters via the CEL params variable,
143
+
e.g. `params.maxReplicas`. `spec.matchConstraints` specifies what resources this policy is
144
+
designed to validate. Note that the native types such like `ConfigMap` could also be used as
145
+
parameter reference.
146
+
147
+
The `spec.validations` fields contain CEL expressions. If an expression evaluates to false, the
148
+
validation check is enforced according to the `spec.failurePolicy` field.
126
149
127
150
The validating admission policy author is responsible for providing the ReplicaLimit parameter CRD.
128
151
129
-
To configure an validating admission policy for use in a cluster, a binding and parameter resource are created. The following is an example of a ValidatingAdmissionPolicyBinding.
152
+
To configure an validating admission policy for use in a cluster, a binding and parameter resource
153
+
are created. The following is an example of a ValidatingAdmissionPolicyBinding.
154
+
130
155
```yaml
131
156
apiVersion: admissionregistration.k8s.io/v1alpha1
132
157
kind: ValidatingAdmissionPolicyBinding
@@ -141,16 +166,21 @@ spec:
141
166
matchLabels:
142
167
environment: test
143
168
```
169
+
144
170
The parameter resource could be as following:
171
+
145
172
```yaml
146
173
apiVersion: rules.example.com/v1
147
174
kind: ReplicaLimit
148
175
metadata:
149
176
name: "replica-limit-test.example.com"
150
177
maxReplicas: 3
151
178
```
152
-
This policy parameter resource limits deployments to a max of 3 replicas in all namespaces in the test environment.
153
-
An admission policy may have multiple bindings. To bind all other environments environment to have a maxReplicas limit of 100, create another ValidatingAdmissionPolicyBinding:
179
+
180
+
This policy parameter resource limits deployments to a max of 3 replicas in all namespaces in the
181
+
test environment. An admission policy may have multiple bindings. To bind all other environments
182
+
environment to have a maxReplicas limit of 100, create another ValidatingAdmissionPolicyBinding:
183
+
154
184
```yaml
155
185
apiVersion: admissionregistration.k8s.io/v1alpha1
156
186
kind: ValidatingAdmissionPolicyBinding
@@ -167,15 +197,20 @@ spec:
167
197
operator: NotIn,
168
198
values: ["test"]
169
199
```
200
+
170
201
And have a parameter resource like:
202
+
171
203
```yaml
172
204
apiVersion: rules.example.com/v1
173
205
kind: ReplicaLimit
174
206
metadata:
175
207
name: "replica-limit-clusterwide.example.com"
176
208
maxReplicas: 100
177
209
```
178
-
Bindings can have overlapping match criteria. The policy is evaluated for each matching binding. In the above example, the "nontest" policy binding could instead have been defined as a global policy:
210
+
211
+
Bindings can have overlapping match criteria. The policy is evaluated for each matching binding.
212
+
In the above example, the "nontest" policy binding could instead have been defined as a global policy:
213
+
179
214
```yaml
180
215
apiVersion: admissionregistration.k8s.io/v1alpha1
181
216
kind: ValidatingAdmissionPolicyBinding
@@ -191,42 +226,56 @@ spec:
191
226
operator: Exists
192
227
```
193
228
194
-
The params object representing a parameter resource will not be set if a parameter resource has not been bound,
195
-
so for policies requiring a parameter resource,
196
-
it can be useful to add a check to ensure one has been bound.
229
+
The params object representing a parameter resource will not be set if a parameter resource has
230
+
not been bound, so for policies requiring a parameter resource, it can be useful to add a check to
231
+
ensure one has been bound.
232
+
233
+
For the use cases require parameter configuration, we recommend to add a param check in
234
+
`spec.validations[0].expression`:
197
235
198
-
For the use cases require parameter configuration,
199
-
we recommend to add a param check in `spec.validations[0].expression`:
200
236
```
201
237
- expression: "params != null"
202
238
message: "params missing but required to bind to this policy"
203
239
```
204
240
205
-
It can be convenient to be able to have optional parameters as part of a parameter resource, and only validate them if present.
206
-
CEL provides has(), which checks if the key passed to it exists. CEL also implements Boolean short-circuiting:
207
-
If the first half of a logical OR evaluates to true, it won’t evaluate the other half (since the result of the entire OR will be true regardless).
241
+
It can be convenient to be able to have optional parameters as part of a parameter resource, and
242
+
only validate them if present. CEL provides `has()`, which checks if the key passed to it exists.
243
+
CEL also implements Boolean short-circuiting. If the first half of a logical OR evaluates to true,
244
+
it won’t evaluate the other half (since the result of the entire OR will be true regardless).
245
+
208
246
Combining the two, we can provide a way to validate optional parameters:
Here, we first check that the optional parameter is present with `!has(params.optionalNumber)`.
211
-
If `optionalNumber` hasn’t been defined, then the expression short-circuits since `!has(params.optionalNumber)` will evaluate to true.
212
-
If `optionalNumber` has been defined, then the latter half of the CEL expression will be evaluated, and optionalNumber will be checked to ensure that it contains a value between 5 and 10 inclusive.
251
+
252
+
- If `optionalNumber` hasn’t been defined, then the expression short-circuits since
253
+
`!has(params.optionalNumber)`will evaluate to true.
254
+
- If `optionalNumber` has been defined, then the latter half of the CEL expression will be
255
+
evaluated, and optionalNumber will be checked to ensure that it contains a value between 5 and
256
+
10 inclusive.
213
257
214
258
#### Authorization Check
215
259
216
260
We introduced the authorization check for parameter resources.
217
-
User is expected to have `read` access to the resources referenced by `paramKind` in `ValidatingAdmissionPolicy` and `paramRef` in `ValidatingAdmissionPolicyBinding`.
261
+
User is expected to have `read` access to the resources referenced by `paramKind` in
262
+
`ValidatingAdmissionPolicy`and `paramRef` in `ValidatingAdmissionPolicyBinding`.
218
263
219
-
Note that if a resource in `paramKind` fails resolving via the restmapper, `read` access to all resources of groups is required.
264
+
Note that if a resource in `paramKind` fails resolving via the restmapper, `read` access to all
265
+
resources of groups is required.
220
266
221
267
### Failure Policy
222
268
223
-
`failurePolicy`defines how mis-configurations and CEL expressions evaluating to error from the admission policy are handled.
224
-
Allowed values are `Ignore` or `Fail`.
269
+
`failurePolicy`defines how mis-configurations and CEL expressions evaluating to error from the
270
+
admission policy are handled. Allowed values are `Ignore` or `Fail`.
225
271
226
-
- `Ignore`means that an error calling the ValidatingAdmissionPolicy is ignored and the API request is allowed to continue.
227
-
- `Fail`means that an error calling the ValidatingAdmissionPolicy causes the admission to fail and the API request to be rejected.
272
+
- `Ignore`means that an error calling the ValidatingAdmissionPolicy is ignored and the API
273
+
request is allowed to continue.
274
+
- `Fail`means that an error calling the ValidatingAdmissionPolicy causes the admission to fail
275
+
and the API request to be rejected.
228
276
229
277
Note that the `failurePolicy` is defined inside `ValidatingAdmissionPolicy`:
278
+
230
279
```yaml
231
280
apiVersion: admissionregistration.k8s.io/v1alpha1
232
281
kind: ValidatingAdmissionPolicy
@@ -241,18 +290,21 @@ validations:
241
290
242
291
`spec.validations[i].expression`represents the expression which will be evaluated by CEL.
243
292
To learn more, see the [CEL language specification](https://github.com/google/cel-spec)
244
-
CEL expressions have access to the contents of the Admission request/response, organized into CEL variables as well as some other useful variables:
293
+
CEL expressions have access to the contents of the Admission request/response, organized into CEL
294
+
variables as well as some other useful variables:
295
+
245
296
- 'object'- The object from the incoming request. The value is null for DELETE requests.
246
297
- 'oldObject'- The existing object. The value is null for CREATE requests.
247
-
- 'request'- Attributes of the [admission request](/pkg/apis/admission/types.go#AdmissionRequest).
248
-
- 'params'- Parameter resource referred to by the policy binding being evaluated. The value is null if `ParamKind` is unset.
298
+
- 'request'- Attributes of the [admission request](/docs/reference/config-api/apiserver-admission.v1/#admission-k8s-io-v1-AdmissionRequest).
299
+
- 'params'- Parameter resource referred to by the policy binding being evaluated. The value is
300
+
null if `ParamKind` is unset.
249
301
250
-
The `apiVersion`, `kind`, `metadata.name` and `metadata.generateName` are always accessible from the root of the
251
-
object. No other metadata properties are accessible.
302
+
The `apiVersion`, `kind`, `metadata.name` and `metadata.generateName` are always accessible from
303
+
the root of the object. No other metadata properties are accessible.
252
304
253
305
Only property names of the form `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*` are accessible.
254
-
Only property names of the form `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*` are accessible.
255
-
Accessible property names are escaped according to the following rules when accessed in the expression:
306
+
Accessible property names are escaped according to the following rules when accessed in the
@@ -303,10 +355,12 @@ Concatenation on arrays with x-kubernetes-list-type use the semantics of the lis
303
355
| `size(object.names) == size(object.details) && object.names.all(n, n in object.details)` | Validate the 'details' map is keyed by the items in the 'names' listSet |
304
356
| `size(object.clusters.filter(c, c.name == object.primary)) == 1` | Validate that the 'primary' property has one and only one occurrence in the 'clusters' listMap |
305
357
306
-
Read [Supported evaluation on CEL](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#evaluation) for more information about CEL rules.
358
+
Read [Supported evaluation on CEL](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#evaluation)
359
+
for more information about CEL rules.
307
360
308
361
`spec.validation[i].reason`represents a machine-readable description of why this validation failed.
309
-
If this is the first validation in the list to fail, this reason, as well as the corresponding HTTP response code, are used in the
310
-
HTTP response to the client.
362
+
If this is the first validation in the list to fail, this reason, as well as the corresponding
363
+
HTTP response code, are used in the HTTP response to the client.
311
364
The currently supported reasons are: `Unauthorized`, `Forbidden`, `Invalid`, `RequestEntityTooLarge`.
312
365
If not set, `StatusReasonInvalid` is used in the response to the client.
0 commit comments