Rename to opensearch
Change-Id: I132123d4ac3a6e865c14e71806450aa760c50a15
diff --git a/logging/opensearch_setting/templates/configmap-script.yml b/logging/opensearch_setting/templates/configmap-script.yml
new file mode 100644
index 0000000..a90adcb
--- /dev/null
+++ b/logging/opensearch_setting/templates/configmap-script.yml
@@ -0,0 +1,178 @@
+# Copyright 2021-present Open Networking Foundation
+# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
+#
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: "{{ .Release.Name }}-scripts"
+ labels:
+ app: onos
+ chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
+ release: "{{ .Release.Name }}"
+ heritage: "{{ .Release.Service }}"
+data:
+ update.sh: |
+ #!/bin/bash
+ host=${ES_URL:-localhost:9201}
+ skip_tls=${SKIP_TLS:true}
+ auth_username=${AUTH_USERNAME}
+ auth_password=${AUTH_PASSWORD}
+ dest_dir=${DEST_DIR}
+ monitor_dir=${MONITOR_DIR}
+ setting_dir=${SETTING_DIR}
+ temp_dir=${OUTPUT_DIR:-/opensearch_temp}
+
+ curl_opts=""
+ if [ ! -z $auth_username ]; then
+ curl_opts="$curl_opts -u $auth_username:$auth_password"
+ fi
+
+ if [ $skip_tls ]; then
+ curl_opts="$curl_opts -k"
+ fi
+
+ cleanup() {
+ echo "Cleaning up..."
+ exit
+ }
+
+ #return destination id if exist
+ check_destination() { name=$1
+ curl -s $curl_opts -H "content-type: application/json" "$host/_opensearch/_alerting/destinations" | jq -r ".destinations[] | select(.name == \"$name\").id"
+ }
+
+ update_destination() {
+ echo "update"
+ file=$1
+ id=$2
+ #compress the json file to reduce the http size
+ new_file="$temp_dir/$file"
+ jq -c '' < "$file" > "$new_file"
+ curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opensearch/_alerting/destinations/$id"
+ rm "$new_file"
+ echo " "
+ }
+
+ create_destination() {
+ echo "create"
+ file=$1
+ new_file="$temp_dir/$file"
+ jq -c '' < "$file" > "$new_file"
+ curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opensearch/_alerting/destinations"
+ rm "$new_file"
+ echo " "
+ }
+
+ #return cluster_id if exist
+ check_monitor() {
+ name=$1
+ curl -s $curl_opts -H "content-type: application/json" \
+ --data "{\"query\":{\"match\":{ \"monitor.name\": \"$name\"}}}" \
+ "$host/_opensearch/_alerting/monitors/_search" | jq -r '.hits.hits[0]._id'
+ }
+
+ create_monitor() {
+ file=$1
+ dest_id=$2
+ new_file="$temp_dir/$file"
+ echo "create monitor"
+ jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
+ curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opensearch/_alerting/monitors/"
+ rm "$new_file"
+ echo " "
+ }
+
+ update_monitor() {
+ file=$1
+ dest_id=$2
+ monitor_id=$3
+ new_file="$temp_dir/$file"
+ echo "update monitor"
+ jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
+ curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opensearch/_alerting/monitors/$monitor_id"
+ rm "$new_file"
+ echo " "
+ }
+
+ # handle the destination
+ handle_destination() {
+ dir=$dest_dir
+ mkdir -p "$temp_dir/$dir"
+ config_file=$dir/config.json
+ for k in $(jq ' keys | .[]' "$config_file"); do
+ file=$dir/$(jq -r ".[$k].file" "$config_file");
+
+ name=$(jq -r '.name' "$file")
+ id=$(check_destination "$name")
+ if [ -z "$id" ]; then
+ create_destination "$file"
+ else
+ update_destination "$file" "$id"
+ fi
+ done
+ }
+
+ # handle the monitors
+ handle_monitor() {
+ dir=$monitor_dir
+ mkdir -p "$temp_dir/$dir"
+ config_file=$dir/config.json
+ for k in $(jq ' keys | .[]' "$config_file"); do
+ file=$dir/$(jq -r ".[$k].file" "$config_file");
+ dest_name=$(jq -r ".[$k].destination" "$config_file");
+ format=$(jq -r ".[$k].format" "$config_file");
+
+ dest_id=$(check_destination "$dest_name")
+ if [ -z "$dest_id" ]; then
+ echo "destination doesn't exist, skip this monitor"
+ continue
+ fi
+
+ name=$(jq -r '.name' "$file")
+
+ ## Replace the slack output format
+ FILENAME=$(mktemp)
+ jq --arg slack "${format}" '.triggers[0].actions[0].message_template.source=$slack' "$file" > "$FILENAME"
+ monitor_id=$(check_monitor "$name")
+ if [ "$monitor_id" = "null" ]; then
+ create_monitor "$FILENAME" "$dest_id"
+ else
+ update_monitor "$FILENAME" "$dest_id" "$monitor_id"
+ fi
+ done
+
+ }
+
+ handle_settings() {
+ dir=$setting_dir
+ mkdir -p "$temp_dir/$dir"
+ config_file=$dir/config.json
+
+ for k in $(jq ' keys | .[]' "$config_file"); do
+ file=$dir/$(jq -r ".[$k].file" "$config_file");
+ type=$(jq -r ".[$k].type" "$config_file");
+ url=$(jq -r ".[$k].url" "$config_file");
+
+ new_file="$temp_dir/$file"
+ jq -c '' < "$file" > "$new_file"
+ curl -s $curl_opts $curl_opts -X $type -H "content-type: application/json" -d "@$new_file" "$host/$url"
+ rm "$new_file"
+ echo " "
+ done
+ }
+
+ if [ ! -z $setting_dir ]; then
+ echo "handle setting"
+ handle_settings
+ fi
+ if [ ! -z $dest_dir ]; then
+ echo "handle destination"
+ handle_destination
+ fi
+ if [ ! -z $monitor_dir ]; then
+ echo "handle monitor"
+ handle_monitor
+ fi
+
+ trap cleanup INT TERM
+ sleep infinity
diff --git a/logging/opensearch_setting/templates/deployment.yml b/logging/opensearch_setting/templates/deployment.yml
new file mode 100644
index 0000000..3f284a6
--- /dev/null
+++ b/logging/opensearch_setting/templates/deployment.yml
@@ -0,0 +1,82 @@
+# Copyright 2020-present Open Networking Foundation
+# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
+
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: "{{ .Release.Name }}"
+spec:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: opensearch-config
+ template:
+ metadata:
+ labels:
+ app: opensearch-config
+ spec:
+ containers:
+ - name: config
+ image: opennetworking/utils:jq-kubectl-curl
+ imagePullPolicy: IfNotPresent
+ env:
+ - name: ES_URL
+ value: "{{ .Values.elasticsearch.host }}:{{ .Values.elasticsearch.port}}"
+ - name: AUTH_USERNAME
+ value: "{{ .Values.elasticsearch.auth.username}}"
+ - name: AUTH_PASSWORD
+ value: "{{ .Values.elasticsearch.auth.password}}"
+ - name: SKIP_TLS
+ value: "{{ .Values.elasticsearch.auth.skip_tls}}"
+ {{ if .Values.configuration.setting }}
+ - name: SETTING_DIR
+ value: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.setting }}"
+ {{ end }}
+ {{ if .Values.configuration.monitor }}
+ - name: MONITOR_DIR
+ value: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.monitor }}"
+ {{ end }}
+ {{ if .Values.configuration.destination }}
+ - name: DEST_DIR
+ value: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.destination }}"
+ {{ end }}
+ volumeMounts:
+ - name: config-script
+ mountPath: /tmp/update.sh
+ subPath: update.sh
+ {{ if .Values.configuration.setting }}
+ - name: setting-configs
+ mountPath: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.setting }}"
+ {{ end }}
+ {{ if .Values.configuration.monitor }}
+ - name: monitor-configs
+ mountPath: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.monitor }}"
+ {{ end }}
+ {{ if .Values.configuration.destination }}
+ - name: destination-configs
+ mountPath: "{{ .Values.configuration.base_dir }}/{{ .Values.configuration.destination }}"
+ {{ end }}
+ command: ["sh", "-c", "/tmp/update.sh"]
+ volumes:
+ - name: "config-script"
+ configMap:
+ name: "{{ .Release.Name }}-scripts"
+ defaultMode: 0755
+ {{ if .Values.configuration.setting }}
+ - name: "setting-configs"
+ configMap:
+ name: "{{ .Values.configuration.setting_configmap }}"
+ defaultMode: 0644
+ {{ end }}
+ {{ if .Values.configuration.monitor }}
+ - name: "monitor-configs"
+ configMap:
+ name: "{{ .Values.configuration.monitor_configmap }}"
+ defaultMode: 0644
+ {{ end }}
+ {{ if .Values.configuration.destination }}
+ - name: "destination-configs"
+ secret:
+ secretName: "{{ .Values.configuration.destination_secret }}"
+ defaultMode: 0644
+ {{ end }}