@@ -505,7 +505,8 @@ anyOf:
505
505
` ` `
506
506
507
507
<!--
508
- Violations of the structural schema rules are reported in the ` NonStructural` condition in the CustomResourceDefinition.
508
+ Violations of the structural schema rules are reported in the ` NonStructural` condition in the
509
+ CustomResourceDefinition.
509
510
-->
510
511
如果违反了结构化模式规则,CustomResourceDefinition 的 `NonStructural`
511
512
状况中会包含报告信息。
@@ -1195,7 +1196,7 @@ For example:
1195
1196
required:
1196
1197
- minReplicas
1197
1198
- replicas
1198
- - maxReplicas
1199
+ - maxReplicas
1199
1200
` ` `
1200
1201
1201
1202
<!--
@@ -1225,7 +1226,7 @@ The CronTab "my-new-cron-object" is invalid:
1225
1226
```
1226
1227
1227
1228
<!--
1228
- `x-kubernetes-validations` could have multiple rules.
1229
+ `x-kubernetes-validations` could have multiple rules.
1229
1230
1230
1231
The `rule` under `x-kubernetes-validations` represents the expression which will be evaluated by CEL.
1231
1232
@@ -1244,8 +1245,8 @@ The CronTab "my-new-cron-object" is invalid:
1244
1245
```
1245
1246
1246
1247
<!--
1247
- Validation rules are compiled when CRDs are created/updated.
1248
- The request of CRDs create/update will fail if compilation of validation rules fail.
1248
+ Validation rules are compiled when CRDs are created/updated.
1249
+ The request of CRDs create/update will fail if compilation of validation rules fail.
1249
1250
Compilation process includes type checking as well.
1250
1251
-->
1251
1252
当 CRD 被创建/更新时,验证规则被编译。
@@ -1256,7 +1257,7 @@ Compilation process includes type checking as well.
1256
1257
The compilation failure:
1257
1258
1258
1259
- `no_matching_overload`: this function has no overload for the types of the arguments.
1259
-
1260
+
1260
1261
For example, a rule like `self == true` against a field of integer type will get error:
1261
1262
-->
1262
1263
编译失败:
@@ -1269,9 +1270,9 @@ The compilation failure:
1269
1270
Invalid value: apiextensions.ValidationRule{Rule:"self == true", Message:""}: compilation failed: ERROR: \< input>:1:6: found no matching overload for '_ ==_ ' applied to '(int, bool)'
1270
1271
```
1271
1272
1272
- <!--
1273
+ <!--
1273
1274
- `no_such_field`: does not contain the desired field.
1274
-
1275
+
1275
1276
For example, a rule like `self.nonExistingField > 0` against a non-existing field will return
1276
1277
the following error:
1277
1278
-->
@@ -1285,7 +1286,7 @@ The compilation failure:
1285
1286
1286
1287
<!--
1287
1288
- `invalid argument`: invalid argument to macros.
1288
-
1289
+
1289
1290
For example, a rule like `has(self)` will return error:
1290
1291
-->
1291
1292
- `invalid argument`:对宏的无效参数。
@@ -1633,6 +1634,64 @@ xref: [CEL types](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#
1633
1634
[ OpenAPI 类型] ( https://swagger.io/specification/#data-types ) 、
1634
1635
[ Kubernetes 结构化模式] ( /zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema ) 。
1635
1636
1637
+ <!--
1638
+ #### The messageExpression field
1639
+ -->
1640
+ #### ` messageExpression ` 字段 {#the-messageExpression-field}
1641
+
1642
+ <!--
1643
+ Similar to the `message` field, which defines the string reported for a validation rule failure,
1644
+ `messageExpression` allows you to use a CEL expression to construct the message string.
1645
+ This allows you to insert more descriptive information into the validation failure message.
1646
+ `messageExpression` must evaluate a string and may use the same variables that are available to the `rule`
1647
+ field. For example:
1648
+ -->
1649
+ ` message ` 字段定义因验证规则失败时提示的字符串,与它类似,
1650
+ ` messageExpression ` 允许你使用 CEL 表达式构造消息字符串。
1651
+ 这使你可以在验证失败消息中插入更详细的信息。` messageExpression `
1652
+ 必须计算为字符串,并且可以使用在 ` rule ` 字段中可用的变量。
1653
+ 例如:
1654
+
1655
+ ``` yaml
1656
+ x-kubernetes-validations :
1657
+ - rule : " self.x <= self.maxLimit"
1658
+ messageExpression : ' "x exceeded max limit of " + string(self.maxLimit)'
1659
+ ` ` `
1660
+
1661
+ <!--
1662
+ Keep in mind that CEL string concatenation (` +` operator) does not auto-cast to string. If
1663
+ you have a non-string scalar, use the `string(<value>)` function to cast the scalar to a string
1664
+ like shown in the above example.
1665
+ -->
1666
+ 请记住,CEL 字符串连接(`+` 运算符)不会自动转换为字符串。
1667
+ 如果你有一个非字符串标量,请使用 `string(<value>)` 函数将标量转换为字符串,如上例所示。
1668
+
1669
+ <!--
1670
+ ` messageExpression` must evaluate to a string, and this is checked while the CRD is being written. Note that it is possible
1671
+ to set `message` and `messageExpression` on the same rule, and if both are present, `messageExpression`
1672
+ will be used. However, if `messageExpression` evaluates to an error, the string defined in `message`
1673
+ will be used instead, and the `messageExpression` error will be logged. This fallback will also occur if
1674
+ the CEL expression defined in `messageExpression` generates an empty string, or a string containing line
1675
+ breaks.
1676
+ -->
1677
+ ` messageExpression` 必须计算为一个字符串,并且在编写 CRD 时进行检查。
1678
+ 请注意,可以在同一个规则上设置 `message` 和 `messageExpression`,如果两者都存在,则将使用 `messageExpression`。
1679
+ 但是,如果 `messageExpression` 计算出错,则将使用 `message` 中定义的字符串,而 `messageExpression` 的错误将被打印到日志。
1680
+ 如果在 `messageExpression` 中定义的 CEL 表达式产生一个空字符串或包含换行符的字符串,也会发生这种回退。
1681
+
1682
+ <!--
1683
+ If one of the above conditions are met and no `message` has been set, then the default validation failure
1684
+ message will be used instead.
1685
+ -->
1686
+ 如果满足上述条件之一且未设置 `message` 字段,则将使用默认的检查失败消息。
1687
+
1688
+ <!--
1689
+ ` messageExpression` is a CEL expression, so the restrictions listed in [Resource use by validation functions](#resource-use-by-validation-functions) apply. If evaluation halts due to resource constraints
1690
+ during `messageExpression` execution, then no further validation rules will be executed.
1691
+ -->
1692
+ ` messageExpression` 是一个 CEL 表达式,因此[验证函数的资源使用](#resource-use-by-validation-functions)中列出的限制也适用于它。
1693
+ 如果在 `messageExpression` 执行期间由于资源限制而导致计算停止,则不会执行进一步的检查规则。
1694
+
1636
1695
<!--
1637
1696
# ### Validation functions {#available-validation-functions}
1638
1697
-->
@@ -1749,7 +1808,7 @@ Here are some examples for transition rules:
1749
1808
<!--
1750
1809
# ### Resource use by validation functions
1751
1810
-->
1752
- # ### 验证函数的资源使用
1811
+ # ### 验证函数的资源使用 {#resource-use-by-validation-functions}
1753
1812
1754
1813
<!--
1755
1814
When you create or update a CustomResourceDefinition that uses validation rules,
@@ -1766,8 +1825,8 @@ too many instructions, execution of the rule will be halted, and an error will r
1766
1825
-->
1767
1826
运行时也使用类似的系统来观察解释器的行动。如果解释器执行了太多的指令,规则的执行将被停止,并且会产生一个错误。
1768
1827
<!--
1769
- Each CustomResourceDefinition is also allowed a certain amount of resources to finish executing all of
1770
- its validation rules. If the sum total of its rules are estimated at creation time to go over that limit,
1828
+ Each CustomResourceDefinition is also allowed a certain amount of resources to finish executing all of
1829
+ its validation rules. If the sum total of its rules are estimated at creation time to go over that limit,
1771
1830
then a validation error will also occur.
1772
1831
-->
1773
1832
每个 CustomResourceDefinition 也被允许有一定数量的资源来完成其所有验证规则的执行。
@@ -1798,7 +1857,7 @@ given, and this will happen for anything that can be iterated over (lists, maps,
1798
1857
如果没有给出 `foo` 的长度限制,成本系统总是假设最坏的情况,这将发生在任何可以被迭代的事物上(list、map 等)。
1799
1858
1800
1859
<!--
1801
- Because of this, it is considered best practice to put a limit via `maxItems`, `maxProperties`, and
1860
+ Because of this, it is considered best practice to put a limit via `maxItems`, `maxProperties`, and
1802
1861
` maxLength` for anything that will be processed in a validation rule in order to prevent validation
1803
1862
errors during cost estimation. For example, given this schema with one rule :
1804
1863
-->
@@ -1823,8 +1882,8 @@ then the API server rejects this rule on validation budget grounds with error:
1823
1882
API 服务器以验证预算为由拒绝该规则,并显示错误:
1824
1883
1825
1884
```
1826
- spec.validation.openAPIV3Schema.properties[ spec] .properties[ foo] .x-kubernetes-validations[ 0] .rule: Forbidden:
1827
- CEL rule exceeded budget by more than 100x (try simplifying the rule, or adding maxItems, maxProperties, and
1885
+ spec.validation.openAPIV3Schema.properties[ spec] .properties[ foo] .x-kubernetes-validations[ 0] .rule: Forbidden:
1886
+ CEL rule exceeded budget by more than 100x (try simplifying the rule, or adding maxItems, maxProperties, and
1828
1887
maxLength where arrays, maps, and strings are used)
1829
1888
```
1830
1889
@@ -1880,7 +1939,7 @@ openAPIV3Schema:
1880
1939
` ` `
1881
1940
1882
1941
<!--
1883
- If a list inside of a list has a validation rule that uses ` self.all`, that is significantly more expensive
1942
+ If a list inside of a list has a validation rule that uses ` self.all`, that is significantly more expensive
1884
1943
than a non-nested list with the same rule. A rule that would have been allowed on a non-nested list might need
1885
1944
lower limits set on both nested lists in order to be allowed. For example, even without having limits set,
1886
1945
the following rule is allowed :
@@ -2114,38 +2173,50 @@ default.
2114
2173
` baz` 字段则被完全剪裁掉,因为该字段是不可为空的,并且没有默认值设置。
2115
2174
2116
2175
<!--
2117
- # ## Publish Validation Schema in OpenAPI v2
2176
+ # ## Publish Validation Schema in OpenAPI
2118
2177
2119
2178
CustomResourceDefinition [OpenAPI v3 validation schemas](#validation) which are
2120
2179
[structural](#specifying-a-structural-schema) and [enable pruning](#field-pruning) are published
2121
- as part of the [OpenAPI v2 spec](/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)
2122
- from Kubernetes API server.
2180
+ as [OpenAPI v3](/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions) and
2181
+ OpenAPI v2 from Kubernetes API server. It is recommended to use the OpenAPI v3 document
2182
+ as it is a lossless representation of the CustomResourceDefinition OpenAPI v3 validation schema
2183
+ while OpenAPI v2 represents a lossy conversion.
2123
2184
2124
2185
The [kubectl](/docs/reference/kubectl/) command-line tool consumes the published schema to perform
2125
2186
client-side validation (`kubectl create` and `kubectl apply`), schema explanation (`kubectl explain`)
2126
2187
on custom resources. The published schema can be consumed for other purposes as well, like client generation or documentation.
2127
2188
-->
2128
- # ## 以 OpenAPI v2 形式发布合法性检查模式 {#publish-validation-schema-in-openapi-v2 }
2189
+ # ## 以 OpenAPI 形式发布合法性检查模式 {#publish-validation-schema-in-openapi}
2129
2190
2130
2191
CustomResourceDefinition 的[结构化的](#specifying-a-structural-schema)、
2131
2192
[启用了剪裁的](#field-pruning) [OpenAPI v3 合法性检查模式](#validation)会在
2132
2193
Kubernetes API 服务器上作为
2133
- [OpenAPI v2 规约](/zh-cn/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)的一部分发布出来。
2194
+ [OpenAPI 3](/zh-cn/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)
2195
+ 和 OpenAPI v2 发布出来。建议使用 OpenAPI v3 文档,因为它是 CustomResourceDefinition OpenAPI v3
2196
+ 验证模式的无损表示,而 OpenAPI v2 表示有损转换。
2134
2197
2135
2198
[kubectl](/zh-cn/docs/reference/kubectl/) 命令行工具会基于所发布的模式定义来执行客户端的合法性检查
2136
2199
(`kubectl create` 和 `kubectl apply`),为定制资源的模式定义提供解释(`kubectl explain`)。
2137
2200
所发布的模式还可被用于其他目的,例如生成客户端或者生成文档。
2138
2201
2139
2202
<!--
2140
- The OpenAPI v3 validation schema is converted to OpenAPI v2 schema, and
2141
- show up in `definitions` and `paths` fields in the
2203
+
2204
+ # ### Compatibility with OpenAPI V2
2205
+
2206
+ For compatibility with OpenAPI V2, the OpenAPI v3 validation schema performs a lossy conversion
2207
+ to the OpenAPI v2 schema. The schema show up in `definitions` and `paths` fields in the
2142
2208
[OpenAPI v2 spec](/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions).
2143
2209
2144
2210
The following modifications are applied during the conversion to keep backwards compatibility with
2145
2211
kubectl in previous 1.13 version. These modifications prevent kubectl from being over-strict and rejecting
2146
2212
valid OpenAPI schemas that it doesn't understand. The conversion won't modify the validation schema defined in CRD,
2147
2213
and therefore won't affect [validation](#validation) in the API server.
2148
2214
-->
2215
+ # ### Compatibility with OpenAPI V2
2216
+
2217
+ 为了与 OpenAPI V2 兼容,OpenAPI v3 验证模式会对 OpenAPI v2 模式进行有损转换。
2218
+ 该模式显示在 [OpenAPI v2 规范](/zh-cn/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)中的
2219
+ ` definitions` 和` paths` 字段中。
2149
2220
OpenAPI v3 合法性检查模式定义会被转换为 OpenAPI v2 模式定义,并出现在
2150
2221
[OpenAPI v2 规范](/zh-cn/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)的
2151
2222
` definitions` 和 `paths` 字段中。
@@ -2156,16 +2227,14 @@ OpenAPI v3 合法性检查模式定义会被转换为 OpenAPI v2 模式定义,
2156
2227
API 服务器中的[合法性检查](#validation)。
2157
2228
2158
2229
<!--
2159
- 1. The following fields are removed as they aren't supported by OpenAPI v2
2160
- (in future versions OpenAPI v3 will be used without these restrictions)
2230
+ 1. The following fields are removed as they aren't supported by OpenAPI v2.
2161
2231
2162
2232
- The fields `allOf`, `anyOf`, `oneOf` and `not` are removed
2163
2233
2164
2234
2. If `nullable : true` is set, we drop `type`, `nullable`, `items` and `properties` because OpenAPI v2 is
2165
2235
not able to express nullable. To avoid kubectl to reject good objects, this is necessary.
2166
2236
-->
2167
- 1. 以下字段会被移除,因为它们在 OpenAPI v2 中不支持(在将来版本中将使用 OpenAPI v3,
2168
- 因而不会有这些限制)
2237
+ 1. 以下字段会被移除,因为它们在 OpenAPI v2 中不支持。
2169
2238
2170
2239
- 字段 `allOf`、`anyOf`、`oneOf` 和 `not` 会被删除
2171
2240
0 commit comments