From ef33a0a52b2f044c58b4b2198ea2d5b9213968fd Mon Sep 17 00:00:00 2001 From: Murad Biashimov Date: Mon, 19 Feb 2024 10:23:27 +0100 Subject: [PATCH] fix(flink): poll application deployment deletion --- CHANGELOG.md | 1 + internal/plugin/util/wait.go | 20 ------- .../flink/flink_application_deployment.go | 53 +++++++------------ 3 files changed, 21 insertions(+), 53 deletions(-) delete mode 100644 internal/plugin/util/wait.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5c89df7..2ab9867ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ nav_order: 1 - Fix `aiven_organization_user_group` resource - `description` field is required - Use golang 1.22 - Output explicitly `termination_protection = true -> false` when service property is removed +- Fix `aiven_flink_application_deployment` deletion ## [4.13.3] - 2024-01-29 diff --git a/internal/plugin/util/wait.go b/internal/plugin/util/wait.go deleted file mode 100644 index a4393d46c..000000000 --- a/internal/plugin/util/wait.go +++ /dev/null @@ -1,20 +0,0 @@ -package util - -import ( - "context" - "time" - - "github.com/avast/retry-go" -) - -// WaitActive waits for resource activity (for example) -// Top timeout comes from the context, no need to parse timeouts from the object. -// But eventually (attempts + connection timeout) * delay makes less timeout than we usually use (20 minutes or more) -func WaitActive(ctx context.Context, retryableFunc retry.RetryableFunc) error { - return retry.Do( - retryableFunc, - retry.Context(ctx), - retry.Attempts(10), - retry.Delay(2*time.Second), - ) -} diff --git a/internal/sdkprovider/service/flink/flink_application_deployment.go b/internal/sdkprovider/service/flink/flink_application_deployment.go index 872a14fdd..046d5b09f 100644 --- a/internal/sdkprovider/service/flink/flink_application_deployment.go +++ b/internal/sdkprovider/service/flink/flink_application_deployment.go @@ -7,7 +7,6 @@ import ( "github.com/aiven/aiven-go-client/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -135,42 +134,30 @@ func resourceFlinkApplicationDeploymentDelete( return diag.Errorf("cannot read Flink Application Deployment resource ID: %v", err) } - _, err = client.FlinkApplicationDeployments.Cancel(ctx, project, serviceName, applicationID, deploymentID) - if err != nil { - return diag.Errorf("error cancelling Flink Application Deployment: %v", err) - } - - //goland:noinspection GoDeprecation - conf := &retry.StateChangeConf{ - Pending: []string{ - "CANCELLING", - }, - Target: []string{ - "CANCELED", - }, - Refresh: func() (interface{}, string, error) { - r, err := client.FlinkApplicationDeployments.Get(ctx, project, serviceName, applicationID, deploymentID) - if err != nil { - return nil, "", err + // Flink Application Deployment has a quite complicated state machine + // https://api.aiven.io/doc/#tag/Service:_Flink/operation/ServiceFlinkDeleteApplicationDeployment + // Retries until succeeds or exceeds the timeout + for { + select { + case <-ctx.Done(): + // The context itself already comes with delete timeout + return diag.Errorf("can't delete Flink Application Deployment %q: %s", ctx.Err()) + case <-time.After(time.Second): + _, err := client.FlinkApplicationDeployments.Get(ctx, project, serviceName, applicationID, deploymentID) + if aiven.IsNotFound(err) { + return nil } - return r, r.Status, nil - }, - Delay: 1 * time.Second, - Timeout: d.Timeout(schema.TimeoutDelete), - MinTimeout: 1 * time.Second, - } - _, err = conf.WaitForStateContext(ctx) - if err != nil { - return diag.Errorf("error waiting for Flink Application Deployment to become canceled: %s", err) - } + // Must be canceled before deleted + _, err = client.FlinkApplicationDeployments.Cancel(ctx, project, serviceName, applicationID, deploymentID) + if err == nil { + continue + } - _, err = client.FlinkApplicationDeployments.Delete(ctx, project, serviceName, applicationID, deploymentID) - if err != nil { - return diag.Errorf("error deleting Flink Application Deployment: %v", err) + // Completely ignores all errors, until it gets 404 on GET request + _, _ = client.FlinkApplicationDeployments.Delete(ctx, project, serviceName, applicationID, deploymentID) + } } - - return nil } // resourceFlinkApplicationDeploymentRead reads an existing Flink Application Deployment resource.