Skip to content

Commit 9ba42a5

Browse files
authored
Merge pull request kubernetes#128521 from carlory/framework-job
improve the code in test/e2e/framework/job/wait.go
2 parents fa0979c + f78c903 commit 9ba42a5

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

test/e2e/framework/job/wait.go

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
v1 "k8s.io/api/core/v1"
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29-
"k8s.io/apimachinery/pkg/util/wait"
3029
clientset "k8s.io/client-go/kubernetes"
3130
"k8s.io/klog/v2"
3231
"k8s.io/kubernetes/test/e2e/framework"
@@ -60,19 +59,29 @@ func WaitForJobPodsSucceeded(ctx context.Context, c clientset.Interface, ns, job
6059

6160
// waitForJobPodsInPhase wait for all pods for the Job named JobName in namespace ns to be in a given phase.
6261
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) {
6866
count := int32(0)
6967
for _, p := range pods.Items {
7068
if p.Status.Phase == phase {
7169
count++
7270
}
7371
}
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))
7685
}
7786

7887
// 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
196205

197206
// WaitForJobFinishWithTimeout uses c to wait for the Job jobName in namespace ns to finish (either Failed or Complete).
198207
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+
}))
207220
}
208221

209222
func isJobFinished(j *batchv1.Job) bool {
@@ -230,32 +243,33 @@ func isConditionTrue(j *batchv1.Job, condition batchv1.JobConditionType) bool {
230243

231244
// WaitForJobGone uses c to wait for up to timeout for the Job named jobName in namespace ns to be removed.
232245
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))))
240254
}
241255

242256
// WaitForAllJobPodsGone waits for all pods for the Job named jobName in namespace ns
243257
// to be deleted.
244258
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()))
252267
}
253268

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.
256270
func WaitForJobState(ctx context.Context, c clientset.Interface, ns, jobName string, timeout time.Duration, state JobState) error {
257271
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{})).
259273
WithTimeout(timeout).
260274
Should(framework.MakeMatcher(func(job *batchv1.Job) (func() string, error) {
261275
matches := state(job)

0 commit comments

Comments
 (0)