@@ -154,6 +154,7 @@ metadata:
154
154
name: "demo-binding-test.example.com"
155
155
spec:
156
156
policyName: "demo-policy.example.com"
157
+ validationActions: [Deny]
157
158
matchResources:
158
159
namespaceSelector:
159
160
matchLabels:
@@ -175,6 +176,65 @@ The above provides a simple example of using ValidatingAdmissionPolicy without a
175
176
-->
176
177
上面提供的是一个简单的、无配置参数的 ValidatingAdmissionPolicy。
177
178
179
+ <!--
180
+ # ### Validation actions
181
+
182
+ Each `ValidatingAdmissionPolicyBinding` must specify one or more
183
+ ` validationActions` to declare how `validations` of a policy are enforced.
184
+ -->
185
+ # ### 验证操作
186
+
187
+ 每个 `ValidatingAdmissionPolicyBinding` 必须指定一个或多个
188
+ ` validationActions` 来声明如何执行策略的 `validations`。
189
+
190
+ <!--
191
+ The supported `validationActions` are :
192
+ -->
193
+ 支持的 `validationActions` 包括:
194
+
195
+ <!--
196
+ - `Deny` : Validation failure results in a denied request.
197
+ - `Warn` : Validation failure is reported to the request client
198
+ as a [warning](/blog/2020/09/03/warnings/).
199
+ - `Audit` : Validation failure is included in the audit event for the API request.
200
+ -->
201
+ - `Deny` : 验证失败会导致请求被拒绝。
202
+ - `Warn` : 验证失败会作为[警告](/blog/2020/09/03/warnings/)报告给请求客户端。
203
+ - `Audit` : 验证失败会包含在 API 请求的审计事件中。
204
+
205
+ <!--
206
+ For example, to both warn clients about a validation failure and to audit the
207
+ validation failures, use :
208
+ -->
209
+ 例如,要同时向客户端发出验证失败的警告并记录验证失败的审计记录,请使用以下配置:
210
+
211
+ ` ` ` yaml
212
+ validationActions: [Warn, Audit]
213
+ ` ` `
214
+ <!--
215
+ ` Deny` and `Warn` may not be used together since this combination
216
+ needlessly duplicates the validation failure both in the
217
+ API response body and the HTTP warning headers.
218
+ -->
219
+ ` Deny` 和 `Warn` 不能一起使用,因为这种组合会不必要地将验证失败重复输出到
220
+ API 响应体和 HTTP 警告头中。
221
+ <!--
222
+ A `validation` that evaluates to false is always enforced according to these
223
+ actions. Failures defined by the `failurePolicy` are enforced
224
+ according to these actions only if the `failurePolicy` is set to `Fail` (or unset),
225
+ otherwise the failures are ignored.
226
+ -->
227
+ 如果 `validation` 求值为 false,则始终根据这些操作执行。
228
+ 由 `failurePolicy` 定义的失败仅在 `failurePolicy` 设置为 `Fail`(或未设置)时根据这些操作执行,
229
+ 否则这些失败将被忽略。
230
+
231
+ <!--
232
+ See [Audit Annotations : validation falures](/docs/reference/labels-annotations-taints/audit-annotations/#validation-policy-admission-k8s-io-validation_failure)
233
+ for more details about the validation failure audit annotation.
234
+ -->
235
+ 有关验证失败审计注解的详细信息,请参见
236
+ [审计注解:验证失败](/zh-cn/docs/reference/labels-annotations-taints/audit-annotations/#validation-policy-admission-k8s-io-validation_failure)。
237
+
178
238
<!--
179
239
# ### Parameter resources
180
240
@@ -252,6 +312,7 @@ metadata:
252
312
name: "replicalimit-binding-test.example.com"
253
313
spec:
254
314
policyName: "replicalimit-policy.example.com"
315
+ validationActions: [Deny]
255
316
paramRef:
256
317
name: "replica-limit-test.example.com"
257
318
matchResources:
@@ -289,6 +350,7 @@ metadata:
289
350
name: "replicalimit-binding-nontest"
290
351
spec:
291
352
policyName: "replicalimit-policy.example.com"
353
+ validationActions: [Deny]
292
354
paramRef:
293
355
name: "replica-limit-clusterwide.example.com"
294
356
matchResources:
@@ -327,6 +389,7 @@ metadata:
327
389
name: "replicalimit-binding-global"
328
390
spec:
329
391
policyName: "replicalimit-policy.example.com"
392
+ validationActions: [Deny]
330
393
params: "replica-limit-clusterwide.example.com"
331
394
matchResources:
332
395
namespaceSelector:
@@ -449,6 +512,12 @@ variables as well as some other useful variables:
449
512
- 'request' - Attributes of the [admission request](/docs/reference/config-api/apiserver-admission.v1/#admission-k8s-io-v1-AdmissionRequest).
450
513
- 'params' - Parameter resource referred to by the policy binding being evaluated. The value is
451
514
null if `ParamKind` is unset.
515
+ - `authorizer` - A CEL Authorizer. May be used to perform authorization checks for the principal
516
+ (authenticated user) of the request. See
517
+ [Authz](https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#Authz) in the Kubernetes CEL library
518
+ documentation for more details.
519
+ - `authorizer.requestResource` - A shortcut for an authorization check configured with the request
520
+ resource (group, resource, (subresource), namespace, name).
452
521
-->
453
522
### 检查表达式
454
523
@@ -460,6 +529,9 @@ CEL 表达式可以访问按 CEL 变量来组织的 Admission 请求/响应的
460
529
- 'oldObject' - 现有对象。对于 CREATE 请求,该值为 null。
461
530
- 'request' - [ 准入请求] ( /zh-cn/docs/reference/config-api/apiserver-admission.v1/#admission-k8s-io-v1-AdmissionRequest ) 的属性。
462
531
- 'params' - 被计算的策略绑定引用的参数资源。如果未设置 ` paramKind ` ,该值为 null。
532
+ - ` authorizer ` - 一个 CEL 鉴权组件。可以用来为请求的主体(经过身份验证的用户)执行鉴权检查。
533
+ 更多细节可以参考 Kubernetes CEL 库的文档中的 [ Authz] ( https://pkg.go.dev/k8s.io/apiserver/pkg/cel/library#Authz ) 。
534
+ - ` authorizer.requestResource ` - 针对请求资源(组、资源、(子资源)、命名空间、名称)所配置的鉴权检查的快捷方式。
463
535
464
536
<!--
465
537
The `apiVersion`, `kind`, `metadata.name` and `metadata.generateName` are always accessible from
@@ -560,3 +632,271 @@ If not set, `StatusReasonInvalid` is used in the response to the client.
560
632
如果这是列表中第一个失败的检查,其原因以及相应的 HTTP 响应代码会被用在给客户端的 HTTP 响应中。
561
633
目前支持的原因有:` Unauthorized ` 、` Forbidden ` 、` Invalid ` 、` RequestEntityTooLarge ` 。
562
634
如果未设置,将在对客户端的响应中使用 ` StatusReasonInvalid ` 。
635
+
636
+ <!--
637
+ ### Matching requests: `matchConditions`
638
+
639
+ You can define _match conditions_ for a `ValidatingAdmissionPolicy` if you need fine-grained request filtering. These
640
+ conditions are useful if you find that match rules, `objectSelectors` and `namespaceSelectors` still
641
+ doesn't provide the filtering you want. Match conditions are
642
+ [CEL expressions](/docs/reference/using-api/cel/). All match conditions must evaluate to true for the
643
+ resource to be evaluated.
644
+ -->
645
+ ### 匹配请求:` matchConditions `
646
+
647
+ 如果需要进行精细的请求过滤,可以为 ` ValidatingAdmissionPolicy ` 定义 ** 匹配条件(match conditions)** 。
648
+ 如果发现匹配规则、` objectSelectors ` 和 ` namespaceSelectors ` 仍无法提供所需的过滤功能,则使用这些条件非常有用。
649
+ 匹配条件是 [ CEL 表达式] ( /zh-cn/docs/reference/using-api/cel/ ) 。
650
+ 所有匹配条件必须求值为 true 时才会对资源进行评估。
651
+
652
+ <!--
653
+ Here is an example illustrating a few different uses for match conditions:
654
+ -->
655
+ 以下示例说明了匹配条件的几个不同用法:
656
+
657
+ {{< codenew file="access/validating-admission-policy-match-conditions.yaml" >}}
658
+
659
+ <!--
660
+ Match conditions have access to the same CEL variables as validation expressions.
661
+
662
+ In the event of an error evaluating a match condition the policy is not evaluated. Whether to reject
663
+ the request is determined as follows:
664
+
665
+ 1. If **any** match condition evaluated to `false` (regardless of other errors), the API server skips the policy.
666
+ 2. Otherwise:
667
+ - for [`failurePolicy: Fail`](#failure-policy), reject the request (without evaluating the policy).
668
+ - for [`failurePolicy: Ignore`](#failure-policy), proceed with the request but skip the policy.
669
+ -->
670
+ 这些匹配条件可以访问与验证表达式相同的 CEL 变量。
671
+
672
+ 在评估匹配条件时出现错误时,将不会评估策略。根据以下方式确定是否拒绝请求:
673
+
674
+ 1 . 如果** 任何一个** 匹配条件求值结果为 ` false ` (不管其他错误),API 服务器将跳过 Webhook。
675
+ 2 . 否则:
676
+
677
+ - 对于 [ ` failurePolicy: Fail ` ] ( #failure-policy ) ,拒绝请求(不调用 Webhook)。
678
+ - 对于 [ ` failurePolicy: Ignore ` ] ( #failure-policy ) ,继续处理请求但跳过 Webhook。
679
+
680
+ <!--
681
+ ### Audit annotations
682
+
683
+ `auditAnnotations` may be used to include audit annotations in the audit event of the API request.
684
+
685
+ For example, here is an admission policy with an audit annotation:
686
+ -->
687
+ ### 审计注解
688
+
689
+ ` auditAnnotations ` 可用于在 API 请求的审计事件中包括审计注解。
690
+
691
+ 例如,以下是带有审计注解的准入策略:
692
+
693
+ {{< codenew file="access/validating-admission-policy-audit-annotation.yaml" >}}
694
+
695
+ <!--
696
+ When an API request is validated with this admission policy, the resulting audit event will look like:
697
+ -->
698
+ 当使用此准入策略验证 API 请求时,生成的审计事件将如下所示:
699
+
700
+ ```
701
+ # the audit event recorded
702
+ {
703
+ "kind": "Event",
704
+ "apiVersion": "audit.k8s.io/v1",
705
+ "annotations": {
706
+ "demo-policy.example.com/high-replica-count": "Deployment spec.replicas set to 128"
707
+ # other annotations
708
+ ...
709
+ }
710
+ # other fields
711
+ ...
712
+ }
713
+ ```
714
+
715
+ <!--
716
+ In this example the annotation will only be included if the `spec.replicas` of the Deployment is more than
717
+ 50, otherwise the CEL expression evalutes to null and the annotation will not be included.
718
+
719
+ Note that audit annotation keys are prefixed by the name of the `ValidatingAdmissionWebhook` and a `/`. If
720
+ another admission controller, such as an admission webhook, uses the exact same audit annotation key, the
721
+ value of the first admission controller to include the audit annotation will be included in the audit
722
+ event and all other values will be ignored.
723
+ -->
724
+ 在此示例中,只有 Deployment 的 ` spec.replicas ` 大于 50 时才会包含注解,
725
+ 否则 CEL 表达式将求值为 null,并且不会包含注解。
726
+
727
+ 请注意,审计注解键以 ` ValidatingAdmissionWebhook ` 的名称和 ` / ` 为前缀。
728
+ 如果另一个准入控制器(例如准入 Webhook)使用完全相同的审计注解键,
729
+ 则第一个包括审计注解值的准入控制器将出现在审计事件中,而所有其他值都将被忽略。
730
+
731
+ <!--
732
+ ### Message expression
733
+
734
+ To return a more friendly message when the policy rejects a request, we can use a CEL expression
735
+ to composite a message with `spec.validations[i].messageExpression`. Similar to the validation expression,
736
+ a message expression has access to `object`, `oldObject`, `request`, and `params`. Unlike validations,
737
+ message expression must evaluate to a string.
738
+
739
+ For example, to better inform the user of the reason of denial when the policy refers to a parameter,
740
+ we can have the following validation:
741
+ -->
742
+ ### 消息表达式
743
+
744
+ 为了在策略拒绝请求时返回更友好的消息,我们在 ` spec.validations[i].messageExpression `
745
+ 中使用 CEL 表达式来构造消息。
746
+ 与验证表达式类似,消息表达式可以访问 ` object ` 、` oldObject ` 、` request ` 和 ` params ` 。
747
+ 但是,与验证不同,消息表达式必须求值为字符串。
748
+
749
+ 例如,为了在策略引用参数时更好地告知用户拒绝原因,我们可以有以下验证:
750
+
751
+ {{< codenew file="access/deployment-replicas-policy.yaml" >}}
752
+
753
+ <!--
754
+ After creating a params object that limits the replicas to 3 and setting up the binding,
755
+ when we try to create a deployment with 5 replicas, we will receive the following message.
756
+ -->
757
+ 在创建限制副本为 3 的 Params 对象并设置绑定之后,当我们尝试创建具有 5 个副本的 Deployment
758
+ 时,我们将收到以下消息:
759
+
760
+ ```
761
+ $ kubectl create deploy --image=nginx nginx --replicas=5
762
+ error: failed to create deployment: deployments.apps "nginx" is forbidden: ValidatingAdmissionPolicy 'deploy-replica-policy.example.com' with binding 'demo-binding-test.example.com' denied request: object.spec.replicas must be no greater than 3
763
+ ```
764
+
765
+ <!--
766
+ This is more informative than a static message of "too many replicas".
767
+
768
+ The message expression takes precedence over the static message defined in `spec.validations[i].message` if both are defined.
769
+ However, if the message expression fails to evaluate, the static message will be used instead.
770
+ Additionally, if the message expression evaluates to a multi-line string,
771
+ the evaluation result will be discarded and the static message will be used if present.
772
+ Note that static message is validated against multi-line strings.
773
+ -->
774
+ 这比静态消息 "too many replicas" 更具说明性。
775
+
776
+ 如果既定义了消息表达式,又在 ` spec.validations[i].message ` 中定义了静态消息,
777
+ 则消息表达式优先于静态消息。
778
+ 但是,如果消息表达式求值失败,则将使用静态消息。
779
+ 此外,如果消息表达式求值为多行字符串,则会丢弃求值结果并使用静态消息(如果存在)。
780
+ 请注意,静态消息也要检查是否存在多行字符串。
781
+
782
+ <!--
783
+ ### Type checking
784
+
785
+ When a policy definition is created or updated, the validation process parses the expressions it contains
786
+ and reports any syntax errors, rejecting the definition if any errors are found.
787
+ Afterward, the referred variables are checked for type errors, including missing fields and type confusion,
788
+ against the matched types of `spec.matchConstraints`.
789
+ The result of type checking can be retrieved from `status.typeChecking`.
790
+ The presence of `status.typeChecking` indicates the completion of type checking,
791
+ and an empty `status.typeChecking` means that no errors were detected.
792
+
793
+ For example, given the following policy definition:
794
+ -->
795
+ ### 类型检查
796
+
797
+ 创建或更新策略定义时,验证过程将解析它包含的表达式,在发现错误时报告语法错误并拒绝该定义。
798
+ 之后,引用的变量将根据 ` spec.matchConstraints ` 的匹配类型检查类型错误,包括缺少字段和类型混淆。
799
+ 类型检查的结果可以从 ` status.typeChecking ` 中获得。
800
+ ` status.typeChecking ` 的存在表示类型检查已完成,而空的 ` status.typeChecking ` 表示未发现错误。
801
+
802
+ 例如,给定以下策略定义:
803
+
804
+ ``` yaml
805
+ apiVersion : admissionregistration.k8s.io/v1alpha1
806
+ kind : ValidatingAdmissionPolicy
807
+ metadata :
808
+ name : " deploy-replica-policy.example.com"
809
+ spec :
810
+ matchConstraints :
811
+ resourceRules :
812
+ - apiGroups : ["apps"]
813
+ apiVersions : ["v1"]
814
+ operations : ["CREATE", "UPDATE"]
815
+ resources : ["deployments"]
816
+ validations :
817
+ - expression : " object.replicas > 1" # 应该是 object.spec.replicas > 1
818
+ message : " must be replicated"
819
+ reason : Invalid
820
+ ` ` `
821
+
822
+ <!--
823
+ The status will yield the following information:
824
+ -->
825
+ status 字段将提供以下信息:
826
+
827
+ ` ` ` yaml
828
+ status :
829
+ typeChecking :
830
+ expressionWarnings :
831
+ - fieldRef : spec.validations[0].expression
832
+ warning : |-
833
+ apps/v1, Kind=Deployment: ERROR: <input>:1:7: undefined field 'replicas'
834
+ | object.replicas > 1
835
+ | ......^
836
+ ` ` `
837
+
838
+ <!--
839
+ If multiple resources are matched in ` spec.matchConstraints`, all of matched resources will be checked against.
840
+ For example, the following policy definition
841
+ -->
842
+ 如果在 `spec.matchConstraints` 中匹配了多个资源,则所有匹配的资源都将进行检查。
843
+ 例如,以下策略定义:
844
+
845
+ ` ` ` yaml
846
+ apiVersion: admissionregistration.k8s.io/v1alpha1
847
+ kind: ValidatingAdmissionPolicy
848
+ metadata:
849
+ name: "replica-policy.example.com"
850
+ spec:
851
+ matchConstraints:
852
+ resourceRules:
853
+ - apiGroups: ["apps"]
854
+ apiVersions: ["v1"]
855
+ operations: ["CREATE", "UPDATE"]
856
+ resources: ["deployments","replicasets"]
857
+ validations:
858
+ - expression: "object.replicas > 1" # 应该是 object.spec.replicas > 1
859
+ message: "must be replicated"
860
+ reason: Invalid
861
+ ` ` `
862
+
863
+ <!--
864
+ will have multiple types and type checking result of each type in the warning message.
865
+ -->
866
+ 将具有多个类型,并在警告消息中提供每种类型的类型检查结果。
867
+
868
+ ` ` ` yaml
869
+ status:
870
+ typeChecking:
871
+ expressionWarnings:
872
+ - fieldRef: spec.validations[0].expression
873
+ warning: |-
874
+ apps/v1, Kind=Deployment: ERROR: <input>:1:7: undefined field 'replicas'
875
+ | object.replicas > 1
876
+ | ......^
877
+ apps/v1, Kind=ReplicaSet: ERROR: <input>:1:7: undefined field 'replicas'
878
+ | object.replicas > 1
879
+ | ......^
880
+ ` ` `
881
+ <!--
882
+ Type Checking has the following limitation :
883
+
884
+ - No wildcard matching. If `spec.matchConstraints.resourceRules` contains `"*"` in any of `apiGroups`, `apiVersions` or `resources`,
885
+ the types that `"*"` matches will not be checked.
886
+ - The number of matched types is limited to 10. This is to prevent a policy that manually specifying too many types.
887
+ to consume excessive computing resources. In the order of ascending group, version, and then resource, 11th combination and beyond are ignored.
888
+ - Type Checking does not affect the policy behavior in any way. Even if the type checking detects errors, the policy will continue
889
+ to evaluate. If errors do occur during evaluate, the failure policy will decide its outcome.
890
+ - Type Checking does not apply to CRDs, including matched CRD types and reference of paramKind. The support for CRDs will come in future release.
891
+ -->
892
+ 类型检查具有以下限制:
893
+
894
+ - 没有通配符匹配。
895
+ 如果 `spec.matchConstraints.resourceRules` 中的任何一个 `apiGroups``、apiVersions`
896
+ 或 `resources` 包含 "\*",则不会检查与 "\*" 匹配的类型。
897
+ - 匹配的类型数量最多为 10 种。这是为了防止手动指定过多类型的策略消耗过多计算资源。
898
+ 按升序处理组、版本,然后是资源,忽略第 11 个及其之后的组合。
899
+ - 类型检查不会以任何方式影响策略行为。即使类型检查检测到错误,策略也将继续评估。
900
+ 如果在评估期间出现错误,则失败策略将决定其结果。
901
+ - 类型检查不适用于 CRD(自定义资源定义),包括匹配的 CRD 类型和 paramKind 的引用。
902
+ 对 CRD 的支持将在未来发布中推出。
0 commit comments