@@ -26,7 +26,6 @@ import (
26
26
v1 "k8s.io/api/core/v1"
27
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
- "k8s.io/apimachinery/pkg/util/wait"
30
29
clientset "k8s.io/client-go/kubernetes"
31
30
"k8s.io/klog/v2"
32
31
"k8s.io/kubernetes/test/e2e/framework"
@@ -60,19 +59,29 @@ func WaitForJobPodsSucceeded(ctx context.Context, c clientset.Interface, ns, job
60
59
61
60
// waitForJobPodsInPhase wait for all pods for the Job named JobName in namespace ns to be in a given phase.
62
61
func waitForJobPodsInPhase (ctx context.Context , c clientset.Interface , ns , jobName string , expectedCount int32 , phase v1.PodPhase , timeout time.Duration ) error {
63
- return wait .PollUntilContextTimeout (ctx , framework .Poll , timeout , false , func (ctx context.Context ) (bool , error ) {
64
- pods , err := GetJobPods (ctx , c , ns , jobName )
65
- if err != nil {
66
- return false , err
67
- }
62
+ get := func (ctx context.Context ) (* v1.PodList , error ) {
63
+ return GetJobPods (ctx , c , ns , jobName )
64
+ }
65
+ match := func (pods * v1.PodList ) (func () string , error ) {
68
66
count := int32 (0 )
69
67
for _ , p := range pods .Items {
70
68
if p .Status .Phase == phase {
71
69
count ++
72
70
}
73
71
}
74
- return count == expectedCount , nil
75
- })
72
+ if count == expectedCount {
73
+ return nil , nil
74
+ }
75
+ return func () string {
76
+ return fmt .Sprintf ("job %q expected %d pods in %q phase, but got %d:\n %s" ,
77
+ klog .KRef (ns , jobName ), expectedCount , phase , count , format .Object (pods , 1 ))
78
+ }, nil
79
+ }
80
+ return framework .Gomega ().
81
+ Eventually (ctx , framework .HandleRetry (get )).
82
+ WithPolling (framework .Poll ).
83
+ WithTimeout (timeout ).
84
+ Should (framework .MakeMatcher (match ))
76
85
}
77
86
78
87
// WaitForJobComplete uses c to wait for completions to complete for the Job jobName in namespace ns.
@@ -196,14 +205,18 @@ func WaitForJobFinish(ctx context.Context, c clientset.Interface, ns, jobName st
196
205
197
206
// WaitForJobFinishWithTimeout uses c to wait for the Job jobName in namespace ns to finish (either Failed or Complete).
198
207
func WaitForJobFinishWithTimeout (ctx context.Context , c clientset.Interface , ns , jobName string , timeout time.Duration ) error {
199
- return wait .PollUntilContextTimeout (ctx , framework .Poll , timeout , true , func (ctx context.Context ) (bool , error ) {
200
- curr , err := c .BatchV1 ().Jobs (ns ).Get (ctx , jobName , metav1.GetOptions {})
201
- if err != nil {
202
- return false , err
203
- }
204
-
205
- return isJobFinished (curr ), nil
206
- })
208
+ return framework .Gomega ().
209
+ Eventually (ctx , framework .GetObject (c .BatchV1 ().Jobs (ns ).Get , jobName , metav1.GetOptions {})).
210
+ WithPolling (framework .Poll ).
211
+ WithTimeout (timeout ).
212
+ Should (framework .MakeMatcher (func (job * batchv1.Job ) (func () string , error ) {
213
+ if isJobFinished (job ) {
214
+ return nil , nil
215
+ }
216
+ return func () string {
217
+ return fmt .Sprintf ("expected job %q to be finished\n %s" , klog .KObj (job ), format .Object (job , 1 ))
218
+ }, nil
219
+ }))
207
220
}
208
221
209
222
func isJobFinished (j * batchv1.Job ) bool {
@@ -230,32 +243,33 @@ func isConditionTrue(j *batchv1.Job, condition batchv1.JobConditionType) bool {
230
243
231
244
// WaitForJobGone uses c to wait for up to timeout for the Job named jobName in namespace ns to be removed.
232
245
func WaitForJobGone (ctx context.Context , c clientset.Interface , ns , jobName string , timeout time.Duration ) error {
233
- return wait .PollUntilContextTimeout (ctx , framework .Poll , timeout , false , func (ctx context.Context ) (bool , error ) {
234
- _ , err := c .BatchV1 ().Jobs (ns ).Get (ctx , jobName , metav1.GetOptions {})
235
- if apierrors .IsNotFound (err ) {
236
- return true , nil
237
- }
238
- return false , err
239
- })
246
+ return framework .Gomega ().
247
+ Eventually (ctx , func (ctx context.Context ) error {
248
+ _ , err := framework .GetObject (c .BatchV1 ().Jobs (ns ).Get , jobName , metav1.GetOptions {})(ctx )
249
+ return err
250
+ }).
251
+ WithPolling (framework .Poll ).
252
+ WithTimeout (timeout ).
253
+ Should (gomega .MatchError (apierrors .IsNotFound , fmt .Sprintf ("that expected job %q to be gone" , klog .KRef (ns , jobName ))))
240
254
}
241
255
242
256
// WaitForAllJobPodsGone waits for all pods for the Job named jobName in namespace ns
243
257
// to be deleted.
244
258
func WaitForAllJobPodsGone (ctx context.Context , c clientset.Interface , ns , jobName string ) error {
245
- return wait .PollUntilContextTimeout (ctx , framework .Poll , JobTimeout , true , func (ctx context.Context ) (bool , error ) {
246
- pods , err := GetJobPods (ctx , c , ns , jobName )
247
- if err != nil {
248
- return false , err
249
- }
250
- return len (pods .Items ) == 0 , nil
251
- })
259
+ get := func (ctx context.Context ) (* v1.PodList , error ) {
260
+ return GetJobPods (ctx , c , ns , jobName )
261
+ }
262
+ return framework .Gomega ().
263
+ Eventually (ctx , framework .HandleRetry (get )).
264
+ WithPolling (framework .Poll ).
265
+ WithTimeout (JobTimeout ).
266
+ Should (gomega .HaveField ("Items" , gomega .BeEmpty ()))
252
267
}
253
268
254
- // WaitForJobState waits for a job to be matched to the given condition.
255
- // The condition callback may use gomega.StopTrying to abort early.
269
+ // WaitForJobState waits for a job to be matched to the given state function.
256
270
func WaitForJobState (ctx context.Context , c clientset.Interface , ns , jobName string , timeout time.Duration , state JobState ) error {
257
271
return framework .Gomega ().
258
- Eventually (ctx , framework .RetryNotFound ( framework . GetObject (c .BatchV1 ().Jobs (ns ).Get , jobName , metav1.GetOptions {}) )).
272
+ Eventually (ctx , framework .GetObject (c .BatchV1 ().Jobs (ns ).Get , jobName , metav1.GetOptions {})).
259
273
WithTimeout (timeout ).
260
274
Should (framework .MakeMatcher (func (job * batchv1.Job ) (func () string , error ) {
261
275
matches := state (job )
0 commit comments