Skip to content

Commit 6b51a56

Browse files
authored
Merge pull request #38522 from tengqm/add-admission-config-api
Add admission config API and fixes references to the API
2 parents 422691d + 37955a8 commit 6b51a56

File tree

3 files changed

+405
-52
lines changed

3 files changed

+405
-52
lines changed

content/en/docs/reference/_index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ content_type: concept
99
no_list: true
1010
---
1111

12-
1312
<!-- overview -->
1413

1514
This section of the Kubernetes documentation contains references.
1615

17-
18-
1916
<!-- body -->
2017

2118
## API Reference
@@ -76,6 +73,7 @@ operator to use or manage a cluster.
7673

7774

7875
* [kubeconfig (v1)](/docs/reference/config-api/kubeconfig.v1/)
76+
* [kube-apiserver admission (v1)](/docs/reference/config-api/apiserver-admission.v1/)
7977
* [kube-apiserver configuration (v1alpha1)](/docs/reference/config-api/apiserver-config.v1alpha1/) and
8078
[kube-apiserver configuration (v1)](/docs/reference/config-api/apiserver-config.v1/)
8179
* [kube-apiserver encryption (v1)](/docs/reference/config-api/apiserver-encryption.v1/)

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

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,32 @@ This page provides an overview of Validating Admission Policy.
2020

2121
Validating admission policies offer a declarative, in-process alternative to validating admission webhooks.
2222

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.
2527

2628
## What Resources Make a Policy
2729

2830
A policy is generally made up of three resources:
2931

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").
3134

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.
3338

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.
3543

44+
At least a `ValidatingAdmissionPolicy` and a corresponding `ValidatingAdmissionPolicyBinding`
45+
must be defined for a policy to have an effect.
3646

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.
4049

4150
## {{% heading "prerequisites" %}}
4251

@@ -45,11 +54,13 @@ If a `ValidatingAdmissionPolicy` does not need to be configured via parameters,
4554

4655
## Getting Started with Validating Admission Policy
4756

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.
4959

5060
### Creating a ValidatingAdmissionPolicy
5161

5262
The following is an example of a ValidatingAdmissionPolicy.
63+
5364
```yaml
5465
apiVersion: admissionregistration.k8s.io/v1alpha1
5566
kind: ValidatingAdmissionPolicy
@@ -66,10 +77,14 @@ spec:
6677
validations:
6778
- expression: "object.spec.replicas <= 5"
6879
```
80+
6981
`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.:
7187

72-
To configure a validating admission policy for use in a cluster, a binding is required. The following is an example of a ValidatingAdmissionPolicyBinding.:
7388
```yaml
7489
apiVersion: admissionregistration.k8s.io/v1alpha1
7590
kind: ValidatingAdmissionPolicyBinding
@@ -83,8 +98,10 @@ spec:
8398
environment: test
8499
```
85100

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
102+
error will return containing message:
103+
104+
```none
88105
ValidatingAdmissionPolicy 'demo-policy.example.com' with binding 'demo-binding-test.example.com' denied request: failed expression: object.spec.replicas <= 5
89106
```
90107

@@ -96,7 +113,9 @@ Parameter resources allow a policy configuration to be separate from its definit
96113
A policy can define paramKind, which outlines GVK of the parameter resource,
97114
and then a policy binding ties a policy by name (via policyName) to a particular parameter resource via paramRef.
98115

99-
If parameter configuration is needed, the following is an example of a ValidatingAdmissionPolicy with parameter configuration.
116+
If parameter configuration is needed, the following is an example of a ValidatingAdmissionPolicy
117+
with parameter configuration.
118+
100119
```yaml
101120
apiVersion: admissionregistration.k8s.io/v1alpha1
102121
kind: ValidatingAdmissionPolicy
@@ -117,16 +136,22 @@ spec:
117136
- expression: "object.spec.replicas <= params.maxReplicas"
118137
reason: Invalid
119138
```
120-
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.
124139

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.
126149

127150
The validating admission policy author is responsible for providing the ReplicaLimit parameter CRD.
128151

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+
130155
```yaml
131156
apiVersion: admissionregistration.k8s.io/v1alpha1
132157
kind: ValidatingAdmissionPolicyBinding
@@ -141,16 +166,21 @@ spec:
141166
matchLabels:
142167
environment: test
143168
```
169+
144170
The parameter resource could be as following:
171+
145172
```yaml
146173
apiVersion: rules.example.com/v1
147174
kind: ReplicaLimit
148175
metadata:
149176
name: "replica-limit-test.example.com"
150177
maxReplicas: 3
151178
```
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+
154184
```yaml
155185
apiVersion: admissionregistration.k8s.io/v1alpha1
156186
kind: ValidatingAdmissionPolicyBinding
@@ -167,15 +197,20 @@ spec:
167197
operator: NotIn,
168198
values: ["test"]
169199
```
200+
170201
And have a parameter resource like:
202+
171203
```yaml
172204
apiVersion: rules.example.com/v1
173205
kind: ReplicaLimit
174206
metadata:
175207
name: "replica-limit-clusterwide.example.com"
176208
maxReplicas: 100
177209
```
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+
179214
```yaml
180215
apiVersion: admissionregistration.k8s.io/v1alpha1
181216
kind: ValidatingAdmissionPolicyBinding
@@ -191,42 +226,56 @@ spec:
191226
operator: Exists
192227
```
193228

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`:
197235

198-
For the use cases require parameter configuration,
199-
we recommend to add a param check in `spec.validations[0].expression`:
200236
```
201237
- expression: "params != null"
202238
message: "params missing but required to bind to this policy"
203239
```
204240

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+
208246
Combining the two, we can provide a way to validate optional parameters:
247+
209248
`!has(params.optionalNumber) || (params.optionalNumber >= 5 && params.optionalNumber <= 10)`
249+
210250
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.
213257

214258
#### Authorization Check
215259

216260
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`.
218263

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.
220266

221267
### Failure Policy
222268

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`.
225271

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.
228276

229277
Note that the `failurePolicy` is defined inside `ValidatingAdmissionPolicy`:
278+
230279
```yaml
231280
apiVersion: admissionregistration.k8s.io/v1alpha1
232281
kind: ValidatingAdmissionPolicy
@@ -241,18 +290,21 @@ validations:
241290

242291
`spec.validations[i].expression` represents the expression which will be evaluated by CEL.
243292
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+
245296
- 'object' - The object from the incoming request. The value is null for DELETE requests.
246297
- '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.
249301

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.
252304
253305
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
307+
expression:
256308

257309
| escape sequence | property name equivalent |
258310
| ----------------------- | -----------------------|
@@ -303,10 +355,12 @@ Concatenation on arrays with x-kubernetes-list-type use the semantics of the lis
303355
| `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 |
304356
| `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 |
305357

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.
307360

308361
`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.
311364
The currently supported reasons are: `Unauthorized`, `Forbidden`, `Invalid`, `RequestEntityTooLarge`.
312365
If not set, `StatusReasonInvalid` is used in the response to the client.
366+

0 commit comments

Comments
 (0)