Skip to content

Commit

Permalink
Use wait.ExponentialBackoff instead of a fixed interval polling
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Apr 22, 2021
1 parent cbabf04 commit a628278
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions cmd/snapshot-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,36 @@ var (
version = "unknown"
)

// Checks that the VolumeSnapshot v1 CRDs.
// Checks that the VolumeSnapshot v1 CRDs exist.
func ensureCustomResourceDefinitionsExist(client *clientset.Clientset) error {
timeoutCh := make(chan struct{})
go func() {
defer close(timeoutCh)
time.Sleep(10 * time.Second)
}()

condition := func() (bool, error) {
var err error
_, err = client.SnapshotV1().VolumeSnapshots("kube-system").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
klog.Errorf("Failed to list v1 volumesnapshots with error=%+v", err)
return false, nil
}
_, err = client.SnapshotV1().VolumeSnapshotClasses().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
klog.Errorf("Failed to list v1 volumesnapshotclasses with error=%+v", err)
return false, nil
}
_, err = client.SnapshotV1().VolumeSnapshotContents().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
klog.Errorf("Failed to list v1 volumesnapshotcontents with error=%+v", err)
return false, nil
}
return true, nil
}

pollPeriod := 100 * time.Millisecond
if err := wait.PollImmediateUntil(pollPeriod, condition, timeoutCh); err != nil {
return fmt.Errorf("Failed to fetch VolumeSnapshot CRDs: %+v", err)
// with a Factor of 1.5 we wait up to 7.5 seconds (the 10th attempt)
backoff := wait.Backoff{
Duration: 100 * time.Millisecond,
Factor: 1.5,
Steps: 10,
}
if err := wait.ExponentialBackoff(backoff, condition); err != nil {
return err
}
return nil
}
Expand Down

0 comments on commit a628278

Please sign in to comment.