使用 client-go 删除所有 kubernetes 资源?

Delete all kubernetes resources using client-go?

是否可以使用 client go 删除所有具有特定标签的资源? client-go版本为0.17.3

我在客户端看到每个资源类型都有一个字段,但它们有不同的接口。

type Clientset struct {
    *discovery.DiscoveryClient
    admissionregistrationV1alpha1 *admissionregistrationv1alpha1.AdmissionregistrationV1alpha1Client
    admissionregistrationV1beta1  *admissionregistrationv1beta1.AdmissionregistrationV1beta1Client
    appsV1beta1                   *appsv1beta1.AppsV1beta1Client
    appsV1beta2                   *appsv1beta2.AppsV1beta2Client
    appsV1                        *appsv1.AppsV1Client
    auditregistrationV1alpha1     *auditregistrationv1alpha1.AuditregistrationV1alpha1Client
    authenticationV1              *authenticationv1.AuthenticationV1Client
    authenticationV1beta1         *authenticationv1beta1.AuthenticationV1beta1Client
    authorizationV1               *authorizationv1.AuthorizationV1Client
    authorizationV1beta1          *authorizationv1beta1.AuthorizationV1beta1Client
    autoscalingV1                 *autoscalingv1.AutoscalingV1Client
    autoscalingV2beta1            *autoscalingv2beta1.AutoscalingV2beta1Client
    autoscalingV2beta2            *autoscalingv2beta2.AutoscalingV2beta2Client
    batchV1                       *batchv1.BatchV1Client
    batchV1beta1                  *batchv1beta1.BatchV1beta1Client
    batchV2alpha1                 *batchv2alpha1.BatchV2alpha1Client
    certificatesV1beta1           *certificatesv1beta1.CertificatesV1beta1Client
    coordinationV1beta1           *coordinationv1beta1.CoordinationV1beta1Client
    coreV1                        *corev1.CoreV1Client
    eventsV1beta1                 *eventsv1beta1.EventsV1beta1Client
    extensionsV1beta1             *extensionsv1beta1.ExtensionsV1beta1Client
    networkingV1                  *networkingv1.NetworkingV1Client
    policyV1beta1                 *policyv1beta1.PolicyV1beta1Client
    rbacV1                        *rbacv1.RbacV1Client
    rbacV1beta1                   *rbacv1beta1.RbacV1beta1Client
    rbacV1alpha1                  *rbacv1alpha1.RbacV1alpha1Client
    schedulingV1alpha1            *schedulingv1alpha1.SchedulingV1alpha1Client
    schedulingV1beta1             *schedulingv1beta1.SchedulingV1beta1Client
    settingsV1alpha1              *settingsv1alpha1.SettingsV1alpha1Client
    storageV1beta1                *storagev1beta1.StorageV1beta1Client
    storageV1                     *storagev1.StorageV1Client
    storageV1alpha1               *storagev1alpha1.StorageV1alpha1Client
}

是否可以这样做(从获得):

kubectl delete "$(kubectl api-resources --verbs=delete -o name | tr "\n" "," | sed -e 's/,$//')" --all

但是用客户端去?

是的,您可以使用 client-go 自行完成 kubectl 所做的任何事情。毕竟kubectl自己用的是client-go

您可以查看 kubectl source code 以了解它如何执行以下操作:

  1. 获取支持删除动词的 api 资源列表。请参阅 api-resources command implementation 了解如何执行此操作。
  2. 对于找到的每个资源,获取具有您要查找的标签和值的对象列表。查看 kubectl's get command implementation 了解如何执行此操作
  3. 删除每个符合条件的对象。请参阅 kubectl's delete command implementation