Retry monitoring imports on transient Kubernetes transport errors
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-29 20:22:58 -06:00
parent 259f9a02a0
commit 82276a32c6
2 changed files with 62 additions and 13 deletions

View File

@ -26,6 +26,13 @@ node before adoption, preserves existing state entries, and stops on discovery
errors rather than treating them as absent resources. No release uninstall or
PVC deletion is involved. Use `platform all` for platform-wide adoption.
Monitoring imports retry recognized Kubernetes transport failures (including
TLS bad-record-MAC responses) up to three times, with five seconds between
attempts. Each attempt rereads state so an import already recorded by a prior
attempt is not repeated. Authentication, authorization, configuration, and
other errors fail immediately. `LAB_TOFU_K8S_IMPORT_RETRIES` overrides the attempt
limit; certificate verification stays enabled throughout.
## Monitoring during reduced CPU capacity
`platform monitoring` creates the `homelab-monitoring` PriorityClass before

View File

@ -438,6 +438,58 @@ adopt_tofu_kubernetes_manifest() {
tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}"
}
monitoring_read_state_addresses() {
tofu -chdir="${REPO_ROOT}/bootstrap/platform" show -json |
python3 -c '
import json, sys
def walk(module):
for resource in module.get("resources", []):
print(resource["address"])
for child in module.get("child_modules", []):
walk(child)
walk((json.load(sys.stdin).get("values") or {}).get("root_module") or {})
'
}
monitoring_import_with_retry() {
local address="$1" import_id="$2"
local max_attempts="${LAB_TOFU_K8S_IMPORT_RETRIES:-3}"
local attempt=1 status output_file current_addresses
local transport_errors='tls: bad record MAC|TLS handshake timeout|unexpected EOF|unexpected error when reading response body|connection reset by peer|context deadline exceeded|i/o timeout|connection refused|server is currently unable'
if ! [[ "${max_attempts}" =~ ^[1-9][0-9]*$ ]]; then
echo "LAB_TOFU_K8S_IMPORT_RETRIES must be a positive integer." >&2
return 2
fi
while ((attempt <= max_attempts)); do
# A failed response can follow a committed state write. Recheck before
# retrying rather than deleting state or importing an already-owned object.
current_addresses="$(monitoring_read_state_addresses)" || return 1
if grep -Fxq -- "${address}" <<<"${current_addresses}"; then
echo "Monitoring resource ${address} is already recorded; import skipped."
return 0
fi
output_file="$(mktemp "${TMPDIR:-/tmp}/homelab-monitoring-import.XXXXXX")" || return 1
if tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false \
"${address}" "${import_id}" >"${output_file}" 2>&1; then
cat "${output_file}"
rm -f "${output_file}"
return 0
else
status=$?
fi
cat "${output_file}"
if ((attempt >= max_attempts)) || ! grep -Eiq "${transport_errors}" "${output_file}"; then
rm -f "${output_file}"
return "${status}"
fi
rm -f "${output_file}"
attempt=$((attempt + 1))
echo "Monitoring import hit a Kubernetes transport error; retrying ${address} (${attempt}/${max_attempts}) in 5s..." >&2
sleep 5
done
}
adopt_monitoring_resource_if_missing() {
local address="$1" namespace="$2" kind="$3" name="$4" import_id="$5"
local existing
@ -454,7 +506,7 @@ adopt_monitoring_resource_if_missing() {
fi
[[ -n "${existing}" ]] || return 0
echo "Adopting existing monitoring resource ${address}: ${import_id}"
tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false "${address}" "${import_id}" || return 1
monitoring_import_with_retry "${address}" "${import_id}" || return 1
}
adopt_platform_monitoring_resources() {
@ -477,16 +529,7 @@ adopt_platform_monitoring_resources() {
python3 -c 'import json,sys; v=json.loads(json.loads(sys.stdin.read())); print(v["namespace"]); print(str(v["mimir_enabled"]).lower())')" || return 1
namespace="${resolved%%$'\n'*}"
mimir_enabled="${resolved##*$'\n'}"
monitoring_state_addresses="$(tofu -chdir="${REPO_ROOT}/bootstrap/platform" show -json |
python3 -c '
import json, sys
def walk(module):
for resource in module.get("resources", []):
print(resource["address"])
for child in module.get("child_modules", []):
walk(child)
walk((json.load(sys.stdin).get("values") or {}).get("root_module") or {})
')" || return 1
monitoring_state_addresses="$(monitoring_read_state_addresses)" || return 1
adopt_monitoring_resource_if_missing kubernetes_priority_class_v1.monitoring \
"" priorityclass homelab-monitoring homelab-monitoring || return 1
@ -510,8 +553,7 @@ walk((json.load(sys.stdin).get("values") or {}).get("root_module") or {})
-n "${namespace}" get secrets -l "owner=helm,name=${release}" -o name)" || return 1
[[ -n "${revisions}" ]] || continue
echo "Adopting existing monitoring Helm release ${namespace}/${release} into ${address}"
tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false \
"${address}" "${namespace}/${release}" || return 1
monitoring_import_with_retry "${address}" "${namespace}/${release}" || return 1
done
for dashboard_file in "${REPO_ROOT}/bootstrap/platform/grafana-dashboards/"*.json; do