在 Kubernetes 中什么是 deletecollection?

In Kubernetes what is deletecollection?

列出 K8s 中的所有 API 资源时,您会得到:

$ kubectl api-resources -owide
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND                             VERBS
bindings                                                                      true         Binding                          [create]
componentstatuses                 cs                                          false        ComponentStatus                  [get list]
configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]
endpoints                         ep                                          true         Endpoints                        [create delete deletecollection get list patch update watch]
events                            ev                                          true         Event                            [create delete deletecollection get list patch update watch]
limitranges                       limits                                      true         LimitRange                       [create delete deletecollection get list patch update watch]
namespaces                        ns                                          false        Namespace                        [create delete get list patch update watch]
nodes                             no                                          false        Node                             [create delete deletecollection get list patch update watch]
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim            [create delete deletecollection get list patch update watch]
persistentvolumes                 pv                                          false        PersistentVolume                 [create delete deletecollection get list patch update watch]
pods                              po                                          true         Pod                              [create delete deletecollection get list patch update watch]
podtemplates                                                                  true         PodTemplate                      [create delete deletecollection get list patch update watch]
replicationcontrollers            rc                                          true         ReplicationController            [create delete deletecollection get list patch update watch]
resourcequotas                    quota                                       true         ResourceQuota                    [create delete deletecollection get list patch update watch]
secrets                                                                       true         Secret                           [create delete deletecollection get list patch update watch]
serviceaccounts                   sa                                          true         ServiceAccount                   [create delete deletecollection get list patch update watch]
services                          svc                                         true         Service                          [create delete get list patch update watch]
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration     [create delete deletecollection get list patch update watch]
... etc ...

许多人列出了听起来很有用的动词 deletecollection,但我不能 运行 例如

$ kubectl deletecollection
Error: unknown command "deletecollection" for "kubectl"
Run 'kubectl --help' for usage.
unknown command "deletecollection" for "kubectl"

我也无法在文档中找到它,除非它出现在上面的 api-resources 输出中或作为动词提及。

有没有办法删除收藏?

这听起来比我通常最终会做的 grep/awk/xargs 序列更好,如果它确实按照我认为应该做的那样。即删除某种类型的所有 pods。

DeleteCollection 它不是 kubectl 命令参数。
当 RBAC 处于活动状态时,它使用动词来定义您对 class kubernetes 对象的访问类型。 DeleteCollection 是 RBAC 角色定义中使用的动词,用于授权或不授权删除同类对象,如 pods 或部署或服务。

使用动词的 yaml 角色定义示例。

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-admin
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list","delete", "deletecollection"] 




delete 动词是指删除单个资源,例如单个 Pod。 deletecollection 动词是指同时删除多个资源,例如使用标签或字段选择器删除多个 Pods 或命名空间中的所有 Pods。

从 API 文档中举一些例子:

  1. delete a single PodDELETE /api/v1/namespaces/{namespace}/pods/{name}
  2. delete multiple Pods(或deletecollection):
    1. 命名空间 DELETE /api/v1/namespaces/{namespace}/pods
    2. 中的所有 pods
    3. 名称空间中的所有 pods 匹配给定的标签选择器:DELETE /api/v1/namespaces/{namespace}/pods?labelSelector=someLabel%3dsomeValue

关于 kubectl:您不能使用 kubectl 显式调用 deletecollection

相反,kubectl 将根据您调用 kubectl delete 的方式自行推断是使用 delete 还是 deletecollection。当删除单个源 (kubectl delete pod $POD_NAME) 时,kubectl 将使用 delete 调用,当使用标签选择器或简单地删除所有 Pods (kubectl delete pods -l $LABEL=$VALUEkubectl delete pods --all ),它将使用 deletecollection 动词。