Skip to content

Commit 1dc71f0

Browse files
committed
Wait for Consul server before worker registration
1 parent bf772aa commit 1dc71f0

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

consul-postgres-ha/mesh-sidecar/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ just an Envoy sidecar.
3030
2. Starts `mesh-conn` in the background.
3131
3. Starts `consul agent` in the background, with `-server` +
3232
`-bootstrap-expect=N` if `ROLE=coordinator`.
33-
4. (Workers only) Polls `consul connect envoy -bootstrap` until the
34-
local consul agent answers, then exec's envoy.
33+
4. (Workers only) Waits until the local Consul client sees an alive
34+
server, requests admission tokens, registers each service, then
35+
polls `consul connect envoy -bootstrap` until it can exec envoy.
3536
5. `wait -n`s on all background processes — if any one exits, the
3637
container exits with that code, and compose's
3738
`restart: on-failure` brings it back.

consul-postgres-ha/mesh-sidecar/entrypoint.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,30 @@ wait_consul_ready() {
220220
done
221221
}
222222

223+
wait_consul_server_ready() {
224+
local n=0
225+
until [ "$(CONSUL_HTTP_TOKEN="${CONSUL_MANAGEMENT_TOKEN:-}" consul members 2>/dev/null | awk 'NR>1 && $4=="server" && $3=="alive"' | wc -l)" -gt 0 ]; do
226+
n=$((n+1))
227+
if [ $n -gt 300 ]; then
228+
log "consul server not reachable after 10m"
229+
return 1
230+
fi
231+
if [ $((n % 15)) -eq 0 ]; then
232+
log "waiting for consul server..."
233+
fi
234+
sleep 2
235+
done
236+
}
237+
223238
# ---- 6. workers: register one Consul service+sidecar per backend, launch Envoys ----
224239
ENVOYS=()
225240
if [ "$ROLE" = "worker" ]; then
226241
log "waiting for local consul agent..."
227242
wait_consul_ready
243+
if [ "${ADMISSION_BROKER_ENABLE:-}" = "1" ]; then
244+
log "waiting for consul server..."
245+
wait_consul_server_ready
246+
fi
228247

229248
# Iterate unique backends — one per distinct sidecar_port. For each
230249
# backend we render one Consul registration JSON and one Envoy
@@ -322,6 +341,9 @@ if [ "$ROLE" = "worker" ]; then
322341
TAGS_JSON='[]'
323342
fi
324343

344+
CURL_TOKEN_ARGS=()
345+
[ -n "$BACKEND_CONSUL_TOKEN" ] && CURL_TOKEN_ARGS=(-H "X-Consul-Token: ${BACKEND_CONSUL_TOKEN}")
346+
325347
# Defensive cleanup: a prior boot may have used different IDs for
326348
# the same logical proxy (e.g. an earlier release of this image
327349
# registered Pattern B as `Kind: connect-proxy` with ID
@@ -331,7 +353,7 @@ if [ "$ROLE" = "worker" ]; then
331353
# -sidecar-for` sees two matches and refuses to render. Deregister
332354
# known stale IDs idempotently before we PUT the new spec.
333355
for stale_id in "${PARENT}-sidecar-${PEER_ID}"; do
334-
curl -fsS -X PUT "http://127.0.0.1:8500/v1/agent/service/deregister/${stale_id}" \
356+
curl -fsS "${CURL_TOKEN_ARGS[@]}" -X PUT "http://127.0.0.1:8500/v1/agent/service/deregister/${stale_id}" \
335357
>/dev/null 2>&1 || true
336358
done
337359
jq -n \
@@ -369,8 +391,6 @@ if [ "$ROLE" = "worker" ]; then
369391

370392
# Re-register on every boot via the agent HTTP API (PUT-idempotent).
371393
log "registering ${PARENT} (id=${PARENT_ID}) + sidecar (port=${SIDECAR_PORT}, base-id=${BASE_ID})"
372-
CURL_TOKEN_ARGS=()
373-
[ -n "$BACKEND_CONSUL_TOKEN" ] && CURL_TOKEN_ARGS=(-H "X-Consul-Token: ${BACKEND_CONSUL_TOKEN}")
374394
until curl -fsS "${CURL_TOKEN_ARGS[@]}" -X PUT --data-binary @"$SPEC_FILE" \
375395
http://127.0.0.1:8500/v1/agent/service/register; do
376396
log "${PARENT} register failed; retrying"

0 commit comments

Comments
 (0)