From bc4fd95a0a2d9901707ccac035f5867386cf55cc Mon Sep 17 00:00:00 2001 From: Marcin Kaciuba Date: Tue, 14 Nov 2023 13:30:09 +0100 Subject: [PATCH] fix: diconnect before delete --- castai/cluster.go | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/castai/cluster.go b/castai/cluster.go index f87df5e0..693b64aa 100644 --- a/castai/cluster.go +++ b/castai/cluster.go @@ -43,10 +43,27 @@ func resourceCastaiClusterDelete(ctx context.Context, data *schema.ResourceData, return nil } + triggerDisconnect := func() *retry.RetryError { + response, err := client.ExternalClusterAPIDisconnectClusterWithResponse(ctx, clusterId, sdk.ExternalClusterAPIDisconnectClusterJSONRequestBody{ + DeleteProvisionedNodes: getOptionalBool(data, FieldDeleteNodesOnDisconnect, false), + KeepKubernetesResources: toPtr(true), + }) + if checkErr := sdk.CheckOKResponse(response, err); checkErr != nil { + return retry.NonRetryableError(err) + } + + return retry.RetryableError(fmt.Errorf("triggered agent disconnection cluster status %s agent status %s", clusterStatus, agentStatus)) + } + triggerDelete := func() *retry.RetryError { log.Printf("[INFO] Deleting cluster.") - if err := sdk.CheckResponseNoContent(client.ExternalClusterAPIDeleteClusterWithResponse(ctx, clusterId)); err != nil { - return retry.NonRetryableError(fmt.Errorf("cluster status %s agent status %s error: %w", clusterStatus, agentStatus, err)) + res, err := client.ExternalClusterAPIDeleteClusterWithResponse(ctx, clusterId) + if res.StatusCode() == 400 { + return triggerDisconnect() + } + + if chekErr := sdk.CheckResponseNoContent(res, err); chekErr != nil { + return retry.NonRetryableError(fmt.Errorf("error when deleting cluster status %s agent status %s error: %w", clusterStatus, agentStatus, err)) } return retry.RetryableError(fmt.Errorf("triggered cluster deletion")) } @@ -55,17 +72,6 @@ func resourceCastaiClusterDelete(ctx context.Context, data *schema.ResourceData, return triggerDelete() } - if agentStatus == "waiting-connection" { - response, err := client.ExternalClusterAPIDisconnectClusterWithResponse(ctx, clusterId, sdk.ExternalClusterAPIDisconnectClusterJSONRequestBody{ - DeleteProvisionedNodes: getOptionalBool(data, FieldDeleteNodesOnDisconnect, false), - KeepKubernetesResources: toPtr(true), - }) - - if checkErr := sdk.CheckOKResponse(response, err); checkErr != nil { - return retry.NonRetryableError(err) - } - } - // If cluster doesn't have credentials we have to call delete cluster instead of disconnect because disconnect // will do nothing on cluster with empty credentials. if toString(clusterResponse.JSON200.CredentialsId) == "" { @@ -86,15 +92,7 @@ func resourceCastaiClusterDelete(ctx context.Context, data *schema.ResourceData, if toString(clusterResponse.JSON200.CredentialsId) != "" && agentStatus != sdk.ClusterAgentStatusDisconnected { log.Printf("[INFO] Disconnecting cluster.") - response, err := client.ExternalClusterAPIDisconnectClusterWithResponse(ctx, clusterId, sdk.ExternalClusterAPIDisconnectClusterJSONRequestBody{ - DeleteProvisionedNodes: getOptionalBool(data, FieldDeleteNodesOnDisconnect, false), - KeepKubernetesResources: toPtr(true), - }) - if checkErr := sdk.CheckOKResponse(response, err); checkErr != nil { - return retry.NonRetryableError(err) - } - - return retry.RetryableError(fmt.Errorf("triggered agent disconnection cluster status %s agent status %s", clusterStatus, agentStatus)) + return triggerDisconnect() } if agentStatus == sdk.ClusterAgentStatusDisconnected && clusterStatus != sdk.ClusterStatusDeleted {