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 ba7e819 commit bc4fd95
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions castai/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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) == "" {
Expand All @@ -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 {
Expand Down

0 comments on commit bc4fd95

Please sign in to comment.