Kubernetes RBAC 动词:get without list 反之亦然?没有名单看?

Kubernetes RBAC verbs: get without list and vice versa? Watch without list?

虽然有很多关于 Kubernetes RBAC 的文档和示例以及不同资源的可用动词,但我找不到关于某些动词是否总是组合使用或是否有使用它们的用例的任何规则个别地。特别是,我想知道动词 get、list 和 watch。结合起来有什么用,尤其是不结合起来有什么用?

有趣的问题,这里有一些想法和实践中的用法示例。

实践中还有很多例子。例如,您可以通过浏览 kubectl describe clusterroles 检查默认的 ClusterRoles。要查看 kubectl 在幕后发出了哪些 API 请求,您可以增加日志的详细程度,例如 kubectl get pods -w -v 10.

get 但不是 list

您希望某人能够阅读他们知道名字的资源,但不能发现其他资源的存在。例如,允许 kubectl get mypod,但不允许 kubectl get pods.

示例:

  • system:node ClusterRole 对端点、PV 和 PVC 具有 get 但没有 list 权限。
  • system:coredns ClusterRole 在节点上具有 get 但没有 list 权限。
  • system:controller:expand-controller ClusterRole 对端点、机密和服务具有 get 但没有 list 权限。

list 但不是 get

允许 kubectl get pods 但不允许 kubectl get pod mypod。意义不大,因为用get得到的所有信息也包含在list中。然而,在实践中有一些用法。

示例:

  • system:kube-dns ClusterRole 对端点和服务具有 listwatch 权限,但没有 get .
  • system:controller:daemon-set-controller ClusterRoel 对节点有 listwatch 权限,但没有 get.
  • system:coredns ClusterRole 对端点、命名空间、Pods 和服务具有 listwatch 权限, 但不是 get.

getlist,但不是 watch

实际上,在大多数情况下,有 list 的地方也会有 watch。您可以剥夺某人的 watch 以减少 etcd 上的观察者数量。用户可以使用 kubectl get podskubectl get pods mypod,但不能使用 -w 选项。

如果 API 不支持 watch 操作,例如可选指标 APIs.

示例:

  • system:controller:persistent-volume-binder ClusterRole 对节点有 getlist 权限,但没有 watch

watch,但不是 getlist

关于用例,它没有多大意义,因为你可以用 getlist 得到的所有信息是也包含在 watch 中。我不知道这在实践中有任何具体用法。

但是,从技术上讲,这是可能的。例如,如果您对 Pods 具有 watch 权限,但没有 getlist ,你可以这样做:

✅ kubectl get --raw="/api/v1/watch/namespaces/default/pods"
✅ kubectl get --raw="/api/v1/watch/namespaces/default/pods/mypod"

并且有效。但是,这些 watch 端点已弃用,您应该改用带有 watch 参数的 list 端点。但这也有效:

✅ kubectl get --raw="/api/v1/namespaces/default/pods?watch=true"

但是,您不能像这样观看单个 Pod,因为 get 端点没有 watch 参数。所以,以下是无效的:

❌ kubectl get --raw="/api/v1/namespaces/default/pods/mypod?watch=true"

而且你根本无法使用 kubectl 查看资源。以下失败:

❌ kubectl get pods -w
❌ kubectl get pods mypod -w

因为kubectl在watch请求之前分别做了一个listget请求,最有可能获得资源的 resourceVersion,然后将其包含在随后的 watch 请求中。

注意:这意味着,如果您有 listwatch,那么 kubectl get pods -w 有效,但 kubectl get pods mypod -w 没有,如果你有 getwatch,那么 kubectl get pods mypod -w 有效但 kubectl get pods -w 无效。