Skip to content

Commit

Permalink
Add retry feature for delete pulsarcluster (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuteng authored Dec 2, 2024
1 parent 06b64a5 commit 57547f3
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cloud/resource_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package cloud
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"k8s.io/apimachinery/pkg/api/errors"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -463,7 +464,7 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
return resourcePulsarClusterRead(ctx, d, meta)
}
}
err = retry.RetryContext(ctx, 20*time.Minute, func() *retry.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *retry.RetryError {
dia := resourcePulsarClusterRead(ctx, d, meta)
if dia.HasError() {
return retry.NonRetryableError(fmt.Errorf("ERROR_RETRY_READ_PULSAR_CLUSTER: %s", dia[0].Summary))
Expand Down Expand Up @@ -683,7 +684,7 @@ func resourcePulsarClusterUpdate(ctx context.Context, d *schema.ResourceData, me
}
// Delay 10 seconds to wait for api server start reconcile.
time.Sleep(10 * time.Second)
err = retry.RetryContext(ctx, 20*time.Minute, func() *retry.RetryError {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate), func() *retry.RetryError {
dia := resourcePulsarClusterRead(ctx, d, meta)
if dia.HasError() {
return retry.NonRetryableError(fmt.Errorf("ERROR_RETRY_READ_PULSAR_CLUSTER: %s", dia[0].Summary))
Expand Down Expand Up @@ -717,6 +718,23 @@ func resourcePulsarClusterDelete(ctx context.Context, d *schema.ResourceData, me
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_DELETE_PULSAR_CLUSTER: %w", err))
}
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
_, err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
return nil
}
return retry.NonRetryableError(err)
}

e := fmt.Errorf("pulsarcluster (%s) still exists", d.Id())
return retry.RetryableError(e)
})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_RETRY_READ_PULSAR_CLUSTER: %w", err))
}

d.SetId("")
return nil
}

Expand Down

0 comments on commit 57547f3

Please sign in to comment.