delete.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2019 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. package pod
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/onsi/ginkgo"
  19. v1 "k8s.io/api/core/v1"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. clientset "k8s.io/client-go/kubernetes"
  22. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  23. )
  24. const (
  25. // PodDeleteTimeout is how long to wait for a pod to be deleted.
  26. PodDeleteTimeout = 5 * time.Minute
  27. )
  28. // DeletePodOrFail deletes the pod of the specified namespace and name.
  29. func DeletePodOrFail(c clientset.Interface, ns, name string) {
  30. ginkgo.By(fmt.Sprintf("Deleting pod %s in namespace %s", name, ns))
  31. err := c.CoreV1().Pods(ns).Delete(context.TODO(), name, nil)
  32. expectNoError(err, "failed to delete pod %s in namespace %s", name, ns)
  33. }
  34. // DeletePodWithWait deletes the passed-in pod and waits for the pod to be terminated. Resilient to the pod
  35. // not existing.
  36. func DeletePodWithWait(c clientset.Interface, pod *v1.Pod) error {
  37. if pod == nil {
  38. return nil
  39. }
  40. return DeletePodWithWaitByName(c, pod.GetName(), pod.GetNamespace())
  41. }
  42. // DeletePodWithWaitByName deletes the named and namespaced pod and waits for the pod to be terminated. Resilient to the pod
  43. // not existing.
  44. func DeletePodWithWaitByName(c clientset.Interface, podName, podNamespace string) error {
  45. e2elog.Logf("Deleting pod %q in namespace %q", podName, podNamespace)
  46. err := c.CoreV1().Pods(podNamespace).Delete(context.TODO(), podName, nil)
  47. if err != nil {
  48. if apierrors.IsNotFound(err) {
  49. return nil // assume pod was already deleted
  50. }
  51. return fmt.Errorf("pod Delete API error: %v", err)
  52. }
  53. e2elog.Logf("Wait up to %v for pod %q to be fully deleted", PodDeleteTimeout, podName)
  54. err = WaitForPodNotFoundInNamespace(c, podName, podNamespace, PodDeleteTimeout)
  55. if err != nil {
  56. return fmt.Errorf("pod %q was not deleted: %v", podName, err)
  57. }
  58. return nil
  59. }