Skip to content

Commit d3c0cd5

Browse files
authored
Merge pull request #40661 from Zhuzhenghao/1.27/crd
[zh] sync 1.27 custom-resource-definitions
2 parents 032f828 + ffc1c91 commit d3c0cd5

File tree

1 file changed

+96
-27
lines changed

1 file changed

+96
-27
lines changed

content/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions.md

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ anyOf:
505505
```
506506
507507
<!--
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.
509510
-->
510511
如果违反了结构化模式规则,CustomResourceDefinition 的 `NonStructural`
511512
状况中会包含报告信息。
@@ -1195,7 +1196,7 @@ For example:
11951196
required:
11961197
- minReplicas
11971198
- replicas
1198-
- maxReplicas
1199+
- maxReplicas
11991200
```
12001201

12011202
<!--
@@ -1225,7 +1226,7 @@ The CronTab "my-new-cron-object" is invalid:
12251226
```
12261227
12271228
<!--
1228-
`x-kubernetes-validations` could have multiple rules.
1229+
`x-kubernetes-validations` could have multiple rules.
12291230
12301231
The `rule` under `x-kubernetes-validations` represents the expression which will be evaluated by CEL.
12311232
@@ -1244,8 +1245,8 @@ The CronTab "my-new-cron-object" is invalid:
12441245
```
12451246
12461247
<!--
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.
12491250
Compilation process includes type checking as well.
12501251
-->
12511252
当 CRD 被创建/更新时,验证规则被编译。
@@ -1256,7 +1257,7 @@ Compilation process includes type checking as well.
12561257
The compilation failure:
12571258
12581259
- `no_matching_overload`: this function has no overload for the types of the arguments.
1259-
1260+
12601261
For example, a rule like `self == true` against a field of integer type will get error:
12611262
-->
12621263
编译失败:
@@ -1269,9 +1270,9 @@ The compilation failure:
12691270
Invalid value: apiextensions.ValidationRule{Rule:"self == true", Message:""}: compilation failed: ERROR: \<input>:1:6: found no matching overload for '_==_' applied to '(int, bool)'
12701271
```
12711272
1272-
<!--
1273+
<!--
12731274
- `no_such_field`: does not contain the desired field.
1274-
1275+
12751276
For example, a rule like `self.nonExistingField > 0` against a non-existing field will return
12761277
the following error:
12771278
-->
@@ -1285,7 +1286,7 @@ The compilation failure:
12851286
12861287
<!--
12871288
- `invalid argument`: invalid argument to macros.
1288-
1289+
12891290
For example, a rule like `has(self)` will return error:
12901291
-->
12911292
- `invalid argument`:对宏的无效参数。
@@ -1633,6 +1634,64 @@ xref: [CEL types](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#
16331634
[OpenAPI 类型](https://swagger.io/specification/#data-types)
16341635
[Kubernetes 结构化模式](/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema)
16351636

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+
16361695
<!--
16371696
#### Validation functions {#available-validation-functions}
16381697
-->
@@ -1749,7 +1808,7 @@ Here are some examples for transition rules:
17491808
<!--
17501809
#### Resource use by validation functions
17511810
-->
1752-
#### 验证函数的资源使用
1811+
#### 验证函数的资源使用 {#resource-use-by-validation-functions}
17531812

17541813
<!--
17551814
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
17661825
-->
17671826
运行时也使用类似的系统来观察解释器的行动。如果解释器执行了太多的指令,规则的执行将被停止,并且会产生一个错误。
17681827
<!--
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,
17711830
then a validation error will also occur.
17721831
-->
17731832
每个 CustomResourceDefinition 也被允许有一定数量的资源来完成其所有验证规则的执行。
@@ -1798,7 +1857,7 @@ given, and this will happen for anything that can be iterated over (lists, maps,
17981857
如果没有给出 `foo` 的长度限制,成本系统总是假设最坏的情况,这将发生在任何可以被迭代的事物上(list、map 等)。
17991858

18001859
<!--
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
18021861
`maxLength` for anything that will be processed in a validation rule in order to prevent validation
18031862
errors during cost estimation. For example, given this schema with one rule:
18041863
-->
@@ -1823,8 +1882,8 @@ then the API server rejects this rule on validation budget grounds with error:
18231882
API 服务器以验证预算为由拒绝该规则,并显示错误:
18241883

18251884
```
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
18281887
maxLength where arrays, maps, and strings are used)
18291888
```
18301889
@@ -1880,7 +1939,7 @@ openAPIV3Schema:
18801939
```
18811940
18821941
<!--
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
18841943
than a non-nested list with the same rule. A rule that would have been allowed on a non-nested list might need
18851944
lower limits set on both nested lists in order to be allowed. For example, even without having limits set,
18861945
the following rule is allowed:
@@ -2114,38 +2173,50 @@ default.
21142173
`baz` 字段则被完全剪裁掉,因为该字段是不可为空的,并且没有默认值设置。
21152174

