elastic startup fix

This commit is contained in:
Leo Sok 2025-07-24 11:16:27 +03:00
parent c54f84d166
commit 764e820fee

View File

@ -574,14 +574,31 @@ nameOverride: ""
fullnameOverride: "" fullnameOverride: ""
healthNameOverride: "" healthNameOverride: ""
lifecycle: {} lifecycle: {}
postStart:
exec:
command:
postStart: postStart:
exec: exec:
command: command:
- /bin/bash - /bin/bash
- -c - -c
- | - |
set -euo pipefail echo "[postStart] Starting postStart hook..." >&2
echo "[postStart] Waiting for Elasticsearch to be ready..." >&2
# Default values (override in env)
MAX_RETRIES="${MAX_RETRIES:-30}"
RETRY_INTERVAL="${RETRY_INTERVAL:-5}"
# Validate required env vars
REQUIRED_VARS=("CA_CERT" "ES_USERNAME" "ELASTIC_PASSWORD" "ES_URL" "TEMPLATE_PATH_1" "TEMPLATE_PATH_2" "TEMPLATE_PATH_3")
for var in "${REQUIRED_VARS[@]}"; do
if [ -z "${!var:-}" ]; then
echo "[postStart] ERROR: Required env var '$var' is not set!" >&2
exit 1
fi
done
echo "[postStart] Waiting for Elasticsearch at $ES_URL to be ready..." >&2
for ((i=1; i<=MAX_RETRIES; i++)); do for ((i=1; i<=MAX_RETRIES; i++)); do
if /usr/bin/curl --cacert "$CA_CERT" -s -u "$ES_USERNAME:$ELASTIC_PASSWORD" "$ES_URL" >/dev/null; then if /usr/bin/curl --cacert "$CA_CERT" -s -u "$ES_USERNAME:$ELASTIC_PASSWORD" "$ES_URL" >/dev/null; then
echo "[postStart] Elasticsearch is up after $i attempts!" >&2 echo "[postStart] Elasticsearch is up after $i attempts!" >&2
@ -591,32 +608,25 @@ postStart:
echo "[postStart] ERROR: Elasticsearch did not become ready after $MAX_RETRIES attempts." >&2 echo "[postStart] ERROR: Elasticsearch did not become ready after $MAX_RETRIES attempts." >&2
exit 1 exit 1
fi fi
sleep 5 echo "[postStart] Attempt $i: Elasticsearch not ready, retrying in $RETRY_INTERVAL seconds..." >&2
sleep "$RETRY_INTERVAL"
done done
echo "[postStart] Loading index template from $TEMPLATE_PATH" >&2
if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/chat" \ echo "[postStart] Applying index templates..." >&2
-u "$ES_USERNAME:$ES_PASSWORD" \ for idx in "chat:$TEMPLATE_PATH_1" "message:$TEMPLATE_PATH_2" "user:$TEMPLATE_PATH_3"; do
-H 'Content-Type: application/json' \ name="${idx%%:*}"
--data-binary @"$TEMPLATE_PATH_1"; then path="${idx##*:}"
echo "[postStart] ERROR: Failed to apply index template!" >&2 if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/$name" \
exit 1 -u "$ES_USERNAME:$ELASTIC_PASSWORD" \
fi -H 'Content-Type: application/json' \
if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/message" \ --data-binary @"$path"; then
-u "$ES_USERNAME:$ELASTIC_PASSWORD" \ echo "[postStart] ERROR: Failed to apply index template for '$name' from '$path'" >&2
-H 'Content-Type: application/json' \ exit 1
--data-binary @"$TEMPLATE_PATH_2"; then fi
echo "[postStart] ERROR: Failed to apply index template!" >&2 echo "[postStart] Applied template: $name" >&2
exit 1 done
fi
if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/user" \ echo "[postStart] All index templates applied successfully." >&2
-u "$ES_USERNAME:$ELASTIC_PASSWORD" \
-H 'Content-Type: application/json' \
--data-binary @"$TEMPLATE_PATH_3"; then
echo "[postStart] ERROR: Failed to apply index template!" >&2
exit 1
fi
echo "[postStart] Index template 'chat_v1_idx' applied." >&2
echo "Debug message" >> /poststart.log 2>&1
sysctlInitContainer: sysctlInitContainer:
enabled: true enabled: true