Skip to content

Commit 5a50fa4

Browse files
authored
Merge pull request #48490 from windsonsea/apiept
[zh] Sync using-api/api-concepts.md
2 parents a1ef093 + a3c10c7 commit 5a50fa4

File tree

1 file changed

+136
-42
lines changed

1 file changed

+136
-42
lines changed

content/zh-cn/docs/reference/using-api/api-concepts.md

Lines changed: 136 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,10 @@ Over HTTP, Kubernetes supports JSON and Protobuf wire encodings.
233233

234234
通过 HTTP,Kubernetes 支持 JSON 和 Protobuf 网络编码格式。
235235

236-
{{% note %}}
237-
<!--
238-
Although YAML is widely used to define Kubernetes manifests locally, Kubernetes does not
239-
support the [`application/yaml`](https://www.rfc-editor.org/rfc/rfc9512.html) media type
240-
for API operations.
241-
242-
All JSON documents are valid YAML, so you can also use a JSON API response anywhere that is
243-
expecting a YAML input.
244-
-->
245-
虽然 YAML 被广泛用于本地定义 Kubernetes 清单,但 Kubernetes 不支持
246-
[`application/yaml`](https://www.rfc-editor.org/rfc/rfc9512.html) 媒体类型用于 API 操作。
247-
248-
所有的 JSON 文档都是有效的 YAML,因此你也可以在所有期望输入 YAML 的地方使用 JSON API 响应。
249-
{{% /note %}}
250-
251236
<!--
252237
By default, Kubernetes returns objects in [JSON serialization](#json-encoding), using the
253-
`application/json` media type. Although JSON is the default, clients may request the more
254-
efficient binary [Protobuf representation](#protobuf-encoding) for better performance at scale.
238+
`application/json` media type. Although JSON is the default, clients may request a response in
239+
YAML, or use the more efficient binary [Protobuf representation](#protobuf-encoding) for better performance at scale.
255240
256241
The Kubernetes API implements standard HTTP content type negotiation: passing an
257242
`Accept` header with a `GET` call will request that the server tries to return
@@ -260,7 +245,7 @@ the server for a `PUT` or `POST` request, you must set the `Content-Type` reques
260245
appropriately.
261246
-->
262247
默认情况下,Kubernetes 使用 `application/json` 媒体类型以 [JSON 序列化](#json-encoding)返回对象。
263-
虽然 JSON 是默认类型,但客户端可以请求更高效的二进制
248+
虽然 JSON 是默认类型,但客户端可以用 YAML 请求响应,或使用更高效的二进制
264249
[Protobuf 表示](#protobuf-encoding),以便在大规模环境中获得更好的性能。
265250

266251
Kubernetes API 实现了标准的 HTTP 内容类型协商:
@@ -298,24 +283,44 @@ Kubernetes API 默认使用 [JSON](https://www.json.org/json-en.html) 来编码
298283

299284
```
300285
GET /api/v1/pods
301-
---
286+
```
287+
288+
<!--
289+
```
302290
200 OK
303291
Content-Type: application/json
304292
305293
… JSON encoded collection of Pods (PodList object)
306294
```
295+
-->
296+
```
297+
200 OK
298+
Content-Type: application/json
299+
300+
… JSON 编码的 Pod 集合(PodList 对象)
301+
```
307302

308303
<!--
309304
1. Create a pod by sending JSON to the server, requesting a JSON response.
310305
-->
311306
2. 通过向服务器发送 JSON 并请求 JSON 响应来创建 Pod。
312307

308+
<!--
313309
```
314310
POST /api/v1/namespaces/test/pods
315311
Content-Type: application/json
316312
Accept: application/json
317313
… JSON encoded Pod object
318-
---
314+
```
315+
-->
316+
```
317+
POST /api/v1/namespaces/test/pods
318+
Content-Type: application/json
319+
Accept: application/json
320+
… JSON 编码的 Pod 对象
321+
```
322+
323+
```
319324
200 OK
320325
Content-Type: application/json
321326
@@ -326,20 +331,93 @@ Kubernetes API 默认使用 [JSON](https://www.json.org/json-en.html) 来编码
326331
}
327332
```
328333

334+
<!--
335+
### YAML resource encoding {#yaml-encoding}
336+
337+
Kubernetes also supports the [`application/yaml`](https://www.rfc-editor.org/rfc/rfc9512.html) media type for both requests and responses. [`YAML`](https://yaml.org/) can be used for defining Kubernetes manifests and API interactions.
338+
339+
For example:
340+
-->
341+
### YAML 资源编码 {#yaml-encoding}
342+
343+
Kubernetes 还支持 [`application/yaml`](https://www.rfc-editor.org/rfc/rfc9512.html)
344+
媒体类型用于请求和响应。[`YAML`](https://yaml.org/) 可用于定义 Kubernetes 清单和 API 交互。
345+
346+
例如:
347+
348+
<!--
349+
1. List all of the pods on a cluster in YAML format
350+
-->
351+
1. 以 YAML 格式列举集群上的所有 Pod:
352+
353+
```
354+
GET /api/v1/pods
355+
Accept: application/yaml
356+
```
357+
358+
<!--
359+
```
360+
200 OK
361+
Content-Type: application/yaml
362+
363+
… YAML encoded collection of Pods (PodList object)
364+
```
365+
-->
366+
```
367+
200 OK
368+
Content-Type: application/yaml
369+
370+
… YAML 编码的 Pod 集合(PodList 对象)
371+
```
372+
373+
<!--
374+
1. Create a pod by sending YAML-encoded data to the server, requesting a YAML response:
375+
-->
376+
2. 通过向服务器发送 YAML 编码的数据并请求 YAML 响应来创建 Pod:
377+
378+
<!--
379+
```
380+
POST /api/v1/namespaces/test/pods
381+
Content-Type: application/yaml
382+
Accept: application/yaml
383+
… YAML encoded Pod object
384+
```
385+
-->
386+
```
387+
POST /api/v1/namespaces/test/pods
388+
Content-Type: application/yaml
389+
Accept: application/yaml
390+
… YAML 编码的 Pod 对象
391+
```
392+
393+
```
394+
200 OK
395+
Content-Type: application/yaml
396+
397+
apiVersion: v1
398+
kind: Pod
399+
metadata:
400+
name: my-pod
401+
402+
```
403+
329404
<!--
330405
### Kubernetes Protobuf encoding {#protobuf-encoding}
331406
332-
Kubernetes uses an envelope wrapper to encode Protobuf responses. That wrapper starts
333-
with a 4 byte magic number to help identify content in disk or in etcd as Protobuf
334-
(as opposed to JSON), and then is followed by a Protobuf encoded wrapper message, which
335-
describes the encoding and type of the underlying object and then contains the object.
407+
Kubernetes uses an envelope wrapper to encode [Protobuf](https://protobuf.dev/) responses.
408+
That wrapper starts with a 4 byte magic number to help identify content in disk or in etcd as Protobuf
409+
(as opposed to JSON). The 4 byte magic number data is followed by a Protobuf encoded wrapper message, which
410+
describes the encoding and type of the underlying object. Within the Protobuf wrapper message,
411+
the inner object data is recorded using the `raw` field of Unknown (see the [IDL](##protobuf-encoding-idl)
412+
for more detail).
336413
-->
337414
### Kubernetes Protobuf 编码 {#protobuf-encoding}
338415

339-
Kubernetes 使用封套形式来对 Protobuf 响应进行编码。
416+
Kubernetes 使用封套形式来对 [Protobuf](https://protobuf.dev/) 响应进行编码。
340417
封套外层由 4 个字节的特殊数字开头,便于从磁盘文件或 etcd 中辩识 Protobuf
341-
格式的(而不是 JSON)数据。
342-
接下来存放的是 Protobuf 编码的封套消息,其中描述下层对象的编码和类型,最后才是对象本身。
418+
格式的(而不是 JSON)数据。这个 4 字节的特殊数字后跟一个 Protobuf 编码的封套消息,
419+
此消息描述了下层对象的编码和类型。在 Protobuf 封套消息中,内部对象数据使用 Unknown 的
420+
`raw` 字段进行记录(有关细节参见 [IDL](##protobuf-encoding-idl))。
343421

344422
<!--
345423
For example:
@@ -354,25 +432,45 @@ For example:
354432
```
355433
GET /api/v1/pods
356434
Accept: application/vnd.kubernetes.protobuf
357-
---
435+
```
436+
437+
<!--
438+
```
358439
200 OK
359440
Content-Type: application/vnd.kubernetes.protobuf
360441
361442
… JSON encoded collection of Pods (PodList object)
362443
```
444+
-->
445+
```
446+
200 OK
447+
Content-Type: application/vnd.kubernetes.protobuf
448+
449+
… JSON 编码的 Pod 集合(PodList 对象)
450+
```
363451

364452
<!--
365453
1. Create a pod by sending Protobuf encoded data to the server, but request a response
366454
in JSON.
367455
-->
368456
2. 通过向服务器发送 Protobuf 编码的数据创建 Pod,但请求以 JSON 形式接收响应:
369457

458+
<!--
370459
```
371460
POST /api/v1/namespaces/test/pods
372461
Content-Type: application/vnd.kubernetes.protobuf
373462
Accept: application/json
374463
… binary encoded Pod object
375-
---
464+
```
465+
-->
466+
```
467+
POST /api/v1/namespaces/test/pods
468+
Content-Type: application/vnd.kubernetes.protobuf
469+
Accept: application/json
470+
… 二进制编码的 Pod 对象
471+
```
472+
473+
```
376474
200 OK
377475
Content-Type: application/json
378476
@@ -611,7 +709,7 @@ this is called a `Reflector` and is located in the `k8s.io/client-go/tools/cache
611709
(在 Go 客户端库中,这称为 `反射器(Reflector)`,位于 `k8s.io/client-go/tools/cache` 包中。)
612710

613711
<!--
614-
### Watch bookmarks
712+
### Watch bookmarks {#watch-bookmarks}
615713
616714
To mitigate the impact of short history window, the Kubernetes API provides a watch
617715
event named `BOOKMARK`. It is a special kind of event to mark that all changes up
@@ -1070,14 +1168,14 @@ items:
10701168
namespace: kube-system
10711169
```
10721170
1171+
{{< note >}}
10731172
<!--
10741173
Keep in mind that the Kubernetes API does not have a `kind` named `List`.
10751174

10761175
`kind: List` is a client-side, internal implementation detail for processing
10771176
collections that might be of different kinds of object. Avoid depending on
10781177
`kind: List` in automation or other code.
10791178
-->
1080-
{{< note >}}
10811179
请记住,Kubernetes API 没有名为 `List` 的 `kind`。
10821180

10831181
`kind: List` 是一个客户端内部实现细节,用于处理可能属于不同类别的对象的集合。
@@ -1366,8 +1464,8 @@ information about warnings and the Kubernetes API, see the blog article
13661464
-->
13671465
`Warn`
13681466
:(默认值)使 API 服务器成功处理请求,并向客户端发送告警信息。告警信息通过 `Warning:` 响应头发送,
1369-
并为每个未知字段或重复字段添加一条告警信息。有关告警和相关的 Kubernetes API 的信息,
1370-
可参阅博文[告警:增加实用告警功能](/zh-cn/blog/2020/09/03/warnings/)
1467+
并为每个未知字段或重复字段添加一条告警信息。有关告警和相关的 Kubernetes API 的信息,
1468+
可参阅博文[告警:增加实用告警功能](/zh-cn/blog/2020/09/03/warnings/)
13711469

13721470
<!--
13731471
: The API server rejects the request with a 400 Bad Request error when it
@@ -1377,7 +1475,7 @@ detected.
13771475
-->
13781476
`Strict`
13791477
: API 服务器检测到任何未知字段或重复字段时,拒绝处理请求并返回 400 Bad Request 错误。
1380-
来自 API 服务器的响应消息列出了 API 检测到的所有未知字段或重复字段。
1478+
来自 API 服务器的响应消息列出了 API 检测到的所有未知字段或重复字段。
13811479

13821480
<!--
13831481
The field validation level is set by the `fieldValidation` query parameter.
@@ -1429,6 +1527,7 @@ kubectl 默认的校验设置是 `--validate=true` ,这意味着执行严格
14291527
当 kubectl 无法连接到启用字段校验的 API 服务器(Kubernetes 1.27 之前的 API 服务器)时,
14301528
将回退到使用客户端的字段校验。
14311529
客户端校验将在 kubectl 未来版本中被完全删除。
1530+
14321531
{{< note >}}
14331532
<!--
14341533
Prior to Kubernetes 1.25 `kubectl --validate` was used to toggle client-side validation on or off as
@@ -1508,13 +1607,13 @@ that they do not have side effects, by setting their `sideEffects` field to `Non
15081607
此外,准入 Webhook 还可以设置[配置对象](/zh-cn/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#validatingwebhook-v1-admissionregistration-k8s-io)
15091608
`sideEffects` 字段为 `None`,借此声明它们没有副作用。
15101609

1610+
{{< note >}}
15111611
<!--
15121612
If a webhook actually does have side effects, then the `sideEffects` field should be
15131613
set to "NoneOnDryRun". That change is appropriate provided that the webhook is also
15141614
be modified to understand the `DryRun` field in AdmissionReview, and to prevent side
15151615
effects on any request marked as dry runs.
15161616
-->
1517-
{{< note >}}
15181617
如果 webhook 确实有副作用,则应该将 `sideEffects` 字段设置为 “NoneOnDryRun”。
15191618
如果还修改了 webhook 以理解 AdmissionReview 中的 DryRun 字段,
15201619
并防止对标记为试运行的任何请求产生副作用,则该更改是适当的。
@@ -1920,12 +2019,10 @@ on the operation you request, and on the value of `resourceVersion`. If you set
19202019
API 服务器根据你请求的操作和 `resourceVersion` 的值对 `resourceVersion` 参数进行不同的解释。
19212020
如果你设置 `resourceVersionMatch` 那么这也会影响匹配发生的方式。
19222021

1923-
19242022
<!--
19252023
### Semantics for **get** and **list**
19262024

19272025
For **get** and **list**, the semantics of `resourceVersion` are:
1928-
19292026
-->
19302027
### **get** 和 **list** 语义 {#semantics-for-get-and-list}
19312028

@@ -1981,6 +2078,7 @@ This table explains the behavior of **list** requests with various combinations
19812078
设置 `resourceVersionMatch` 参数而不设置 `resourceVersion` 参数是不合法的。
19822079

19832080
下表解释了具有各种 `resourceVersion` 和 `resourceVersionMatch` 组合的 **list** 请求的行为:
2081+
19842082
<!--
19852083
| resourceVersionMatch param | paging params | resourceVersion not set | resourceVersion="0" | resourceVersion="{value other than 0}" |
19862084
|---------------------------------------|-------------------------------|-----------------------|-------------------------------------------|----------------------------------------|
@@ -2009,11 +2107,11 @@ This table explains the behavior of **list** requests with various combinations
20092107

20102108
{{< /table >}}
20112109

2110+
{{< note >}}
20122111
<!--
20132112
If your cluster's API server does not honor the `resourceVersionMatch` parameter,
20142113
the behavior is the same as if you did not set it.
20152114
-->
2016-
{{< note >}}
20172115
如果你的集群的 API 服务器不支持 `resourceVersionMatch` 参数,
20182116
则行为与你未设置它时相同。
20192117
{{< /note >}}
@@ -2065,7 +2163,6 @@ Not older than
20652163
`resourceVersion`, but does not make any guarantee about the `.metadata.resourceVersion` of any
20662164
of the items in that collection.
20672165
-->
2068-
20692166
不老于指定版本
20702167
: 返回数据至少与提供的 `resourceVersion` 一样新。
20712168
最新的可用数据是首选,但可以提供不早于提供的 `resourceVersion` 的任何数据。
@@ -2086,7 +2183,6 @@ Continue Token, Exact
20862183
tokens_ are responsible for keeping track of the initially provided resource version for all paginated
20872184
**list** calls after the initial paginated **list**.
20882185
-->
2089-
20902186
精确匹配
20912187
: 以提供的确切资源版本返回数据。如果提供的 `resourceVersion` 不可用,
20922188
则服务器以 HTTP 410 “Gone” 响应。对于对支持 `resourceVersionMatch` 参数的服务器的 **list** 请求,
@@ -2123,7 +2219,6 @@ verify that the collection's `.metadata.resourceVersion` matches
21232219
the requested `resourceVersion`, and handle the case where it does not. For
21242220
example, the client might fall back to a request with `limit` set.
21252221
-->
2126-
21272222
当使用 `resourceVersionMatch=NotOlderThan` 并设置了限制时,
21282223
客户端必须处理 HTTP 410 “Gone” 响应。
21292224
例如,客户端可能会使用更新的 `resourceVersion` 重试或回退到 `resourceVersion=""`。
@@ -2152,7 +2247,6 @@ For watch, the semantics of resource version are:
21522247

21532248
{{< /table >}}
21542249
-->
2155-
21562250
{{< table caption="watch 操作的 resourceVersion 设置" >}}
21572251

21582252
| resourceVersion 未设置 | resourceVersion="0" | resourceVersion="\<非零值\>" |

0 commit comments

Comments
 (0)