Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfs): fence control-api during db recovery

Alfredo Lopez131f51d882af
diff --git a/deploy/scripts/converge-control-db-after-reset.sh b/deploy/scripts/converge-control-db-after-reset.sh
index 2362f6765..90689d5b4 100755
--- a/deploy/scripts/converge-control-db-after-reset.sh
+++ b/deploy/scripts/converge-control-db-after-reset.sh
@@ -123,6 +123,7 @@ fail_closed() {
if [ "$CONVERGED" != true ]; then
set +e
scale_rc=0
+ kc -n control-plane scale deployment/control-api --replicas=0 >/dev/null || scale_rc=1
kc -n control-plane scale deployment/host-context-controller --replicas=0 >/dev/null || scale_rc=1
kc -n control-plane scale deployment/workflow-recipes deployment/trace-maintenance-worker --replicas=0 >/dev/null || scale_rc=1
kc -n gfs scale deployment/gfsc-writer deployment/gfsc-reader --replicas=0 >/dev/null || scale_rc=1
@@ -154,6 +155,8 @@ esac
# Revalidate the bound replacement and exact runtime, then retry cleanup without
# rerunning migrations or rotating credentials.
if [ "$STATE_PHASE" = converged ]; then
+ kc -n control-plane scale deployment/control-api --replicas="$CONTROL_API_REPLICAS" >/dev/null
+ kc -n control-plane rollout status deployment/control-api --timeout=180s >/dev/null
kc -n gfs scale deployment/gfsc-writer --replicas="$WRITER_REPLICAS" >/dev/null
kc -n gfs scale deployment/gfsc-reader --replicas="$READER_REPLICAS" >/dev/null
kc -n gfs rollout status deployment/gfsc-writer --timeout=180s >/dev/null
@@ -178,6 +181,16 @@ if [ "$STATE_PHASE" = converged ]; then
exit 0
fi
+# Secret-backed environment variables are captured when a Pod is created; a
+# Secret patch does not update an existing container. Reassert this fence on
+# every replacement-bound retry, not only in the destructive reset process, so
+# no stale control-api Pod can survive runtime-role credential reconciliation.
+kc -n control-plane scale deployment/control-api --replicas=0 >/dev/null
+CONTROL_API_PODS="$(kc -n control-plane get pods -l app=control-api -o name)"
+if [ -n "$CONTROL_API_PODS" ]; then
+ kc -n control-plane wait --for=delete pod \
+ -l app=control-api --timeout=180s >/dev/null
+fi
kc -n control-plane scale deployment/host-context-controller --replicas=0 >/dev/null
HCC_PODS="$(kc -n control-plane get pods -l app=host-context-controller -o name)"
if [ -n "$HCC_PODS" ]; then
diff --git a/docs/deploy/gfs-permission-store.md b/docs/deploy/gfs-permission-store.md
index ab070fb4c..05eadc5b4 100644
--- a/docs/deploy/gfs-permission-store.md
+++ b/docs/deploy/gfs-permission-store.md
@@ -76,6 +76,14 @@ After PostgreSQL is recreated and Ready, all reset paths call the single
`converge-control-db-after-reset.sh` entrypoint. It applies migrations,
reconciles runtime roles, waits for Control API, opts into
`GFS_RESTORE_ACTIVE_NOLOGIN=true`, and runs the real Service/SCRAM verifier.
+Every initial or `replacement-bound` convergence attempt first scales Control
+API to zero and waits for its Pods to terminate. PostgreSQL connection strings
+are Secret-backed environment variables captured at Pod creation, so patching
+the runtime Secret cannot repair a container that started with the pre-reset
+credential. Runtime roles and Secrets are reconciled only behind that fence;
+the saved replica count is then restored, creating Pods with the current DSN.
+Any later convergence failure reasserts the Control API fence together with the
+other database-dependent controllers.
That reset-only restoration requires lifecycle `ready`, no candidate, the
exact disabled-role privilege contract, and the unchanged committed DSN;
normal deploys cannot reactivate a disabled role. The shared sequence is used
diff --git a/scripts/tests/test-converge-control-db-after-reset.sh b/scripts/tests/test-converge-control-db-after-reset.sh
index 7066e65e3..46a0bff9f 100755
--- a/scripts/tests/test-converge-control-db-after-reset.sh
+++ b/scripts/tests/test-converge-control-db-after-reset.sh
@@ -19,7 +19,7 @@ case "$*" in
*'get pvc control-postgres-data'*) printf '%s' "${FAKE_PVC_UID:-replacement-uid}" ;;
*'patch configmap control-db-reset-state'*)
[ "${FAIL_CAS:-false}" != true ] || exit 1
- IFS='|' read -r state_uid rv version phase original replacement hcc workflow trace writer reader <"$FAKE_STATE"
+ IFS='|' read -r state_uid rv version phase original replacement hcc workflow trace writer reader control_api <"$FAKE_STATE"
payload=""
while [ "$#" -gt 0 ]; do
if [ "$1" = -p ]; then shift; payload="$1"; break; fi
@@ -27,9 +27,12 @@ case "$*" in
done
next_phase="$(python3 -c 'import json,sys; p=json.loads(sys.argv[1]); print(next(x["value"] for x in p if x["op"]=="replace" and x["path"]=="/data/phase"))' "$payload")"
next_replacement="$(python3 -c 'import json,sys; p=json.loads(sys.argv[1]); print(next(x["value"] for x in p if x["op"]=="replace" and x["path"]=="/data/replacementPvcUid"))' "$payload")"
- printf '%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n' "$state_uid" "$((rv + 1))" "$version" "$next_phase" "$original" "$next_replacement" "$hcc" "$workflow" "$trace" "$writer" "$reader" >"$FAKE_STATE"
+ printf '%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n' "$state_uid" "$((rv + 1))" "$version" "$next_phase" "$original" "$next_replacement" "$hcc" "$workflow" "$trace" "$writer" "$reader" "$control_api" >"$FAKE_STATE"
;;
*'scale deployment/host-context-controller --replicas=0') exit 0 ;;
+ *'scale deployment/control-api --replicas=0') exit 0 ;;
+ *'get pods -l app=control-api -o name') printf 'pod/control-api-old\n' ;;
+ *'wait --for=delete pod -l app=control-api --timeout=180s') exit 0 ;;
*'get pods -l app=host-context-controller -o name') printf 'pod/hcc-old\n' ;;
*'wait --for=delete pod -l app=host-context-controller --timeout=180s') exit 0 ;;
*'get pods -l app=gfs-controller -o name') printf 'pod/gfsc-writer-old\n' ;;
@@ -91,10 +94,19 @@ if ! run_converge >"$TMP/success.out" 2>&1; then
printf 'FAIL: successful convergence fixture failed\n' >&2
exit 1
fi
+grep -q 'scale deployment/control-api --replicas=0' "$LOG" \
+ || { printf 'FAIL: control-api was not fenced before runtime credential reconciliation\n' >&2; exit 1; }
+grep -q 'wait --for=delete pod -l app=control-api' "$LOG" \
+ || { printf 'FAIL: stale control-api pod was not removed before credential reconciliation\n' >&2; exit 1; }
writer_zero="$(grep -n 'scale deployment/gfsc-writer deployment/gfsc-reader --replicas=0' "$LOG" | head -1 | cut -d: -f1)"
hcc_zero="$(grep -n 'scale deployment/host-context-controller --replicas=0' "$LOG" | head -1 | cut -d: -f1)"
hcc_wait="$(grep -n 'wait --for=delete pod -l app=host-context-controller' "$LOG" | cut -d: -f1)"
migration="$(grep -n 'run-control-api-db-migration.sh' "$LOG" | head -1 | cut -d: -f1)"
+runtime_roles="$(grep -n 'provision-control-api-runtime-roles.sh' "$LOG" | head -1 | cut -d: -f1)"
+control_zero="$(grep -n 'scale deployment/control-api --replicas=0' "$LOG" | head -1 | cut -d: -f1)"
+control_wait="$(grep -n 'wait --for=delete pod -l app=control-api' "$LOG" | head -1 | cut -d: -f1)"
+control_restore="$(grep -n 'scale deployment/control-api --replicas=2' "$LOG" | head -1 | cut -d: -f1)"
+control_ready="$(grep -n 'rollout status deployment/control-api --timeout=180s' "$LOG" | head -1 | cut -d: -f1)"
restore_writer="$(grep -n 'scale deployment/gfsc-writer --replicas=3' "$LOG" | cut -d: -f1)"
restore_reader="$(grep -n 'scale deployment/gfsc-reader --replicas=2' "$LOG" | cut -d: -f1)"
reconcile="$(grep -n 'reconcile-gfs-deploy-credentials.sh' "$LOG" | cut -d: -f1)"
@@ -104,12 +116,32 @@ workflow_restore="$(grep -n 'scale deployment/workflow-recipes --replicas=5' "$L
trace_restore="$(grep -n 'scale deployment/trace-maintenance-worker --replicas=6' "$LOG" | cut -d: -f1)"
state_delete="$(grep -n 'delete --raw /api/v1/namespaces/control-plane/configmaps/control-db-reset-state' "$LOG" | cut -d: -f1)"
[[ "$hcc_zero" -lt "$hcc_wait" && "$hcc_wait" -lt "$writer_zero" && "$writer_zero" -lt "$migration" && \
+ "$control_zero" -lt "$control_wait" && "$control_wait" -lt "$migration" && \
+ "$migration" -lt "$runtime_roles" && "$runtime_roles" -lt "$control_restore" && \
+ "$control_restore" -lt "$control_ready" && \
"$reconcile" -lt "$restore_writer" && "$reconcile" -lt "$restore_reader" && \
"$gfs_verify" -lt "$workflow_restore" && "$gfs_verify" -lt "$trace_restore" && \
"$workflow_restore" -lt "$hcc_restore" && "$hcc_restore" -lt "$state_delete" ]] \
|| { printf 'FAIL: GFSC isolation/restore order is wrong\n' >&2; exit 1; }
grep -q 'wait --for=delete pod -l app=gfs-controller' "$LOG" \
|| { printf 'FAIL: GFSC pod termination was not awaited\n' >&2; exit 1; }
+grep -q 'wait --for=delete pod -l app=control-api' "$LOG" \
+ || { printf 'FAIL: stale control-api pod was not removed before credential reconciliation\n' >&2; exit 1; }
+
+: >"$LOG"
+init_state replacement-bound replacement-uid
+if ! run_converge >/dev/null 2>&1; then
+ printf 'FAIL: replacement-bound convergence retry failed\n' >&2
+ exit 1
+fi
+retry_control_zero="$(grep -n 'scale deployment/control-api --replicas=0' "$LOG" | head -1 | cut -d: -f1)"
+retry_control_wait="$(grep -n 'wait --for=delete pod -l app=control-api' "$LOG" | head -1 | cut -d: -f1)"
+retry_runtime_roles="$(grep -n 'provision-control-api-runtime-roles.sh' "$LOG" | head -1 | cut -d: -f1)"
+retry_control_restore="$(grep -n 'scale deployment/control-api --replicas=2' "$LOG" | head -1 | cut -d: -f1)"
+[[ "$retry_control_zero" -lt "$retry_control_wait" && \
+ "$retry_control_wait" -lt "$retry_runtime_roles" && \
+ "$retry_runtime_roles" -lt "$retry_control_restore" ]] \
+ || { printf 'FAIL: replacement-bound retry reused a stale control-api pod\n' >&2; exit 1; }
: >"$LOG"
init_state
@@ -119,6 +151,8 @@ if run_converge env FAIL_RECONCILE=true >/dev/null 2>&1; then
fi
[[ "$(grep -c 'scale deployment/gfsc-writer deployment/gfsc-reader --replicas=0' "$LOG")" -eq 2 ]] \
|| { printf 'FAIL: failure did not reassert the zero-replica state\n' >&2; exit 1; }
+[[ "$(grep -c 'scale deployment/control-api --replicas=0' "$LOG")" -eq 2 ]] \
+ || { printf 'FAIL: failure did not reassert the control-api writer fence\n' >&2; exit 1; }
! grep -q 'scale deployment/gfsc-writer --replicas=3' "$LOG" \
|| { printf 'FAIL: writer replicas were restored after failure\n' >&2; exit 1; }
! grep -q 'scale deployment/gfsc-reader --replicas=2' "$LOG" \
diff --git a/scripts/tests/test-gfs-deploy-contract.sh b/scripts/tests/test-gfs-deploy-contract.sh
index 3b4fa5a58..b1fd920e9 100755
--- a/scripts/tests/test-gfs-deploy-contract.sh
+++ b/scripts/tests/test-gfs-deploy-contract.sh
@@ -56,17 +56,22 @@ grep -q 'claim.get("namespace") == "control-plane"' deploy/scripts/reset-control
grep -q 'claim.get("uid") == expected_uid' deploy/scripts/reset-control-db-storage.sh || fail 'released PV cleanup is not claim-UID-scoped'
! grep -q 'ensure_pvc "control-postgres-data"' scripts/minikube/ensure-pvcs.sh || fail 'generic PVC reconciler can still delete the control database'
reset_convergence="$(cat deploy/scripts/converge-control-db-after-reset.sh)"
-for required in run-control-api-db-migration.sh provision-control-api-runtime-roles.sh 'rollout status deployment/control-api' 'GFS_RESTORE_ACTIVE_NOLOGIN=true' reconcile-gfs-deploy-credentials.sh verify-gfs.sh; do
+for required in run-control-api-db-migration.sh provision-control-api-runtime-roles.sh 'scale deployment/control-api --replicas=0' 'wait --for=delete pod' 'rollout status deployment/control-api' 'GFS_RESTORE_ACTIVE_NOLOGIN=true' reconcile-gfs-deploy-credentials.sh verify-gfs.sh; do
[[ "$reset_convergence" == *"$required"* ]] || fail "reset convergence omits $required"
done
migration="$(grep -n 'run-control-api-db-migration.sh' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
roles="$(grep -n 'provision-control-api-runtime-roles.sh' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
-scale="$(grep -n 'scale deployment/control-api' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
-ready="$(grep -n 'rollout status deployment/control-api' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
+control_zero="$(grep -n 'scale deployment/control-api --replicas=0' deploy/scripts/converge-control-db-after-reset.sh | tail -1 | cut -d: -f1)"
+control_wait="$(grep -n -- '-l app=control-api --timeout=180s' deploy/scripts/converge-control-db-after-reset.sh | head -1 | cut -d: -f1)"
+control_restore="$(grep -n 'scale deployment/control-api --replicas="\$CONTROL_API_REPLICAS"' deploy/scripts/converge-control-db-after-reset.sh | tail -1 | cut -d: -f1)"
+ready="$(grep -n 'rollout status deployment/control-api' deploy/scripts/converge-control-db-after-reset.sh | tail -1 | cut -d: -f1)"
restore="$(grep -n 'GFS_RESTORE_ACTIVE_NOLOGIN=true' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
verify="$(grep -n 'verify-gfs.sh' deploy/scripts/converge-control-db-after-reset.sh | tail -1 | cut -d: -f1)"
success="$(grep -n 'migrations, runtime roles, GFS restore, and verification complete' deploy/scripts/converge-control-db-after-reset.sh | cut -d: -f1)"
-[[ "$migration" -lt "$roles" && "$roles" -lt "$scale" && "$scale" -lt "$ready" && "$ready" -lt "$restore" && "$restore" -lt "$verify" && "$verify" -lt "$success" ]] \
+[[ "$control_zero" -lt "$control_wait" && "$control_wait" -lt "$migration" && \
+ "$migration" -lt "$roles" && "$roles" -lt "$control_restore" && \
+ "$control_restore" -lt "$ready" && "$ready" -lt "$restore" && \
+ "$restore" -lt "$verify" && "$verify" -lt "$success" ]] \
|| fail 'reset convergence ordering is incomplete'
! grep -q '|| true' deploy/scripts/converge-control-db-after-reset.sh || fail 'reset convergence suppresses a critical failure'
for upgrade_path in Makefile scripts/minikube/pre-gate-sync.sh scripts/minikube/full-setup.sh; do