我们知道在k8s中List请求是很常见的一种操作,用于获取一种资源对象的列表,如pod的List方法用于获取k8s集群中的所有pod资源。
在深入理解List操作之前,我们需要了解resourceVersion这个参数。
在每k8s资源对象中都有一个字段resourceVersion用于标识当前资源的版本,可用于乐观并发控制和获取资源的方式。
在k8s中有三种操作方法支持resourceVersion参数。apiserver根据不同的请求方法和不同的参数,对resourceVersion
都有不同的解释。
| resourceVersion unset | resourceVersion=”0” | resourceVersion=”{value other than 0}” |
|---|---|---|
| Most Recent | Any | Not older than |
Most Recent: 从etcd中取数据
Any: 优先用最新的,但不保证一定是最新的
Not older than: 不低于某个版本号
从 v1.19开始,List方法开始支持resourceVersionMatch,如果你设置了resourceVersionMatch参数,将会决定apiserver如何解析resourceVersion
官方文档建议当在List请求中如果使用了resourceVersion应该带上resourceVersionMatch参数。当然也会处理好一些特殊情况apiserver会忽略resourceVersonMatch。
除非你有很强的一致性要求,建议使用resourceVersionMatch=NotOlderThan以获得更好的性能。
如果你设置了resourceVersionMatch而没设置resourceVersion将会是无效的。
resourceVersion与resourceVersionMatch的关系可以查看官方文档中的表格
1 | // staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go |
1 | // staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go |
REF: