@@ -233,25 +233,10 @@ Over HTTP, Kubernetes supports JSON and Protobuf wire encodings.
233
233
234
234
通过 HTTP,Kubernetes 支持 JSON 和 Protobuf 网络编码格式。
235
235
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
-
251
236
<!--
252
237
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.
255
240
256
241
The Kubernetes API implements standard HTTP content type negotiation: passing an
257
242
`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
260
245
appropriately.
261
246
-->
262
247
默认情况下,Kubernetes 使用 ` application/json ` 媒体类型以 [ JSON 序列化] ( #json-encoding ) 返回对象。
263
- 虽然 JSON 是默认类型,但客户端可以请求更高效的二进制
248
+ 虽然 JSON 是默认类型,但客户端可以用 YAML 请求响应,或使用更高效的二进制
264
249
[ Protobuf 表示] ( #protobuf-encoding ) ,以便在大规模环境中获得更好的性能。
265
250
266
251
Kubernetes API 实现了标准的 HTTP 内容类型协商:
@@ -298,24 +283,44 @@ Kubernetes API 默认使用 [JSON](https://www.json.org/json-en.html) 来编码
298
283
299
284
```
300
285
GET /api/v1/pods
301
- ---
286
+ ```
287
+
288
+ <!--
289
+ ```
302
290
200 OK
303
291
Content-Type: application/json
304
292
305
293
… JSON encoded collection of Pods (PodList object)
306
294
```
295
+ -->
296
+ ```
297
+ 200 OK
298
+ Content-Type: application/json
299
+
300
+ … JSON 编码的 Pod 集合(PodList 对象)
301
+ ```
307
302
308
303
<!--
309
304
1. Create a pod by sending JSON to the server, requesting a JSON response.
310
305
-->
311
306
2 . 通过向服务器发送 JSON 并请求 JSON 响应来创建 Pod。
312
307
308
+ <!--
313
309
```
314
310
POST /api/v1/namespaces/test/pods
315
311
Content-Type: application/json
316
312
Accept: application/json
317
313
… 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
+ ```
319
324
200 OK
320
325
Content-Type: application/json
321
326
@@ -326,20 +331,93 @@ Kubernetes API 默认使用 [JSON](https://www.json.org/json-en.html) 来编码
326
331
}
327
332
```
328
333
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
+
329
404
<!--
330
405
### Kubernetes Protobuf encoding {#protobuf-encoding}
331
406
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).
336
413
-->
337
414
### Kubernetes Protobuf 编码 {#protobuf-encoding}
338
415
339
- Kubernetes 使用封套形式来对 Protobuf 响应进行编码。
416
+ Kubernetes 使用封套形式来对 [ Protobuf] ( https://protobuf.dev/ ) 响应进行编码。
340
417
封套外层由 4 个字节的特殊数字开头,便于从磁盘文件或 etcd 中辩识 Protobuf
341
- 格式的(而不是 JSON)数据。
342
- 接下来存放的是 Protobuf 编码的封套消息,其中描述下层对象的编码和类型,最后才是对象本身。
418
+ 格式的(而不是 JSON)数据。这个 4 字节的特殊数字后跟一个 Protobuf 编码的封套消息,
419
+ 此消息描述了下层对象的编码和类型。在 Protobuf 封套消息中,内部对象数据使用 Unknown 的
420
+ ` raw ` 字段进行记录(有关细节参见 [ IDL] ( ##protobuf-encoding-idl ) )。
343
421
344
422
<!--
345
423
For example:
@@ -354,25 +432,45 @@ For example:
354
432
```
355
433
GET /api/v1/pods
356
434
Accept: application/vnd.kubernetes.protobuf
357
- ---
435
+ ```
436
+
437
+ <!--
438
+ ```
358
439
200 OK
359
440
Content-Type: application/vnd.kubernetes.protobuf
360
441
361
442
… JSON encoded collection of Pods (PodList object)
362
443
```
444
+ -->
445
+ ```
446
+ 200 OK
447
+ Content-Type: application/vnd.kubernetes.protobuf
448
+
449
+ … JSON 编码的 Pod 集合(PodList 对象)
450
+ ```
363
451
364
452
<!--
365
453
1. Create a pod by sending Protobuf encoded data to the server, but request a response
366
454
in JSON.
367
455
-->
368
456
2 . 通过向服务器发送 Protobuf 编码的数据创建 Pod,但请求以 JSON 形式接收响应:
369
457
458
+ <!--
370
459
```
371
460
POST /api/v1/namespaces/test/pods
372
461
Content-Type: application/vnd.kubernetes.protobuf
373
462
Accept: application/json
374
463
… 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
+ ```
376
474
200 OK
377
475
Content-Type: application/json
378
476
@@ -611,7 +709,7 @@ this is called a `Reflector` and is located in the `k8s.io/client-go/tools/cache
611
709
(在 Go 客户端库中,这称为 ` 反射器(Reflector) ` ,位于 ` k8s.io/client-go/tools/cache ` 包中。)
612
710
613
711
<!--
614
- ### Watch bookmarks
712
+ ### Watch bookmarks {#watch-bookmarks}
615
713
616
714
To mitigate the impact of short history window, the Kubernetes API provides a watch
617
715
event named `BOOKMARK`. It is a special kind of event to mark that all changes up
@@ -1070,14 +1168,14 @@ items:
1070
1168
namespace : kube-system
1071
1169
` ` `
1072
1170
1171
+ {{< note >}}
1073
1172
<!--
1074
1173
Keep in mind that the Kubernetes API does not have a ` kind` named `List`.
1075
1174
1076
1175
`kind : List` is a client-side, internal implementation detail for processing
1077
1176
collections that might be of different kinds of object. Avoid depending on
1078
1177
`kind : List` in automation or other code.
1079
1178
-->
1080
- {{< note >}}
1081
1179
请记住,Kubernetes API 没有名为 `List` 的 `kind`。
1082
1180
1083
1181
`kind : List` 是一个客户端内部实现细节,用于处理可能属于不同类别的对象的集合。
@@ -1366,8 +1464,8 @@ information about warnings and the Kubernetes API, see the blog article
1366
1464
-->
1367
1465
` Warn `
1368
1466
:(默认值)使 API 服务器成功处理请求,并向客户端发送告警信息。告警信息通过 ` Warning: ` 响应头发送,
1369
- 并为每个未知字段或重复字段添加一条告警信息。有关告警和相关的 Kubernetes API 的信息,
1370
- 可参阅博文[ 告警:增加实用告警功能] ( /zh-cn/blog/2020/09/03/warnings/ ) 。
1467
+ 并为每个未知字段或重复字段添加一条告警信息。有关告警和相关的 Kubernetes API 的信息,
1468
+ 可参阅博文[ 告警:增加实用告警功能] ( /zh-cn/blog/2020/09/03/warnings/ ) 。
1371
1469
1372
1470
<!--
1373
1471
: The API server rejects the request with a 400 Bad Request error when it
@@ -1377,7 +1475,7 @@ detected.
1377
1475
-->
1378
1476
` Strict `
1379
1477
: API 服务器检测到任何未知字段或重复字段时,拒绝处理请求并返回 400 Bad Request 错误。
1380
- 来自 API 服务器的响应消息列出了 API 检测到的所有未知字段或重复字段。
1478
+ 来自 API 服务器的响应消息列出了 API 检测到的所有未知字段或重复字段。
1381
1479
1382
1480
<!--
1383
1481
The field validation level is set by the `fieldValidation` query parameter.
@@ -1429,6 +1527,7 @@ kubectl 默认的校验设置是 `--validate=true` ,这意味着执行严格
1429
1527
当 kubectl 无法连接到启用字段校验的 API 服务器(Kubernetes 1.27 之前的 API 服务器)时,
1430
1528
将回退到使用客户端的字段校验。
1431
1529
客户端校验将在 kubectl 未来版本中被完全删除。
1530
+
1432
1531
{{< note >}}
1433
1532
<!--
1434
1533
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
1508
1607
此外,准入 Webhook 还可以设置[ 配置对象] (/zh-cn/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#validatingwebhook-v1-admissionregistration-k8s-io)
1509
1608
的 ` sideEffects ` 字段为 ` None ` ,借此声明它们没有副作用。
1510
1609
1610
+ {{< note >}}
1511
1611
<!--
1512
1612
If a webhook actually does have side effects, then the `sideEffects` field should be
1513
1613
set to "NoneOnDryRun". That change is appropriate provided that the webhook is also
1514
1614
be modified to understand the `DryRun` field in AdmissionReview, and to prevent side
1515
1615
effects on any request marked as dry runs.
1516
1616
-->
1517
- {{< note >}}
1518
1617
如果 webhook 确实有副作用,则应该将 ` sideEffects ` 字段设置为 “NoneOnDryRun”。
1519
1618
如果还修改了 webhook 以理解 AdmissionReview 中的 DryRun 字段,
1520
1619
并防止对标记为试运行的任何请求产生副作用,则该更改是适当的。
@@ -1920,12 +2019,10 @@ on the operation you request, and on the value of `resourceVersion`. If you set
1920
2019
API 服务器根据你请求的操作和 `resourceVersion` 的值对 `resourceVersion` 参数进行不同的解释。
1921
2020
如果你设置 `resourceVersionMatch` 那么这也会影响匹配发生的方式。
1922
2021
1923
-
1924
2022
<!--
1925
2023
# ## Semantics for **get** and **list**
1926
2024
1927
2025
For **get** and **list**, the semantics of `resourceVersion` are :
1928
-
1929
2026
-->
1930
2027
# ## **get** 和 **list** 语义 {#semantics-for-get-and-list}
1931
2028
@@ -1981,6 +2078,7 @@ This table explains the behavior of **list** requests with various combinations
1981
2078
设置 `resourceVersionMatch` 参数而不设置 `resourceVersion` 参数是不合法的。
1982
2079
1983
2080
下表解释了具有各种 `resourceVersion` 和 `resourceVersionMatch` 组合的 **list** 请求的行为:
2081
+
1984
2082
<!--
1985
2083
| resourceVersionMatch param | paging params | resourceVersion not set | resourceVersion="0" | resourceVersion="{value other than 0}" |
1986
2084
|---------------------------------------|-------------------------------|-----------------------|-------------------------------------------|----------------------------------------|
@@ -2009,11 +2107,11 @@ This table explains the behavior of **list** requests with various combinations
2009
2107
2010
2108
{{< /table >}}
2011
2109
2110
+ {{< note >}}
2012
2111
<!--
2013
2112
If your cluster's API server does not honor the `resourceVersionMatch` parameter,
2014
2113
the behavior is the same as if you did not set it.
2015
2114
-->
2016
- {{< note >}}
2017
2115
如果你的集群的 API 服务器不支持 `resourceVersionMatch` 参数,
2018
2116
则行为与你未设置它时相同。
2019
2117
{{< /note >}}
@@ -2065,7 +2163,6 @@ Not older than
2065
2163
` resourceVersion` , but does not make any guarantee about the `.metadata.resourceVersion` of any
2066
2164
of the items in that collection.
2067
2165
-->
2068
-
2069
2166
不老于指定版本
2070
2167
: 返回数据至少与提供的 `resourceVersion` 一样新。
2071
2168
最新的可用数据是首选,但可以提供不早于提供的 `resourceVersion` 的任何数据。
@@ -2086,7 +2183,6 @@ Continue Token, Exact
2086
2183
tokens_ are responsible for keeping track of the initially provided resource version for all paginated
2087
2184
**list** calls after the initial paginated **list**.
2088
2185
-->
2089
-
2090
2186
精确匹配
2091
2187
: 以提供的确切资源版本返回数据。如果提供的 `resourceVersion` 不可用,
2092
2188
则服务器以 HTTP 410 “Gone” 响应。对于对支持 `resourceVersionMatch` 参数的服务器的 **list** 请求,
@@ -2123,7 +2219,6 @@ verify that the collection's `.metadata.resourceVersion` matches
2123
2219
the requested `resourceVersion`, and handle the case where it does not. For
2124
2220
example, the client might fall back to a request with `limit` set.
2125
2221
-->
2126
-
2127
2222
当使用 `resourceVersionMatch=NotOlderThan` 并设置了限制时,
2128
2223
客户端必须处理 HTTP 410 “Gone” 响应。
2129
2224
例如,客户端可能会使用更新的 `resourceVersion` 重试或回退到 `resourceVersion=""`。
@@ -2152,7 +2247,6 @@ For watch, the semantics of resource version are:
2152
2247
2153
2248
{{< /table >}}
2154
2249
-->
2155
-
2156
2250
{{< table caption="watch 操作的 resourceVersion 设置" >}}
2157
2251
2158
2252
| resourceVersion 未设置 | resourceVersion="0" | resourceVersion="\<非零值\>" |
0 commit comments