fix: adopt existing platform resources on rerun
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 14:38:18 -06:00
parent 2320a6ef81
commit bb7cdce4b6
1 changed files with 37 additions and 12 deletions

View File

@ -291,18 +291,18 @@ tofu_state_has_resource() {
cleanup_stale_helm_release_secrets() {
local namespace="$1"
local release_name="$2"
local status
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" get secrets \
-l "owner=helm,name=${release_name}" \
-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|uninstalled)
echo "Deleting stale Helm release secret ${namespace}/${secret} with status=${status}..."
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" delete secret "${secret}"
;;
esac
done
for status in pending-install pending-upgrade pending-rollback failed uninstalling uninstalled; do
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" get secret \
-l "owner=helm,name=${release_name},status=${status}" \
-o name 2>/dev/null |
while read -r secret_ref; do
[[ -z "${secret_ref}" ]] && continue
echo "Deleting stale Helm release ${namespace}/${release_name} secret ${secret_ref#secret/} with status=${status}..."
kubectl --kubeconfig "${KUBECONFIG_PATH}" -n "${namespace}" delete "${secret_ref}"
done
done
}
helm_release_deployed_secret_exists() {
@ -422,6 +422,9 @@ adopt_tofu_kubernetes_manifest() {
adopt_platform_existing_resources() {
local stack="bootstrap/platform"
local dashboard_file
local dashboard_key
local dashboard_name
adopt_tofu_helm_release "${stack}" "helm_release.calico_crds" "tigera-operator" "calico-crds"
adopt_tofu_helm_release "${stack}" "helm_release.calico" "tigera-operator" "calico"
@ -448,6 +451,18 @@ adopt_platform_existing_resources() {
"namespace" \
"monitoring" \
"monitoring"
for dashboard_file in "${REPO_ROOT}/${stack}/grafana-dashboards/"*.json; do
[[ -e "${dashboard_file}" ]] || continue
dashboard_key="$(basename "${dashboard_file}")"
dashboard_name="homelab-grafana-${dashboard_key%.json}"
adopt_tofu_kubernetes_resource \
"${stack}" \
"kubernetes_config_map.grafana_dashboards[\"${dashboard_key}\"]" \
"monitoring" \
"configmap" \
"${dashboard_name}" \
"monitoring/${dashboard_name}"
done
}
adopt_apps_existing_resources() {
@ -582,7 +597,7 @@ tofu_apply_with_kubernetes_retry() {
local retry_sleep="${LAB_TOFU_K8S_APPLY_RETRY_SLEEP_SECONDS:-30}"
local refresh_fallback="${LAB_TOFU_K8S_REFRESH_FALSE_FALLBACK:-true}"
local refresh_fallback_attempts="${LAB_TOFU_K8S_REFRESH_FALSE_RETRIES:-${LAB_TOFU_K8S_APPLY_RETRIES:-3}}"
local retry_error_pattern='cannot get OpenAPI|failed get OpenAPI spec|context deadline exceeded|TLS handshake timeout|connection refused|unexpected EOF|server is currently unable|bad record MAC|failed to query with labels|unexpected error when reading response body|unable to build kubernetes objects from release manifest'
local retry_error_pattern='cannot get OpenAPI|failed get OpenAPI spec|context deadline exceeded|TLS handshake timeout|connection refused|unexpected EOF|server is currently unable|bad record MAC|failed to query with labels|unexpected error when reading response body|unable to build kubernetes objects from release manifest|another operation .* is in progress|already exists'
local attempt=1
local status=0
local output_file
@ -616,6 +631,11 @@ tofu_apply_with_kubernetes_retry() {
while true; do
echo "OpenTofu ${stack} apply still hit Kubernetes API/OpenAPI read errors after ${attempt} attempt(s); trying guarded -refresh=false apply ${refresh_attempt}/${refresh_fallback_attempts}." >&2
ensure_kubernetes_api_ready_for_tofu_stack "${stack}"
if [[ "${stack}" == "bootstrap/platform" ]]; then
adopt_platform_existing_resources
elif [[ "${stack}" == "bootstrap/apps" ]]; then
adopt_apps_existing_resources
fi
output_file="$(mktemp "${TMPDIR:-/tmp}/homelab-tofu-refresh-false-apply.XXXXXX")"
set +e
tofu -chdir="${REPO_ROOT}/${stack}" apply -refresh=false "$@" 2>&1 | tee "${output_file}"
@ -643,6 +663,11 @@ tofu_apply_with_kubernetes_retry() {
echo "OpenTofu ${stack} apply hit a transient Kubernetes API/OpenAPI error; waiting for API stability before retry ${attempt}/${max_attempts}..." >&2
rm -f "${output_file}"
ensure_kubernetes_api_ready_for_tofu_stack "${stack}"
if [[ "${stack}" == "bootstrap/platform" ]]; then
adopt_platform_existing_resources
elif [[ "${stack}" == "bootstrap/apps" ]]; then
adopt_apps_existing_resources
fi
sleep "${retry_sleep}"
done
}