[zh-cn]sync custom-resource-definitions.md

Signed-off-by: xin.li <xin.li@daocloud.io>
pull/49051/head
xin.li 2024-12-12 22:26:32 +08:00
parent 83469fc63a
commit 1eab8cdb3d
1 changed files with 74 additions and 31 deletions

View File

@ -118,7 +118,7 @@ metadata:
# 名字必需与下面的 spec 字段匹配,并且格式为 '<名称的复数形式>.<组名>' # 名字必需与下面的 spec 字段匹配,并且格式为 '<名称的复数形式>.<组名>'
name: crontabs.stable.example.com name: crontabs.stable.example.com
spec: spec:
# 组名称,用于 REST API: /apis/<组>/<版本> # 组名称,用于 REST API/apis/<组>/<版本>
group: stable.example.com group: stable.example.com
# 列举此 CustomResourceDefinition 所支持的版本 # 列举此 CustomResourceDefinition 所支持的版本
versions: versions:
@ -182,12 +182,11 @@ You can watch the `Established` condition of your CustomResourceDefinition
to be true or watch the discovery information of the API server for your to be true or watch the discovery information of the API server for your
resource to show up. resource to show up.
--> -->
此端点 URL 自此可以用来创建和管理定制对象。对象的 `kind` 将是来自你上面创建时 此端点 URL 自此可以用来创建和管理定制对象。对象的 `kind`
所用的 spec 中指定的 `CronTab` 将是来自你上面创建时所用的规约中指定的 `CronTab`
创建端点的操作可能需要几秒钟。你可以监测你的 CustomResourceDefinition 的 创建端点的操作可能需要几秒钟。你可以监测你的 CustomResourceDefinition 的
`Established` 状况变为 true或者监测 API 服务器的发现信息等待你的资源出现在 `Established` 状况变为 true或者监测 API 服务器的发现信息等待你的资源出现在那里。
那里。
<!-- <!--
## Create custom objects ## Create custom objects
@ -254,8 +253,8 @@ the singular or plural forms defined in the CRD, as well as any short names.
You can also view the raw YAML data: You can also view the raw YAML data:
--> -->
使用 kubectl 时,资源名称是大小写不敏感的,而且你既可以使用 CRD 中所定义的单数 使用 kubectl 时,资源名称是大小写不敏感的,而且你既可以使用 CRD
形式或复数形式,也可以使用其短名称: 中所定义的单数形式或复数形式,也可以使用其短名称:
```shell ```shell
kubectl get ct -o yaml kubectl get ct -o yaml
@ -335,12 +334,12 @@ CustomResourceDefinition, the structural schema was optional.
CustomResource 对象在定制字段中保存结构化的数据,这些字段和内置的字段 CustomResource 对象在定制字段中保存结构化的数据,这些字段和内置的字段
`apiVersion`、`kind` 和 `metadata` 等一起存储,不过内置的字段都会被 API `apiVersion`、`kind` 和 `metadata` 等一起存储,不过内置的字段都会被 API
服务器隐式完成合法性检查。有了 [OpenAPI v3.0 检查](#validation) 服务器隐式完成合法性检查。有了 [OpenAPI v3.0 检查](#validation)能力之后,
能力之后,你可以设置一个模式Schema在创建和更新定制对象时这一模式会被用来 你可以设置一个模式Schema在创建和更新定制对象时这一模式会被用来
对对象内容进行合法性检查。参阅下文了解这类模式的细节和局限性。 对对象内容进行合法性检查。参阅下文了解这类模式的细节和局限性。
`apiextensions.k8s.io/v1` 版本中CustomResourceDefinition 的这一结构化模式 `apiextensions.k8s.io/v1` 版本中CustomResourceDefinition
定义是必需的。 的这一结构化模式定义是必需的。
在 CustomResourceDefinition 的 beta 版本中,结构化模式定义是可选的。 在 CustomResourceDefinition 的 beta 版本中,结构化模式定义是可选的。
<!-- <!--
@ -375,7 +374,7 @@ A structural schema is an [OpenAPI v3.0 validation schema](#validation) which:
<!-- <!--
Non-structural example 1: Non-structural example 1:
--> -->
非结构化的例 1: 非结构化的例 1
```none ```none
allOf: allOf:
@ -616,8 +615,8 @@ to clients, `kubectl` also checks for unknown fields and rejects those objects w
would be sent to the API server. would be sent to the API server.
--> -->
本例中通过 `--validate=false` 命令行选项 关闭了客户端的合法性检查以展示 API 服务器的行为, 本例中通过 `--validate=false` 命令行选项 关闭了客户端的合法性检查以展示 API 服务器的行为,
因为 [OpenAPI 合法性检查模式也会发布到](#publish-validation-schema-in-openapi) 因为 [OpenAPI 合法性检查模式也会发布到](#publish-validation-schema-in-openapi)客户端,
客户端,`kubectl` 也会检查未知的字段并在对象被发送到 API `kubectl` 也会检查未知的字段并在对象被发送到 API
服务器之前就拒绝它们。 服务器之前就拒绝它们。
<!-- <!--
@ -632,8 +631,8 @@ For example:
#### 控制剪裁 {#controlling-pruning} #### 控制剪裁 {#controlling-pruning}
默认情况下,定制资源的所有版本中的所有未规定的字段都会被剪裁掉。 默认情况下,定制资源的所有版本中的所有未规定的字段都会被剪裁掉。
通过在结构化的 OpenAPI v3 [检查模式定义](#specifying-a-structural-schema) 通过在结构化的 OpenAPI v3 [检查模式定义](#specifying-a-structural-schema)中为特定字段的子树添加
中为特定字段的子树添加 `x-kubernetes-preserve-unknown-fields: true` 属性, `x-kubernetes-preserve-unknown-fields: true` 属性,
可以选择不对其执行剪裁操作。 可以选择不对其执行剪裁操作。
例如: 例如:
@ -755,6 +754,9 @@ anyOf:
... ...
``` ```
<!--
and
-->
```none ```none
@ -973,7 +975,7 @@ The `x-kubernetes-validations` extension can be used to validate custom resource
当[验证规则特性](#validation-rules)被启用并且 CustomResourceDefinition 当[验证规则特性](#validation-rules)被启用并且 CustomResourceDefinition
模式是一个[结构化的模式定义](#specifying-a-structural-schema)时, 模式是一个[结构化的模式定义](#specifying-a-structural-schema)时,
`x-kubernetes-validations` `x-kubernetes-validations`
扩展可以使用[通用表达式语言 (CEL)](https://github.com/google/cel-spec)表达式来验证定制资源。 扩展可以使用[通用表达式语言CEL](https://github.com/google/cel-spec)表达式来验证定制资源。
<!-- <!--
Refer to the [structural schemas](#specifying-a-structural-schema) section for other Refer to the [structural schemas](#specifying-a-structural-schema) section for other
@ -1296,7 +1298,7 @@ rules are supported.
<!-- <!--
For example: For example:
--> -->
例如: 例如
```none ```none
... ...
@ -1327,7 +1329,7 @@ For example:
<!-- <!--
will reject a request to create this custom resource: will reject a request to create this custom resource:
--> -->
将拒绝创建这个定制资源的请求: 将拒绝创建这个定制资源的请求
```yaml ```yaml
apiVersion: "stable.example.com/v1" apiVersion: "stable.example.com/v1"
@ -2732,11 +2734,6 @@ may also be used with field selectors when included in the `spec.versions[*].sel
{{< feature-state feature_gate_name="CustomResourceFieldSelectors" >}} {{< feature-state feature_gate_name="CustomResourceFieldSelectors" >}}
<!-- <!--
For Kubernetes {{< skew currentVersion >}} the ability to define field selectors for
custom resources is available by default (enabled by default since Kubernetes v1.31);
you can disable it for your cluster by turning off the `CustomResourceFieldSelectors`
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/).
The `spec.versions[*].selectableFields` field of a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} may be used to The `spec.versions[*].selectableFields` field of a {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} may be used to
declare which other fields in a custom resource may be used in field selectors declare which other fields in a custom resource may be used in field selectors
with the feature of `CustomResourceFieldSelectors` with the feature of `CustomResourceFieldSelectors`
@ -2746,11 +2743,6 @@ selectable fields.
Save the CustomResourceDefinition to `shirt-resource-definition.yaml`: Save the CustomResourceDefinition to `shirt-resource-definition.yaml`:
--> -->
在 Kubernetes {{< skew currentVersion >}} 中,
自定义资源的字段选择器功能默认启用(自 Kubernetes v1.31 起默认开启)。
如果你想禁用此功能,可以通过关闭 `CustomResourceFieldSelectors`
[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/) 实现。
CustomResourceDefinition 的 `spec.versions[*].selectableFields` CustomResourceDefinition 的 `spec.versions[*].selectableFields`
字段可用于声明自定义资源中的哪些其他字段可在字段选择器中使用。 字段可用于声明自定义资源中的哪些其他字段可在字段选择器中使用。
这一功能依赖于 `CustomResourceFieldSelectors` 这一功能依赖于 `CustomResourceFieldSelectors`
@ -2987,6 +2979,59 @@ Save the CustomResourceDefinition to `resourcedefinition.yaml`:
将此 CustomResourceDefinition 保存到 `resourcedefinition.yaml` 文件: 将此 CustomResourceDefinition 保存到 `resourcedefinition.yaml` 文件:
<!--
```yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
status:
type: object
properties:
replicas:
type: integer
labelSelector:
type: string
# subresources describes the subresources for custom resources.
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
specReplicasPath: .spec.replicas
# statusReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Replicas.
statusReplicasPath: .status.replicas
# labelSelectorPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Selector.
labelSelectorPath: .status.labelSelector
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
```
-->
```yaml ```yaml
apiVersion: apiextensions.k8s.io/v1 apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition kind: CustomResourceDefinition
@ -3239,9 +3284,7 @@ crontabs/my-new-cron-object 3s
* Serve [multiple versions](/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/) of a * Serve [multiple versions](/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/) of a
CustomResourceDefinition. CustomResourceDefinition.
--> -->
* 阅读了解[定制资源](/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources/) * 阅读了解[定制资源](/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources/)
* 参阅 [CustomResourceDefinition](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#customresourcedefinition-v1-apiextensions-k8s-io) * 参阅 [CustomResourceDefinition](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#customresourcedefinition-v1-apiextensions-k8s-io)
* 参阅支持 CustomResourceDefinition 的[多个版本](/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/) * 参阅支持 CustomResourceDefinition 的[多个版本](/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/)