Fix destroy skipped on zero resource count when terraform state exists; reduce Run cyclomatic complexity#1792
Open
Fix destroy skipped on zero resource count when terraform state exists; reduce Run cyclomatic complexity#1792
Conversation
…secret is missing - Remove state secret check in delete.go to always attempt destroy - Add --upload-on-error flag to step binary for best-effort state upload on failure - Add tests for both changes Co-authored-by: gambol99 <4280679+gambol99@users.noreply.github.com>
Co-authored-by: gambol99 <4280679+gambol99@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix issue 1781 with codebase review and unit tests
Fix orphaned cloud resources when terraform apply fails before state secret is created
Mar 11, 2026
… secret is missing Instead of removing the check entirely, log a warning and proceed when the state secret is absent. This handles both Kubernetes-backend configs (where the secret exists) and external-backend / failed-apply configs (where the state lives in S3/GCS and the K8s secret was never created). Co-authored-by: gambol99 <4280679+gambol99@users.noreply.github.com>
…phaning partial resources Move state secret existence check before the Resources==0 skip-destroy decision. Only skip destroy when BOTH Resources==0 AND no state secret exists. If state exists with Resources==0, proceed with destroy (failed apply left state but didn't update resource count). Also fix comment indentation in cmd/step/main.go. Co-authored-by: gambol99 <4280679+gambol99@users.noreply.github.com>
Copilot
AI
changed the title
Fix orphaned cloud resources when terraform apply fails before state secret is created
Fix destroy job skipped when apply fails with zero/missing resource count or external state backend
Mar 11, 2026
Extract five focused helper functions from Run: - waitForSignal: wait-for-file logic with timeout - runCommand: single command attempt (pipe setup, start, wait) - runCommandWithRetries: retry loop - handleCommandError: error-file touch + upload-on-error + wrapped error - uploadSuccessFiles: post-success secret uploads Also add uploadRetryAttempts constant to replace magic number 2. Add shouldSkipDestroy helper to delete.go to clarify three-part condition. Co-authored-by: gambol99 <4280679+gambol99@users.noreply.github.com>
Copilot
AI
changed the title
Fix destroy job skipped when apply fails with zero/missing resource count or external state backend
Fix destroy skipped on zero resource count when terraform state exists; reduce Run cyclomatic complexity
Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
terraform applyfails mid-run,configuration.Status.Resourcesmay be left at zero, causing the controller to unconditionally skipterraform destroy— orphaning any partially-created cloud resources indefinitely.Changes
pkg/controller/configuration/delete.goResources == 0early-return. The destroy skip now requires bothResources == 0and no state secret present. If state exists, destroy runs regardless of the resource count.shouldSkipDestroy(configuration, stateExists) boolfor readability.cmd/step/main.goResolved
gocyclolint failure (Runcomplexity: 33 → 13) by extracting five focused helpers:waitForSignalrunCommandrunCommandWithRetries(attempts, error)handleCommandErroruploadSuccessFilesAdded
uploadRetryAttempts = 2constant to replace duplicate magic numbers across both upload paths.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.