From 0fe51a89a2af8e48e18501e4729c4764be2af17e Mon Sep 17 00:00:00 2001 From: windsonsea Date: Wed, 24 Apr 2024 09:38:08 +0800 Subject: [PATCH] [zh] Add trans to api-extension/custom-resources.md --- .../api-extension/custom-resources.md | 73 +++++++++++++++++++ .../shirt-resource-definition.yaml | 36 +++++++++ 2 files changed, 109 insertions(+) create mode 100644 content/zh-cn/examples/customresourcedefinition/shirt-resource-definition.yaml diff --git a/content/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources.md b/content/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources.md index 1cfb815c48..dad59ea99d 100644 --- a/content/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources.md +++ b/content/zh-cn/docs/concepts/extend-kubernetes/api-extension/custom-resources.md @@ -592,6 +592,79 @@ Kubernetes [客户端库](/zh-cn/docs/reference/using-api/client-libraries/)可 - 使用 [Kubernetes 客户端生成工具](https://github.com/kubernetes/code-generator)所生成的客户端。 生成客户端的工作有些难度,不过某些项目可能会随着 CRD 或聚合 API 一起提供一个客户端。 + +## 定制资源字段选择算符 {#custom-resource-field-selectors} + +[字段选择算符](/zh-cn/docs/concepts/overview/working-with-objects/field-selectors/)允许客户端根据一个或多个资源字段的值选择定制资源。 + + +所有定制资源都支持 `metadata.name` 和 `metadata.namespace` 字段选择算符。 + +当 {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} +中声明的字段包含在 {{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} +的 `spec.versions[*].selectableFields` 字段中时,也可以与字段选择算符一起使用。 + + +### 定制资源的可选择字段 {#crd-selectable-fields} + +{{< feature-state feature_gate_name="CustomResourceFieldSelectors" >}} + + +你需要启用 `CustomResourceFieldSelectors` +[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/) +来使用此行为,然后将其应用到集群中的所有 CustomResourceDefinitions。 + +{{< glossary_tooltip term_id="CustomResourceDefinition" text="CustomResourceDefinition" >}} +的 `spec.versions[*].selectableFields` 字段可以用来声明可以在字段选择算符中使用定制资源中的其他哪些字段。 +以下示例将 `.spec.color` 和 `.spec.size` 字段添加为可选择字段。 + +{{% code_sample file="customresourcedefinition/shirt-resource-definition.yaml" %}} + + +字段选择算符随后可用于仅获取 `color` 为 `blue` 的资源: + +```shell +kubectl get shirts.stable.example.com --field-selector spec.color=blue +``` + + +输出应该是: + +``` +NAME COLOR SIZE +example1 blue S +example2 blue M +``` + ## {{% heading "whatsnext" %}}