Delete all kubernetes resources using client-go?

7/9/2020

Is it possible to delete all resources with specific label using client go? The version of client-go is 0.17.3

I see in the client that there is a field for every resource type but they have different interfaces.

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
}

Is it possible to do something like this (got it from here):

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

But using the client-go?

-- aliench0
client-go
go
kubernetes

1 Answer

7/9/2020

Yes it is possible to do anything that kubectl does on your own, using client-go. After all, kubectl uses client-go itself.

You can look at the kubectl source code to see how it does the following:

  1. Get a list of api resources that support the delete verb. See the api-resources command implementation for how to do this.
  2. For each resource found, get a list of objects that have the label and value you are looking for. Check out kubectl's get command implementation for how to do this
  3. Delete each object matching the criteria. See kubectl's delete command implementation for this.

    1: https://github.com/kubernetes/kubectl

    2: https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/apiresources/apiresources.go

    3: https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/get/get.go

    4: https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/delete/delete.go

-- Brian Pursley
Source: StackOverflow