From a3f68cfa01adee73007fb87df2ab0a8f5371867c Mon Sep 17 00:00:00 2001 From: Ryo Date: Thu, 7 Aug 2025 21:02:49 +0800 Subject: [PATCH] feat(ci): add elasticsearch setup for helm (#635) --- .../es/coze_resource.index-template.json | 74 +++++++++++++++++ .../es/project_draft.index-template.json | 79 +++++++++++++++++++ .../elasticsearch-init-configmap.yaml | 6 ++ .../templates/elasticsearch-init-job.yaml | 47 +++++++++++ .../elasticsearch-init-script-configmap.yaml | 49 ++++++++++++ 5 files changed, 255 insertions(+) create mode 100644 helm/charts/opencoze/files/es/coze_resource.index-template.json create mode 100644 helm/charts/opencoze/files/es/project_draft.index-template.json create mode 100644 helm/charts/opencoze/templates/elasticsearch-init-configmap.yaml create mode 100644 helm/charts/opencoze/templates/elasticsearch-init-job.yaml create mode 100644 helm/charts/opencoze/templates/elasticsearch-init-script-configmap.yaml diff --git a/helm/charts/opencoze/files/es/coze_resource.index-template.json b/helm/charts/opencoze/files/es/coze_resource.index-template.json new file mode 100644 index 00000000..0b02fde5 --- /dev/null +++ b/helm/charts/opencoze/files/es/coze_resource.index-template.json @@ -0,0 +1,74 @@ +{ + "index_patterns": ["coze_resource*"], + "template": { + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "analysis": { + "analyzer": { + "text_analyzer": { + "type": "custom", + "tokenizer": "standard", + "filter": ["lowercase", "stop", "snowball"] + }, + "smartcn": { + "type": "smartcn" + } + } + } + }, + "mappings": { + "dynamic": false, + "properties": { + "res_type": { + "type": "keyword" + }, + "app_id": { + "type": "keyword", + "null_value": "NULL" + }, + "res_id": { + "type": "keyword" + }, + "res_sub_type": { + "type": "keyword" + }, + "name": { + "type": "text", + "analyzer": "smartcn", + "search_analyzer": "smartcn", + "fields": { + "raw": { + "type": "keyword" + } + } + }, + "owner_id": { + "type": "keyword" + }, + "space_id": { + "type": "keyword" + }, + "biz_status": { + "type": "keyword" + }, + "publish_status": { + "type": "keyword" + }, + "create_time": { + "type": "long" + }, + "update_time": { + "type": "long" + }, + "publish_time": { + "type": "long" + } + } + } + }, + "priority": 200, + "_meta": { + "description": "resource library's index template" + } +} diff --git a/helm/charts/opencoze/files/es/project_draft.index-template.json b/helm/charts/opencoze/files/es/project_draft.index-template.json new file mode 100644 index 00000000..d56465d1 --- /dev/null +++ b/helm/charts/opencoze/files/es/project_draft.index-template.json @@ -0,0 +1,79 @@ +{ + "index_patterns": ["project_draft*"], + "template": { + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "analysis": { + "analyzer": { + "text_analyzer": { + "type": "custom", + "tokenizer": "standard", + "filter": ["lowercase", "stop", "snowball"] + }, + "smartcn": { + "type": "smartcn" + } + } + } + }, + "mappings": { + "dynamic": false, + "properties": { + "create_time": { + "type": "long" + }, + "has_published": { + "type": "keyword" + }, + "id": { + "type": "keyword" + }, + "name": { + "type": "text", + "analyzer": "smartcn", + "search_analyzer": "smartcn", + "fields": { + "raw": { + "type": "keyword" + } + } + }, + "owner_id": { + "type": "keyword" + }, + "publish_time": { + "type": "long" + }, + "space_id": { + "type": "keyword" + }, + "status": { + "type": "keyword" + }, + "type": { + "type": "keyword" + }, + "update_time": { + "type": "long" + }, + "fav_time": { + "type": "long" + }, + "recently_open_time": { + "type": "long" + }, + "is_fav": { + "type": "keyword" + }, + "is_recently_open": { + "type": "keyword" + } + } + } + }, + "priority": 200, + "_meta": { + "description": "Project draft index template" + } +} diff --git a/helm/charts/opencoze/templates/elasticsearch-init-configmap.yaml b/helm/charts/opencoze/templates/elasticsearch-init-configmap.yaml new file mode 100644 index 00000000..9da15270 --- /dev/null +++ b/helm/charts/opencoze/templates/elasticsearch-init-configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "opencoze.fullname" . }}-es-init-config +data: +{{ (.Files.Glob "files/es/*.json").AsConfig | indent 2 }} \ No newline at end of file diff --git a/helm/charts/opencoze/templates/elasticsearch-init-job.yaml b/helm/charts/opencoze/templates/elasticsearch-init-job.yaml new file mode 100644 index 00000000..9a1f7568 --- /dev/null +++ b/helm/charts/opencoze/templates/elasticsearch-init-job.yaml @@ -0,0 +1,47 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ include "opencoze.fullname" . }}-es-init + +spec: + template: + spec: + restartPolicy: Never + initContainers: + - name: wait-for-es + image: busybox:1.36 + command: ['sh', '-c', 'until nc -z {{ include "opencoze.fullname" . }}-elasticsearch 9200; do echo waiting for elasticsearch; sleep 2; done'] + containers: + - name: es-init + image: alpine/curl:8.12.1 + env: + - name: ES_USERNAME + valueFrom: + secretKeyRef: + name: {{ .Release.Name }}-es-secret + key: username + - name: ES_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Release.Name }}-es-secret + key: password + command: + - /bin/sh + - -c + - | + set -ex + /scripts/setup_es.sh + volumeMounts: + - name: es-init-script + mountPath: /scripts + - name: es-index-schema + mountPath: /es_index_schema + volumes: + - name: es-init-script + configMap: + name: {{ include "opencoze.fullname" . }}-es-init-script + defaultMode: 0755 + - name: es-index-schema + configMap: + name: {{ include "opencoze.fullname" . }}-es-init-config + backoffLimit: 4 \ No newline at end of file diff --git a/helm/charts/opencoze/templates/elasticsearch-init-script-configmap.yaml b/helm/charts/opencoze/templates/elasticsearch-init-script-configmap.yaml new file mode 100644 index 00000000..6044879d --- /dev/null +++ b/helm/charts/opencoze/templates/elasticsearch-init-script-configmap.yaml @@ -0,0 +1,49 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "opencoze.fullname" . }}-es-init-script +data: + setup_es.sh: | + #!/bin/sh + set -ex + + ES_HOST="http://{{ include "opencoze.fullname" . }}-elasticsearch:9200" + + CURL_AUTH="" + if [ -n "$ES_USERNAME" ] && [ -n "$ES_PASSWORD" ]; then + CURL_AUTH="-u $ES_USERNAME:$ES_PASSWORD" + fi + + # Wait for Elasticsearch to be ready + until curl -s -f $CURL_AUTH "$ES_HOST/_cluster/health?wait_for_status=yellow&timeout=5s"; do + echo "Waiting for Elasticsearch..." + sleep 5 + done + + # Upload index templates + for file in /es_index_schema/*.json; do + if [ -f "$file" ]; then + template_name=$(basename "$file" .index-template.json) + echo "Uploading index template $template_name" + curl -X PUT $CURL_AUTH "$ES_HOST/_index_template/$template_name" -H "Content-Type: application/json" --data-binary "@$file" + fi + done + + # Create indices + for file in /es_index_schema/*.json; do + if [ -f "$file" ]; then + template_name=$(basename "$file" .index-template.json) + index_name=$template_name + echo "Creating index $index_name" + curl -X PUT $CURL_AUTH "$ES_HOST/$index_name" -H "Content-Type: application/json" -d'{ + "settings": { + "index": { + "number_of_shards": 1, + "number_of_replicas": 1 + } + } + }' + fi + done + + echo "Elasticsearch setup complete." \ No newline at end of file