terranetes-controller icon indicating copy to clipboard operation
terranetes-controller copied to clipboard

Don't run a terraform destroy if there are no resources to clean up

Open KashifSaadat opened this issue 1 year ago • 1 comments

Sometimes when creating a Configuration resource, the module it is pointing to may be invalid for whatever reason causing the plan to fail. If you then attempt to delete the Configuration resource before fixing the module (to get a successful plan), then the terraform destroy action would also fail for the same reason. This leaves the Configuration resource stuck in a Deleting state and means you need to either:

  • Drop the finalizer to get rid of the object (not recommended, could orphan objects)
  • Fix the module on the same ref that the Configuration resource is pointing to, and then trigger some modification on the Configuration object so that the destroy action runs again

It would instead be better if Terranetes could analyse the state file, and if there are no resources detected it would clean up without initiating a terraform destroy.

KashifSaadat avatar Jan 17 '24 09:01 KashifSaadat

@KashifSaadat it sounds like the issue is:

  • If resources are trying to be created but fail, then the resource status should also be marked as not ready
  • Resource statuses need to reflect accurately what has happened i.e. the plan failed or the create failed etc.
  • If the plan failed, then there is nothing to technically created we need to understand what type of failure happened:
  • IF the module failed because of some init, then it also means the init will fail even on a destroy
  • we need to handle this properly so that we a user can clean the resource up to rerun an apply

We may need to think through a way of managing something that never got created due to failures and be able to manage it well i.e. do a tf list resources and if it's empty then we can safely clean up forcibly.

jon-shanks avatar Jan 17 '24 16:01 jon-shanks