21162175
<!--
2117-
### Publish Validation Schema in OpenAPI v2
2176+
### Publish Validation Schema in OpenAPI
21182177

21192178
CustomResourceDefinition [OpenAPI v3 validation schemas](#validation) which are
21202179
[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.
21232184

21242185
The [kubectl](/docs/reference/kubectl/) command-line tool consumes the published schema to perform
21252186
client-side validation (`kubectl create` and `kubectl apply`), schema explanation (`kubectl explain`)
21262187
on custom resources. The published schema can be consumed for other purposes as well, like client generation or documentation.
21272188
-->
2128-
### 以 OpenAPI v2 形式发布合法性检查模式 {#publish-validation-schema-in-openapi-v2}
2189+
### 以 OpenAPI 形式发布合法性检查模式 {#publish-validation-schema-in-openapi}
21292190

21302191
CustomResourceDefinition 的[结构化的](#specifying-a-structural-schema)、
21312192
[启用了剪裁的](#field-pruning) [OpenAPI v3 合法性检查模式](#validation)会在
21322193
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 表示有损转换。
21342197

21352198
[kubectl](/zh-cn/docs/reference/kubectl/) 命令行工具会基于所发布的模式定义来执行客户端的合法性检查
21362199
(`kubectl create` 和 `kubectl apply`),为定制资源的模式定义提供解释(`kubectl explain`)。
21372200
所发布的模式还可被用于其他目的,例如生成客户端或者生成文档。
21382201

21392202
<!--
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
21422208
[OpenAPI v2 spec](/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions).
21432209

21442210
The following modifications are applied during the conversion to keep backwards compatibility with
21452211
kubectl in previous 1.13 version. These modifications prevent kubectl from being over-strict and rejecting
21462212
valid OpenAPI schemas that it doesn't understand. The conversion won't modify the validation schema defined in CRD,
21472213
and therefore won't affect [validation](#validation) in the API server.
21482214
-->
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` 字段中。
21492220
OpenAPI v3 合法性检查模式定义会被转换为 OpenAPI v2 模式定义,并出现在
21502221
[OpenAPI v2 规范](/zh-cn/docs/concepts/overview/kubernetes-api/#openapi-and-swagger-definitions)的
21512222
`definitions` 和 `paths` 字段中。
@@ -2156,16 +2227,14 @@ OpenAPI v3 合法性检查模式定义会被转换为 OpenAPI v2 模式定义,
21562227
API 服务器中的[合法性检查](#validation)。
21572228

21582229
<!--
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.
21612231

21622232
- The fields `allOf`, `anyOf`, `oneOf` and `not` are removed
21632233

21642234
2. If `nullable: true` is set, we drop `type`, `nullable`, `items` and `properties` because OpenAPI v2 is
21652235
not able to express nullable. To avoid kubectl to reject good objects, this is necessary.
21662236
-->
2167-
1. 以下字段会被移除,因为它们在 OpenAPI v2 中不支持(在将来版本中将使用 OpenAPI v3,
2168-
因而不会有这些限制)
2237+
1. 以下字段会被移除,因为它们在 OpenAPI v2 中不支持。
21692238

21702239
- 字段 `allOf`、`anyOf`、`oneOf` 和 `not` 会被删除
21712240

0 commit comments

Comments
 (0)