简体   繁体   English

在 pod 列表的 Kube.netes 监视操作中,resourceVersion 是否保证单调递增?

[英]In a Kubernetes watch operation for list of pod, are resourceVersion guaranteed to be monotonically increasing?

From Kube.netes API Concepts > Efficient detection of changes :来自Kube.netes API Concepts > Efficient detection of changes :

When retrieving a collection of resources (either namespace or cluster scoped), the response from the API server contains a resourceVersion value.检索资源集合(命名空间或集群范围)时,来自 API 服务器的响应包含 resourceVersion 值。 The client can use that resourceVersion to initiate a watch against the API server.客户端可以使用该 resourceVersion 启动对 API 服务器的监视。 When you send a watch request, the API server responds with a stream of changes.当您发送监视请求时,API 服务器响应 stream 的更改。 These changes itemize the outcome of operations (such as create, delete, and update) that occurred after the resourceVersion you specified as a parameter to the watch request.这些更改详细列出了在您指定为监视请求的参数的 resourceVersion 之后发生的操作(例如创建、删除和更新)的结果。 The overall watch mechanism allows a client to fetch the current state and then subscribe to subsequent changes, without missing any events.整体监视机制允许客户端获取当前 state,然后订阅后续更改,而不会错过任何事件。

When I tried a watch operation (using kube.netes python client) I get a stream of kube.netes events, the events themselves do not have a resourceVersion , the object inside the event ( kind: Pod ) do have resourceVersion .当我尝试 watch 操作(使用 kube.netes python 客户端)时,我得到了一个 stream 的 kube.netes 事件,事件本身没有resourceVersion ,事件( kind: Pod )中的 object 确实有resourceVersion

from kubernetes import client,config,watch
config.load_kube_config(context='eks-prod')
v1 = client.CoreV1Api()
watcher = watch.Watch()
namespace = 'my-namespace'
last_resource_version=0
for i in  watcher.stream(v1.list_namespaced_pod, namespace, resource_version=last_resource_version, timeout_seconds=5):
   print(i['object'].metadata.resource_version)
   last_resource_version = i['object'].metadata.resource_version

The resource version are output in the order they are received and they are not monotonically increasing at least in the initial batch of events:资源版本按接收顺序为 output,至少在最初的事件批次中它们不是单调递增的:

380744163
380641499
380710458
380775853
380525082
381044514
380676150
380641735
380566799
380806984
380566585
380710721
378885571
380524650
380710218
380806798
373502190
380566206
381044372
380524615
380676624
380806573
380775663
380605904
380743917
380606153
380676388
380744368
380641258
380775416
380606397

But can I assume that if this watch is disconnected I can safely resume from the highest resource version I've seen?但是我可以假设如果这只手表断开连接,我可以安全地从我见过的最高资源版本恢复吗? In the above case, can I safely resume from 381044514 (the highest) without missing events?在上述情况下,我可以安全地从381044514 (最高)恢复而不会丢失事件吗?

From Resource Version Semantics来自资源版本语义

You must not assume resource versions are numeric or collatable.不能假设资源版本是数字的或可整理的。 API clients may only compare two resource versions for equality (this means that you must not compare resource versions for greater-than or less-than relationships). API 客户端只能比较两个资源版本是否相等(这意味着您不能比较资源版本的大于或小于关系)。

So in principle no you can't use the "highest" resource version because they are not really numeric or sortable.所以原则上你不能使用“最高”资源版本,因为它们不是真正的数字或可排序的。 The best you can do is use the latest resourceVersion that you received as is, verbatim.您能做的最好的事情就是逐字使用您收到的最新resourceVersion And be prepared to get a resource too old that you are supposed to handle by retrying without specifying a resource version , in that case you must also handle the case where you will likely receive some events more than once.并准备好让您应该通过重试而不指定资源版本来处理的resource too old ,在这种情况下,您还必须处理您可能会多次收到某些事件的情况。

This scenario where the resourceVersion in the last event received is not the actual latest/most recent is easily reproducible in EKS 1.21 where the initial response to the watch will return the events in more or less random order.在 EKS 1.21 中很容易重现上次收到的事件中的resourceVersion不是实际最新/最近的情况,其中对手表的初始响应将或多或少以随机顺序返回事件。 If I send two watch requests simultaneously I'll get the same 30 events but in different order.如果我同时发送两个监视请求,我将获得相同的 30 个事件,但顺序不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM