diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index bc7077c..4345d2e 100644 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -574,14 +574,31 @@ nameOverride: "" fullnameOverride: "" healthNameOverride: "" lifecycle: {} +postStart: + exec: + command: postStart: exec: command: - /bin/bash - -c - | - set -euo pipefail - echo "[postStart] Waiting for Elasticsearch to be ready..." >&2 + echo "[postStart] Starting postStart hook..." >&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 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 @@ -591,32 +608,25 @@ postStart: echo "[postStart] ERROR: Elasticsearch did not become ready after $MAX_RETRIES attempts." >&2 exit 1 fi - sleep 5 + echo "[postStart] Attempt $i: Elasticsearch not ready, retrying in $RETRY_INTERVAL seconds..." >&2 + sleep "$RETRY_INTERVAL" done - echo "[postStart] Loading index template from $TEMPLATE_PATH" >&2 - if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/chat" \ - -u "$ES_USERNAME:$ES_PASSWORD" \ - -H 'Content-Type: application/json' \ - --data-binary @"$TEMPLATE_PATH_1"; then - echo "[postStart] ERROR: Failed to apply index template!" >&2 - exit 1 - fi - if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/message" \ - -u "$ES_USERNAME:$ELASTIC_PASSWORD" \ - -H 'Content-Type: application/json' \ - --data-binary @"$TEMPLATE_PATH_2"; then - echo "[postStart] ERROR: Failed to apply index template!" >&2 - exit 1 - fi - if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/user" \ - -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 + + echo "[postStart] Applying index templates..." >&2 + for idx in "chat:$TEMPLATE_PATH_1" "message:$TEMPLATE_PATH_2" "user:$TEMPLATE_PATH_3"; do + name="${idx%%:*}" + path="${idx##*:}" + if ! /usr/bin/curl --cacert "$CA_CERT" -X PUT "$ES_URL/$name" \ + -u "$ES_USERNAME:$ELASTIC_PASSWORD" \ + -H 'Content-Type: application/json' \ + --data-binary @"$path"; then + echo "[postStart] ERROR: Failed to apply index template for '$name' from '$path'" >&2 + exit 1 + fi + echo "[postStart] Applied template: $name" >&2 + done + + echo "[postStart] All index templates applied successfully." >&2 sysctlInitContainer: enabled: true