Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fi

REGION_PROJECT=$(<"${SHARED_DIR}/region-project-id")
REGION_CLUSTER=$(<"${SHARED_DIR}/region-cluster-name")
REGION_FOLDER_ID=$(cat "${SHARED_DIR}/region-folder-id" 2>/dev/null || echo "")
MC_PROJECT=$(<"${SHARED_DIR}/mc-project-id")
MC_CLUSTER=$(<"${SHARED_DIR}/mc-cluster-name")
SERVICE_PROJECT=$(cat "${SHARED_DIR}/service-project-id" 2>/dev/null || echo "")
Expand All @@ -60,6 +61,9 @@ MC_PROJECT_NUMBER=$(gcloud projects describe "${MC_PROJECT}" --format='value(pro

log "Infrastructure to clean up:"
log " Region: ${REGION_PROJECT} (#${REGION_PROJECT_NUMBER}) / ${REGION_CLUSTER}"
if [[ -n "${REGION_FOLDER_ID}" ]]; then
log " Folder: ${REGION_FOLDER_ID}"
fi
log " MC: ${MC_PROJECT} (#${MC_PROJECT_NUMBER}) / ${MC_CLUSTER}"
if [[ -n "${SERVICE_PROJECT}" ]]; then
log " Service: ${SERVICE_PROJECT}"
Expand Down Expand Up @@ -293,6 +297,39 @@ delete_project() {
fi
}

# ========================================================================
# Phase 5b: Delete the per-run E2E folder
# ========================================================================
delete_folder() {
local folder_id=$1
local label=$2
local attempt
local output

if [[ -z "${folder_id}" ]]; then
log " ERROR: No folder ID available for ${label} cleanup"
return 1
fi

log "--- [${label}] Deleting folder: ${folder_id} ---"
for attempt in 1 2 3; do
if output=$(gcloud resource-manager folders delete "${folder_id}" --quiet 2>&1); then
echo "${output}" | tee -a "${LOG}"
log " Folder ${folder_id} deletion initiated"
return 0
fi

echo "${output}" | tee -a "${LOG}"
if [[ ${attempt} -lt 3 ]]; then
log " Folder deletion failed; waiting for project deletion to propagate (attempt ${attempt}/3)"
sleep 5
fi
done

log " ERROR: Failed to delete folder ${folder_id} after 3 attempts"
return 1
}

# ========================================================================
# Phase 6: Clear TFC workspace state
# ========================================================================
Expand Down Expand Up @@ -661,9 +698,25 @@ if [[ -n "${CUSTOMER_PROJECT}" ]]; then
delete_project "${CUSTOMER_PROJECT}" "Customer" || CLEANUP_FAILED=1
fi

folder_cleanup_succeeded=false
if [[ -n "${REGION_FOLDER_ID}" ]]; then
if delete_folder "${REGION_FOLDER_ID}" "E2E Region"; then
folder_cleanup_succeeded=true
else
CLEANUP_FAILED=1
fi
else
log "ERROR: No E2E region folder ID available; retaining TFC workspace and state for auto-destroy"
CLEANUP_FAILED=1
fi

# Phase 6: Clear TFC workspace state
log ""
clear_tfc_workspace || CLEANUP_FAILED=1
if [[ "${folder_cleanup_succeeded}" == "true" ]]; then
clear_tfc_workspace || CLEANUP_FAILED=1
else
log "Skipping TFC workspace cleanup because folder cleanup did not complete"
fi

log ""
if [[ "${CLEANUP_FAILED}" -ne 0 ]]; then
Expand All @@ -673,5 +726,5 @@ if [[ "${CLEANUP_FAILED}" -ne 0 ]]; then
fi

log "=== Cleanup complete ==="
log "Projects are now in PENDING_DELETE state (30-day soft delete)"
log "Projects and the per-run folder are now in PENDING_DELETE state (30-day soft delete)"
log "TFC workspace state has been cleared"
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ ref:
3. Force-deletes any remaining NEGs
4. Deletes DNS records from regional zones
5. Force-deletes GCP projects (cascades to all resources)
6. Clears Terraform Cloud workspace state
6. Deletes the now-empty per-run E2E folder
7. Clears Terraform Cloud workspace state

WIF credential is read from SHARED_DIR/wif-cred.json (written by hypershift-gcp-wif-auth).
HCP Terraform CLI credentials are mounted from the tfcloud-token group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,38 @@ import_orphaned_firestore() {
return 1
}

# A failed apply can leave resources in the remote state before Terraform has
# produced its normal outputs. Preserve the folder ID when that happens so the
# cleanup step can delete the per-run folder before it clears the workspace.
parse_folder_id() {
awk -F= '$1 ~ /^[[:space:]]*folder_id[[:space:]]*$/ { value=$2; gsub(/^[[:space:]]+|[[:space:]]+$/, "", value); gsub(/^"|"$/, "", value); print value; exit }'
}

recover_partial_folder_id() {
local folder_id=""

log "Attempting to recover E2E folder ID from partial Terraform state..."

# Prefer the root output when Terraform recorded it despite the failed apply.
folder_id=$(terraform output -json 2>>"${LOG}" | \
jq -er '.region.value.folder_id | strings | select(length > 0)' 2>>"${LOG}" || true)

# Root outputs are not guaranteed after a partial apply, but the folder
# resource itself is already present in remote state if it was created.
if [[ -z "${folder_id}" ]]; then
folder_id=$(terraform state show -no-color module.region.google_folder.region 2>>"${LOG}" | \
parse_folder_id || true)
fi

if [[ -z "${folder_id}" ]]; then
log "WARNING: Could not recover E2E folder ID from partial Terraform state"
return 1
fi

printf '%s\n' "${folder_id}" > "${SHARED_DIR}/region-folder-id"
log "Recovered E2E folder ID: ${folder_id}"
}

MAX_APPLY_ATTEMPTS=5
apply_attempt=1
apply_wait=30
Expand Down Expand Up @@ -318,6 +350,7 @@ while (( apply_attempt <= MAX_APPLY_ATTEMPTS )); do
log "Error details:"
log "${non_transient}"
log "Check TFC workspace: https://app.terraform.io/app/${TFC_ORG}/workspaces/${WORKSPACE_NAME}"
recover_partial_folder_id || true
exit 1
fi

Expand All @@ -336,6 +369,7 @@ while (( apply_attempt <= MAX_APPLY_ATTEMPTS )); do
else
log "ERROR: Terraform apply failed after ${MAX_APPLY_ATTEMPTS} attempts"
log "Check TFC workspace: https://app.terraform.io/app/${TFC_ORG}/workspaces/${WORKSPACE_NAME}"
recover_partial_folder_id || true
exit 1
fi
done
Expand All @@ -362,6 +396,7 @@ fi
# Write individual outputs to SHARED_DIR for downstream steps
jq -r '.region.value.project_id // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/region-project-id"
jq -r '.region.value.cluster_name // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/region-cluster-name"
jq -r '.region.value.folder_id // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/region-folder-id"
Comment thread
jimdaga marked this conversation as resolved.
Resolved
jq -r '.management_cluster.value.project_id // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/mc-project-id"
jq -r '.management_cluster.value.cluster_name // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/mc-cluster-name"
jq -r '.management_cluster.value.cluster_endpoint // empty' /tmp/tf-outputs.json > "${SHARED_DIR}/mc-cluster-endpoint"
Expand All @@ -379,7 +414,7 @@ if [[ -n "${INFRA_ID}" ]]; then
fi

# Validate critical outputs were written (early writes + terraform outputs)
for output_file in region region-project-id region-cluster-name mc-project-id mc-cluster-name mc-cluster-endpoint customer-project-id api-endpoint oidc-endpoint workspace-name run-id; do
for output_file in region region-project-id region-cluster-name region-folder-id mc-project-id mc-cluster-name mc-cluster-endpoint customer-project-id api-endpoint oidc-endpoint workspace-name run-id; do
if [[ ! -s "${SHARED_DIR}/${output_file}" ]]; then
log "ERROR: Output file ${output_file} is empty or missing"
exit 1
Expand All @@ -389,6 +424,7 @@ done
log ""
log "=== Provision Complete ==="
log " Region Project: $(<${SHARED_DIR}/region-project-id)"
log " Region Folder: $(<${SHARED_DIR}/region-folder-id)"
log " MC Project: $(<${SHARED_DIR}/mc-project-id)"
log " MC Cluster: $(<${SHARED_DIR}/mc-cluster-name)"
log " TFC Workspace: ${WORKSPACE_NAME}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ref:
- region (selected GCP region)
- region-project-id
- region-cluster-name
- region-folder-id (for post-test cleanup)
- mc-project-id
- mc-cluster-name
- mc-cluster-endpoint
Expand Down