Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: refactor code with wait.ExponentialBackoffWithContext #67

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -175,43 +176,37 @@ func createOrPatchStatefulSet(ctx context.Context, logger logr.Logger, ec *ecv1a

func waitForStatefulSetReady(ctx context.Context, logger logr.Logger, r client.Client, name, namespace string) error {
logger.Info("Now checking the readiness of statefulset", "name", name, "namespace", namespace)
// Define backoff parameters
initialDuration := 3 * time.Second
factor := 2.0
maxSteps := 5

duration := initialDuration
backoff := wait.Backoff{
Duration: 3 * time.Second,
Factor: 2.0,
Steps: 5,
}

for i := 0; i < maxSteps; i++ {
err := wait.ExponentialBackoffWithContext(ctx, backoff, func(ctx context.Context) (bool, error) {
// Fetch the StatefulSet
sts, err := getStatefulSet(ctx, r, name, namespace)
if err != nil {
return err
return false, err
}

// Check if the StatefulSet is ready
if sts.Status.ReadyReplicas == *sts.Spec.Replicas {
// StatefulSet is ready
logger.Info("StatefulSet is ready", "name", name, "namespace", namespace)
return nil
return true, nil
}

// Log the current status
logger.Info("StatefulSet is not ready", "ReadyReplicas", strconv.Itoa(int(sts.Status.ReadyReplicas)), "DesiredReplicas", strconv.Itoa(int(*sts.Spec.Replicas)))
return false, nil
})

// Wait for the backoff duration
select {
case <-ctx.Done():
return ctx.Err() // Context canceled or timed out
case <-time.After(duration):
// Proceed to the next retry
}

// Increase the backoff duration
duration = time.Duration(float64(duration) * factor)
if err != nil {
return fmt.Errorf("StatefulSet %s/%s did not become ready: %w", namespace, name, err)
}

return fmt.Errorf("StatefulSet %s/%s did not become ready after %d attempts", namespace, name, maxSteps)
return nil
}

func createHeadlessServiceIfNotExist(ctx context.Context, logger logr.Logger, c client.Client, ec *ecv1alpha1.EtcdCluster, scheme *runtime.Scheme) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestWaitForStatefulSetReady(t *testing.T) {
},
},
expectedResult: false,
expectedError: errors.New("StatefulSet default/test-sts did not become ready after 5 attempts"),
expectedError: errors.New("StatefulSet default/test-sts did not become ready: timed out waiting for the condition"),
},
{
name: "StatefulSet does not exist",
Expand Down
Loading