Skip to content

Commit

Permalink
fix: diconnect before delete
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Nov 14, 2023
1 parent 28ee8ed commit 8088901
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion castai/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func resourceCastaiClusterDelete(ctx context.Context, data *schema.ResourceData,
clusterId := data.Id()

log.Printf("[INFO] Checking current status of the cluster.")
diconnectionTriggerd := false

err := retry.RetryContext(ctx, data.Timeout(schema.TimeoutDelete), func() *retry.RetryError {
clusterResponse, err := client.ExternalClusterAPIGetClusterWithResponse(ctx, clusterId)
Expand All @@ -51,10 +52,27 @@ func resourceCastaiClusterDelete(ctx context.Context, data *schema.ResourceData,
return retry.RetryableError(fmt.Errorf("triggered cluster deletion"))
}

if agentStatus == sdk.ClusterAgentStatusDisconnected || agentStatus == "waiting-connection" {
if agentStatus == sdk.ClusterAgentStatusDisconnected {
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 diconnectionTriggerd {
return triggerDelete()
}

diconnectionTriggerd = true
}

// 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) == "" {
Expand Down

0 comments on commit 8088901

Please sign in to comment.