Skip to content

Commit 5c9f95a

Browse files
authored
Merge pull request #4681 from viveksb007/main
fixed#4680 wrapping last retryable err with timeout err
2 parents cd4514c + fde35ec commit 5c9f95a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pkg/runtime/retry_utils.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package runtime
22

33
import (
4-
"k8s.io/apimachinery/pkg/util/wait"
4+
"fmt"
55
"time"
6+
7+
"k8s.io/apimachinery/pkg/util/wait"
68
)
79

810
// RetryImmediateOnError tries to run fn every interval until it succeeds, a non-retryable error occurs or the timeout is reached.
11+
// If the timeout is reached while retrying, the returned error wraps both the timeout and the last retryable error.
912
func RetryImmediateOnError(interval time.Duration, timeout time.Duration, retryable func(error) bool, fn func() error) error {
10-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
13+
var lastRetryableErr error
14+
err := wait.PollImmediate(interval, timeout, func() (bool, error) {
1115
err := fn()
1216
if err != nil {
1317
if retryable(err) {
18+
lastRetryableErr = err
1419
return false, nil
1520
}
1621
return false, err
1722
}
1823
return true, nil
1924
})
25+
if wait.Interrupted(err) && lastRetryableErr != nil {
26+
return fmt.Errorf("%w: %v", err, lastRetryableErr)
27+
}
28+
return err
2029
}

pkg/runtime/retry_utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func Test_RetryImmediateOnError(t *testing.T) {
6969
// may race with the next tick, so count can be 3 or 4.
7070
wantMinCount: 3,
7171
wantMaxCount: 4,
72-
wantErr: errors.New("timed out waiting for the condition"),
72+
wantErr: errors.New("timed out waiting for the condition: retryable"),
7373
},
7474
{
7575
name: "retry 4 times before success",
@@ -95,7 +95,7 @@ func Test_RetryImmediateOnError(t *testing.T) {
9595
// may race with the next tick, so count can be 3 or 4.
9696
wantMinCount: 3,
9797
wantMaxCount: 4,
98-
wantErr: errors.New("timed out waiting for the condition"),
98+
wantErr: errors.New("timed out waiting for the condition: retryable"),
9999
},
100100
}
101101
for _, tt := range tests {

0 commit comments

Comments
 (0)