diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go deleted file mode 100644 index bd25653d..00000000 --- a/pkg/retry/retry.go +++ /dev/null @@ -1,68 +0,0 @@ -package retry - -import ( - "fmt" - "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/util/retry" - "time" -) - -// AlwaysRetryFunc returns always true and thus indicates that always should be tried until the retrier hits its limit. -var AlwaysRetryFunc = func(err error) bool { - return true -} - -// TestableRetryFunc returns true if the returned error is a TestableRetrierError and indicates that an action should be tried until the retrier hits its limit. -var TestableRetryFunc = func(err error) bool { - _, ok := err.(*TestableRetrierError) - return ok -} - -// TestableRetrierError marks errors that indicate that a previously executed action should be retried with again. It must wrap an existing error. -type TestableRetrierError struct { - Err error -} - -// Error returns the error's string representation. -func (tre *TestableRetrierError) Error() string { - return tre.Err.Error() -} - -// OnError provides a K8s-way "retrier" mechanism. The value from retriable is used to indicate if workload should -// retried another time. Please see AlwaysRetryFunc() if a workload should always retried until a fixed threshold is -// reached. -func OnError(maxTries int, retriable func(error) bool, workload func() error) error { - return onError(maxTries, 3*time.Minute, retriable, workload) -} - -// OnErrorWithLimit provides a K8s-way "retrier" mechanism with a time limit as option. -func OnErrorWithLimit(limit time.Duration, retriable func(error) bool, workload func() error) error { - // Use a high integer here to avoid limit the cap with the steps. - return onError(9999999, limit, retriable, workload) -} - -func onError(maxTries int, limit time.Duration, retriable func(error) bool, workload func() error) error { - err := retry.OnError(wait.Backoff{ - Duration: 1500 * time.Millisecond, - Factor: 1.5, - Jitter: 0, - Steps: maxTries, - Cap: limit, - }, retriable, workload) - - if err != nil && retriable(err) { - return fmt.Errorf("the maximum number of retries was reached: %w", err) - } - return err -} - -// OnConflict provides a K8s-way "retrier" mechanism to avoid conflicts on resource updates. -func OnConflict(fn func() error) error { - return retry.RetryOnConflict(wait.Backoff{ - Duration: 1500 * time.Millisecond, - Factor: 1.5, - Jitter: 0, - Steps: 9999, - Cap: 30 * time.Second, - }, fn) -} diff --git a/pkg/retry/retry_test.go b/pkg/retry/retry_test.go deleted file mode 100644 index d439cbd1..00000000 --- a/pkg/retry/retry_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package retry - -import ( - "fmt" - "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func Test_OnErrorRetry(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { - // given - maxTries := 2 - fn := func() error { - println(fmt.Sprintf("Current time: %s", time.Now())) - return nil - } - - // when - err := OnError(maxTries, AlwaysRetryFunc, fn) - - // then - require.NoError(t, err) - }) - t.Run("should fail", func(t *testing.T) { - // given - maxTries := 2 - fn := func() error { - println(fmt.Sprintf("Current time: %s", time.Now())) - return assert.AnError - } - - // when - err := OnError(maxTries, AlwaysRetryFunc, fn) - - // then - require.Error(t, err) - assert.ErrorIs(t, err, assert.AnError) - }) -} - -func Test_OnConflict(t *testing.T) { - t.Run("should retry once and succeed", func(t *testing.T) { - // given - retryCount := 0 - fn := func() error { - retryCount++ - if retryCount == 1 { - return &errors.StatusError{ErrStatus: metav1.Status{Reason: metav1.StatusReasonConflict}} - } - return nil - } - - // when - err := OnConflict(fn) - - // then - require.NoError(t, err) - }) - t.Run("should fail", func(t *testing.T) { - // given - fn := func() error { - println(fmt.Sprintf("Current time: %s", time.Now())) - return assert.AnError - } - - // when - err := OnConflict(fn) - - // then - require.Error(t, err) - assert.ErrorIs(t, err, assert.AnError) - }) -} - -func TestTestableRetrierError(t *testing.T) { - sut := new(TestableRetrierError) - sut.Err = assert.AnError - require.Error(t, sut) - assert.ErrorContains(t, sut, assert.AnError.Error()) -} - -func Test_TestableRetryFunc(t *testing.T) { - assert.False(t, TestableRetryFunc(nil)) - assert.False(t, TestableRetryFunc(assert.AnError)) - retrierErr := new(TestableRetrierError) - retrierErr.Err = assert.AnError - assert.True(t, TestableRetryFunc(retrierErr)) -}