sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/retry.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package e2e
    21  
    22  import (
    23  	"context"
    24  	"time"
    25  
    26  	"k8s.io/apimachinery/pkg/util/wait"
    27  )
    28  
    29  const (
    30  	// Parameters for retrying with exponential backoff.
    31  	retryBackoffInitialDuration = 100 * time.Millisecond
    32  	retryBackoffFactor          = 3
    33  	retryBackoffJitter          = 0.1
    34  	retryBackoffSteps           = 3
    35  )
    36  
    37  // retryWithTimeout retries a function that returns an error until a timeout is reached
    38  func retryWithTimeout(interval, timeout time.Duration, fn func() error) error {
    39  	var pollError error
    40  	err := wait.PollUntilContextTimeout(context.TODO(), interval, timeout, true, func(context.Context) (bool, error) {
    41  		pollError = nil
    42  		err := fn()
    43  		if err != nil {
    44  			pollError = err
    45  			return false, nil //nolint:nilerr // We don't want to return err here
    46  		}
    47  		return true, nil
    48  	})
    49  	if pollError != nil {
    50  		return pollError
    51  	}
    52  	return err
    53  }