From d7b1ece3b2a4b798c8f3c2466198af3ed826b7ac Mon Sep 17 00:00:00 2001 From: Leo Sok Date: Thu, 24 Jul 2025 12:24:55 +0300 Subject: [PATCH] poststartup script changed to init container --- elasticsearch/values.yaml | 403 ++++++++------------------------------ 1 file changed, 85 insertions(+), 318 deletions(-) diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index 8d1818e..9b7d43b 100644 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -178,279 +178,46 @@ extraContainers: [] # command: ['do', 'something'] -extraInitContainers: [] -# - name: es-index-init -# image: alpine/curl:latest -# command: ['/bin/sh', '-c'] -# args: -# - | -# ES_HOST="elasticsearch-master" # Match your ES service name -# ES_PORT="9200" -# ELASTIC_PASSWORD=$(cat /etc/elasticsearch-password/password) -# curl -u "elastic:${ELASTIC_PASSWORD}" -X PUT "http://${ES_HOST}:${ES_PORT}/chat_v1_idx?pretty" -H 'Content-Type: application/json' -d' -# { -# "settings": { -# "index": { -# "number_of_shards": 3, -# "number_of_replicas": 1, -# "max_ngram_diff": 2 -# }, -# "analysis": { -# "analyzer": { -# "ngram_analyzer": { -# "type": "custom", -# "tokenizer": "ngram_tokenizer", -# "filter": [ -# "lowercase" -# ] -# }, -# "standard_search_analyzer_lowercase": { -# "type": "custom", -# "tokenizer": "standard", -# "filter": [ -# "lowercase" -# ] -# } -# }, -# "tokenizer": { -# "ngram_tokenizer": { -# "type": "ngram", -# "min_gram": 3, -# "max_gram": 5, -# "token_chars": [] -# } -# }, -# "normalizer": { -# "lowercase_normalizer": { -# "type": "custom", -# "filter": [ -# "lowercase" -# ] -# } -# } -# } -# }, -# "mappings": { -# "properties": { -# "id": { -# "type": "long" -# }, -# "name": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "name_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "description": { -# "type": "text", -# "analyzer": "standard", -# "search_analyzer": "standard_search_analyzer_lowercase", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "_class": { -# "type": "text", -# "fields": { -# "keyword": { -# "ignore_above": 256.0, -# "type": "keyword" -# } -# } -# } -# } -# } -# } -# curl -u "elastic:${ELASTIC_PASSWORD}" -X PUT "http://${ES_HOST}:${ES_PORT}/message_v1_idx?pretty" -H 'Content-Type: application/json' -d' -# { -# "settings": { -# "index": { -# "number_of_shards": "3", -# "number_of_replicas": "1" -# }, -# "analysis": { -# "analyzer": { -# "edge_ngram_index_analyzer": { -# "tokenizer": "edge_ngram_index_tokenizer", -# "filter": [ -# "lowercase", -# "apostrophe", -# "classic" -# ] -# }, -# "message_search_analyzer": { -# "tokenizer": "standard" -# } -# }, -# "tokenizer": { -# "edge_ngram_index_tokenizer": { -# "type": "edge_ngram", -# "min_gram": 1, -# "max_gram": 20, -# "token_chars": [ -# "letter", -# "digit" -# ] -# } -# } -# } -# }, -# "mappings": { -# "properties": { -# "chatId": { -# "type": "long" -# }, -# "messageId": { -# "type": "long" -# }, -# "serverTime": { -# "type": "long" -# }, -# "_class": { -# "type": "text", -# "fields": { -# "keyword": { -# "ignore_above": 256.0, -# "type": "keyword" -# } -# } -# }, -# "id": { -# "type": "keyword" -# }, -# "text": { -# "type": "text", -# "analyzer": "edge_ngram_index_analyzer", -# "search_analyzer": "message_search_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# } -# } -# } -# } -# curl -u "elastic:${ELASTIC_PASSWORD}" -X PUT "http://${ES_HOST}:${ES_PORT}/user_v1_idx?pretty" -H 'Content-Type: application/json' -d' -# { -# "settings": { -# "index": { -# "number_of_shards": 3, -# "number_of_replicas": 1, -# "max_ngram_diff": 2 -# }, -# "analysis": { -# "analyzer": { -# "ngram_analyzer": { -# "type": "custom", -# "tokenizer": "ngram_tokenizer", -# "filter": [ -# "lowercase" -# ] -# }, -# "standard_search_analyzer_lowercase": { -# "type": "custom", -# "tokenizer": "standard", -# "filter": [ -# "lowercase" -# ] -# } -# }, -# "tokenizer": { -# "ngram_tokenizer": { -# "type": "ngram", -# "min_gram": 3, -# "max_gram": 5, -# "token_chars": [] -# } -# }, -# "normalizer": { -# "lowercase_normalizer": { -# "type": "custom", -# "filter": [ -# "lowercase" -# ] -# } -# } -# } -# }, -# "mappings": { -# "properties": { -# "id": { -# "type": "long" -# }, -# "username": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "username_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "nickname": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "nickname_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "phone": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "phone_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "email": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "email_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "job_title": { -# "type": "keyword", -# "normalizer": "lowercase_normalizer" -# }, -# "job_title_ngram": { -# "type": "text", -# "analyzer": "ngram_analyzer", -# "search_analyzer": "ngram_analyzer", -# "index_options": "offsets", -# "term_vector": "with_positions_offsets" -# }, -# "_class": { -# "type": "text", -# "fields": { -# "keyword": { -# "ignore_above": 256.0, -# "type": "keyword" -# } -# } -# } -# } -# } -# } -# volumeMounts: -# - name: elasticsearch-password -# mountPath: /etc/elasticsearch-password -# readOnly: true +extraInitContainers: + - name: elasticsearch-template-loader + image: alpine/curl:latest + command: + - /bin/bash + - -c + - | + set -ex + echo "[initContainer] Waiting for Elasticsearch at $ES_URL..." + for i in $(seq 1 ${MAX_RETRIES:-30}); do + if curl -s "$ES_URL" >/dev/null; then + echo "[initContainer] Elasticsearch is up!" + break + fi + echo "[initContainer] Attempt $i: ES not ready, sleeping..." + sleep "${RETRY_INTERVAL:-5}" + done + + echo "[initContainer] Applying templates..." + for idx in "chat:$TEMPLATE_PATH_1" "message:$TEMPLATE_PATH_2" "user:$TEMPLATE_PATH_3"; do + name="${idx%%:*}" + path="${idx##*:}" + echo "[initContainer] Applying $name from $path..." + curl -X PUT "$ES_URL/$name" \ + -H 'Content-Type: application/json' \ + --data-binary @"$path" + done + echo "[initContainer] Done applying templates." + env: + - name: ES_URL + value: "http://elasticsearch-master:9200" + - name: TEMPLATE_PATH_1 + value: "/templates/chat.json" + - name: TEMPLATE_PATH_2 + value: "/templates/message.json" + - name: TEMPLATE_PATH_3 + value: "/templates/user.json" + volumeMounts: + - name: index-template + mountPath: /templates extraVolumes: # Mount the JSON template @@ -576,51 +343,51 @@ nameOverride: "" fullnameOverride: "" healthNameOverride: "" lifecycle: {} -postStart: - exec: - command: - - /bin/bash - - -c - - | - 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=( "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 -s "$ES_URL" >/dev/null; then - echo "[postStart] Elasticsearch is up after $i attempts!" >&2 - break - fi - if [ "$i" -eq "$MAX_RETRIES" ]; then - echo "[postStart] ERROR: Elasticsearch did not become ready after $MAX_RETRIES attempts." >&2 - exit 1 - fi - echo "[postStart] Attempt $i: Elasticsearch not ready, retrying in $RETRY_INTERVAL seconds..." >&2 - sleep "$RETRY_INTERVAL" - done - 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 -X PUT "$ES_URL/$name" \ - -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 - +postStart: {} +# exec: +# command: +# - /bin/bash +# - -c +# - | +# 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=( "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 -s "$ES_URL" >/dev/null; then +# echo "[postStart] Elasticsearch is up after $i attempts!" >&2 +# break +# fi +# if [ "$i" -eq "$MAX_RETRIES" ]; then +# echo "[postStart] ERROR: Elasticsearch did not become ready after $MAX_RETRIES attempts." >&2 +# exit 1 +# fi +# echo "[postStart] Attempt $i: Elasticsearch not ready, retrying in $RETRY_INTERVAL seconds..." >&2 +# sleep "$RETRY_INTERVAL" +# done +# 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 -X PUT "$ES_URL/$name" \ +# -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