delete_resources.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // TODO: Refactor common part of functions in this file for generic object kinds.
  14. package utils
  15. import (
  16. "fmt"
  17. apierrs "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. clientset "k8s.io/client-go/kubernetes"
  21. appsinternal "k8s.io/kubernetes/pkg/apis/apps"
  22. batchinternal "k8s.io/kubernetes/pkg/apis/batch"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. extensionsinternal "k8s.io/kubernetes/pkg/apis/extensions"
  25. )
  26. func deleteResource(c clientset.Interface, kind schema.GroupKind, namespace, name string, options *metav1.DeleteOptions) error {
  27. switch kind {
  28. case api.Kind("Pod"):
  29. return c.CoreV1().Pods(namespace).Delete(name, options)
  30. case api.Kind("ReplicationController"):
  31. return c.CoreV1().ReplicationControllers(namespace).Delete(name, options)
  32. case extensionsinternal.Kind("ReplicaSet"), appsinternal.Kind("ReplicaSet"):
  33. return c.AppsV1().ReplicaSets(namespace).Delete(name, options)
  34. case extensionsinternal.Kind("Deployment"), appsinternal.Kind("Deployment"):
  35. return c.AppsV1().Deployments(namespace).Delete(name, options)
  36. case extensionsinternal.Kind("DaemonSet"):
  37. return c.AppsV1().DaemonSets(namespace).Delete(name, options)
  38. case batchinternal.Kind("Job"):
  39. return c.BatchV1().Jobs(namespace).Delete(name, options)
  40. case api.Kind("Secret"):
  41. return c.CoreV1().Secrets(namespace).Delete(name, options)
  42. case api.Kind("ConfigMap"):
  43. return c.CoreV1().ConfigMaps(namespace).Delete(name, options)
  44. case api.Kind("Service"):
  45. return c.CoreV1().Services(namespace).Delete(name, options)
  46. default:
  47. return fmt.Errorf("Unsupported kind when deleting: %v", kind)
  48. }
  49. }
  50. func DeleteResourceWithRetries(c clientset.Interface, kind schema.GroupKind, namespace, name string, options *metav1.DeleteOptions) error {
  51. deleteFunc := func() (bool, error) {
  52. err := deleteResource(c, kind, namespace, name, options)
  53. if err == nil || apierrs.IsNotFound(err) {
  54. return true, nil
  55. }
  56. if IsRetryableAPIError(err) {
  57. return false, nil
  58. }
  59. return false, fmt.Errorf("Failed to delete object with non-retriable error: %v", err)
  60. }
  61. return RetryWithExponentialBackOff(deleteFunc)
  62. }