wait.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 job
  14. import (
  15. "context"
  16. "time"
  17. "k8s.io/api/core/v1"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. clientset "k8s.io/client-go/kubernetes"
  22. jobutil "k8s.io/kubernetes/pkg/controller/job"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. )
  25. // WaitForAllJobPodsRunning wait for all pods for the Job named JobName in namespace ns to become Running. Only use
  26. // when pods will run for a long time, or it will be racy.
  27. func WaitForAllJobPodsRunning(c clientset.Interface, ns, jobName string, parallelism int32) error {
  28. return wait.Poll(framework.Poll, JobTimeout, func() (bool, error) {
  29. pods, err := GetJobPods(c, ns, jobName)
  30. if err != nil {
  31. return false, err
  32. }
  33. count := int32(0)
  34. for _, p := range pods.Items {
  35. if p.Status.Phase == v1.PodRunning {
  36. count++
  37. }
  38. }
  39. return count == parallelism, nil
  40. })
  41. }
  42. // WaitForJobComplete uses c to wait for completions to complete for the Job jobName in namespace ns.
  43. func WaitForJobComplete(c clientset.Interface, ns, jobName string, completions int32) error {
  44. return wait.Poll(framework.Poll, JobTimeout, func() (bool, error) {
  45. curr, err := c.BatchV1().Jobs(ns).Get(context.TODO(), jobName, metav1.GetOptions{})
  46. if err != nil {
  47. return false, err
  48. }
  49. return curr.Status.Succeeded == completions, nil
  50. })
  51. }
  52. // WaitForJobFinish uses c to wait for the Job jobName in namespace ns to finish (either Failed or Complete).
  53. func WaitForJobFinish(c clientset.Interface, ns, jobName string) error {
  54. return wait.PollImmediate(framework.Poll, JobTimeout, func() (bool, error) {
  55. curr, err := c.BatchV1().Jobs(ns).Get(context.TODO(), jobName, metav1.GetOptions{})
  56. if err != nil {
  57. return false, err
  58. }
  59. return jobutil.IsJobFinished(curr), nil
  60. })
  61. }
  62. // WaitForJobGone uses c to wait for up to timeout for the Job named jobName in namespace ns to be removed.
  63. func WaitForJobGone(c clientset.Interface, ns, jobName string, timeout time.Duration) error {
  64. return wait.Poll(framework.Poll, timeout, func() (bool, error) {
  65. _, err := c.BatchV1().Jobs(ns).Get(context.TODO(), jobName, metav1.GetOptions{})
  66. if apierrors.IsNotFound(err) {
  67. return true, nil
  68. }
  69. return false, err
  70. })
  71. }
  72. // WaitForAllJobPodsGone waits for all pods for the Job named jobName in namespace ns
  73. // to be deleted.
  74. func WaitForAllJobPodsGone(c clientset.Interface, ns, jobName string) error {
  75. return wait.PollImmediate(framework.Poll, JobTimeout, func() (bool, error) {
  76. pods, err := GetJobPods(c, ns, jobName)
  77. if err != nil {
  78. return false, err
  79. }
  80. return len(pods.Items) == 0, nil
  81. })
  82. }