fix: adopt existing Helm releases before platform apply
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-07 12:31:36 -06:00
parent ec3e925a03
commit 32178fb840
1 changed files with 22 additions and 6 deletions

View File

@ -297,7 +297,7 @@ cleanup_stale_helm_release_secrets() {
-o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.labels.status}{"\n"}{end}' 2>/dev/null |
while IFS=$'\t' read -r secret status; do
case "${status}" in
pending-install|pending-upgrade|pending-rollback|failed|uninstalling)
pending-install|pending-upgrade|pending-rollback|failed|uninstalling|uninstalled)
echo "Deleting stale Helm release secret ${namespace}/${secret} with status=${status}..."
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" delete secret "${secret}"
;;
@ -317,6 +317,22 @@ helm_release_deployed_secret_exists() {
[[ -n "${secret_name}" ]]
}
helm_release_exists_for_adoption() {
local namespace="$1"
local release_name="$2"
if helm_release_deployed_secret_exists "${namespace}" "${release_name}"; then
return 0
fi
if command -v helm >/dev/null 2>&1 &&
helm --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" status "${release_name}" >/dev/null 2>&1; then
return 0
fi
return 1
}
kubernetes_resource_exists() {
local namespace="$1"
local resource_kind="$2"
@ -336,17 +352,17 @@ adopt_tofu_helm_release() {
local namespace="$3"
local release_name="$4"
local has_state=false
local has_deployed_release=false
local has_existing_release=false
if tofu_state_has_resource "${stack}" "${resource_address}"; then
has_state=true
fi
cleanup_stale_helm_release_secrets "${namespace}" "${release_name}"
if helm_release_deployed_secret_exists "${namespace}" "${release_name}"; then
has_deployed_release=true
if helm_release_exists_for_adoption "${namespace}" "${release_name}"; then
has_existing_release=true
fi
if [[ "${has_state}" == "true" && "${has_deployed_release}" == "false" ]]; then
if [[ "${has_state}" == "true" && "${has_existing_release}" == "false" ]]; then
echo "Removing stale Helm release state for ${namespace}/${release_name} from ${stack} (${resource_address}) because no deployed Helm release exists..."
tofu -chdir="${REPO_ROOT}/${stack}" state rm "${resource_address}"
return 0
@ -355,7 +371,7 @@ adopt_tofu_helm_release() {
if [[ "${has_state}" == "true" ]]; then
return 0
fi
if [[ "${has_deployed_release}" == "false" ]]; then
if [[ "${has_existing_release}" == "false" ]]; then
return 0
fi