...
1
16
17
18
19 package utils
20
21 import (
22 "context"
23 "fmt"
24
25 apierrors "k8s.io/apimachinery/pkg/api/errors"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/runtime/schema"
28 clientset "k8s.io/client-go/kubernetes"
29 appsinternal "k8s.io/kubernetes/pkg/apis/apps"
30 batchinternal "k8s.io/kubernetes/pkg/apis/batch"
31 api "k8s.io/kubernetes/pkg/apis/core"
32 extensionsinternal "k8s.io/kubernetes/pkg/apis/extensions"
33 )
34
35 func DeleteResource(c clientset.Interface, kind schema.GroupKind, namespace, name string, options metav1.DeleteOptions) error {
36 switch kind {
37 case api.Kind("Pod"):
38 return c.CoreV1().Pods(namespace).Delete(context.TODO(), name, options)
39 case api.Kind("ReplicationController"):
40 return c.CoreV1().ReplicationControllers(namespace).Delete(context.TODO(), name, options)
41 case extensionsinternal.Kind("ReplicaSet"), appsinternal.Kind("ReplicaSet"):
42 return c.AppsV1().ReplicaSets(namespace).Delete(context.TODO(), name, options)
43 case extensionsinternal.Kind("Deployment"), appsinternal.Kind("Deployment"):
44 return c.AppsV1().Deployments(namespace).Delete(context.TODO(), name, options)
45 case extensionsinternal.Kind("DaemonSet"):
46 return c.AppsV1().DaemonSets(namespace).Delete(context.TODO(), name, options)
47 case batchinternal.Kind("Job"):
48 return c.BatchV1().Jobs(namespace).Delete(context.TODO(), name, options)
49 case api.Kind("Secret"):
50 return c.CoreV1().Secrets(namespace).Delete(context.TODO(), name, options)
51 case api.Kind("ConfigMap"):
52 return c.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), name, options)
53 case api.Kind("Service"):
54 return c.CoreV1().Services(namespace).Delete(context.TODO(), name, options)
55 default:
56 return fmt.Errorf("unsupported kind when deleting: %v", kind)
57 }
58 }
59
60 func DeleteResourceWithRetries(c clientset.Interface, kind schema.GroupKind, namespace, name string, options metav1.DeleteOptions) error {
61 deleteFunc := func() (bool, error) {
62 err := DeleteResource(c, kind, namespace, name, options)
63 if err == nil || apierrors.IsNotFound(err) {
64 return true, nil
65 }
66 return false, fmt.Errorf("failed to delete object with non-retriable error: %v", err)
67 }
68 return RetryWithExponentialBackOff(deleteFunc)
69 }
70
View as plain text