Add retry logic to index creation script

- Re-add the retry logic back to the index creation script.
- Fixed small regex bug.
- Also added function to lookup the id of a view, because the new
  views API requires an id to set the default view.
- Set noglob to make sure the asterisks in the view names aren't
  expanded.

Change-Id: Idfd56f09a739731f2ce3153b8fc284bb499a91d4
This commit is contained in:
Mosher, Jaymes (jm616v) 2024-08-15 15:17:44 -06:00
parent 15f55f32ec
commit c393d87b0d
3 changed files with 47 additions and 15 deletions

View File

@ -15,7 +15,7 @@ apiVersion: v1
appVersion: v8.9.0 appVersion: v8.9.0
description: OpenStack-Helm Kibana description: OpenStack-Helm Kibana
name: kibana name: kibana
version: 0.1.17 version: 0.1.18
home: https://www.elastic.co/products/kibana home: https://www.elastic.co/products/kibana
sources: sources:
- https://github.com/elastic/kibana - https://github.com/elastic/kibana

View File

@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/}} */}}
set -ex set -ex
set -o noglob
create_data_view() { create_data_view() {
local index_name=$1 local index_name=$1
@ -37,42 +38,72 @@ data_view_exists() {
-H "kbn-xsrf: true" \ -H "kbn-xsrf: true" \
-H "Content-Type: application/json") -H "Content-Type: application/json")
if echo "$response" | grep -q "\"title\":\"${index_name}-[*]\""; then if echo "$response" | grep -Fq "\"title\":\"${index_name}-*\""; then
return 0 return 0
fi fi
return 1 return 1
} }
set_default_data_view() { set_default_data_view() {
local index_name=$1 local view_id=$1
curl -u "${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}" \ curl -u "${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}" \
--max-time 30 \ --max-time 30 \
-X POST "${KIBANA_ENDPOINT}/api/data_views/default" \ -X POST "${KIBANA_ENDPOINT}/api/data_views/default" \
-H "kbn-xsrf: true" \ -H "kbn-xsrf: true" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{ -d "{
\"value\": \"${index_name}-*\" \"data_view_id\": \"${view_id}\",
\"force\": true
}" }"
} }
find_and_set_python() {
pythons="python3 python python2"
for p in ${pythons[@]}; do
python=$(which ${p})
if [[ $? -eq 0 ]]; then
echo found python: ${python}
break
fi
done
}
get_view_id() {
local index_name=$1
local response=$(curl -s -u "${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}" \
--max-time 30 \
-X GET "${KIBANA_ENDPOINT}/api/data_views" \
-H "kbn-xsrf: true" \
-H "Content-Type: application/json" |
$python -c "import sys,json; j=json.load(sys.stdin); t=[x['id'] for x in j['data_view'] if x['title'] == '${index_name}-*']; print(t[0] if len(t) else '')"
)
echo $response
}
# Create data views # Create data views
{{- range $objectType, $indices := .Values.conf.create_kibana_indexes.indexes }} {{- range $objectType, $indices := .Values.conf.create_kibana_indexes.indexes }}
{{- range $indices }} {{- range $indices }}
if ! data_view_exists "{{ . }}"; then while true; do
create_data_view "{{ . }}" create_data_view "{{ . }}"
echo "Data view '{{ . }}' created successfully." if data_view_exists "{{ . }}"; then
else echo "Data view '{{ . }}-*' exists"
echo "Data view '{{ . }}' already exists." break
fi else
echo "Retrying creation of data view '{{ . }}-*' ..."
create_data_view "{{ . }}"
sleep 30
fi
done
{{- end }} {{- end }}
{{- end }} {{- end }}
# Ensure default data view exists and set it # Lookup default view id. The new Kibana view API requires the id
# instead of simply the name like the previous index API did.
find_and_set_python
default_index="{{ .Values.conf.create_kibana_indexes.default_index }}" default_index="{{ .Values.conf.create_kibana_indexes.default_index }}"
if ! data_view_exists "$default_index"; then default_index_id=$(get_view_id $default_index)
create_data_view "$default_index"
echo "Default data view '${default_index}' created successfully."
fi
set_default_data_view "$default_index" set_default_data_view "$default_index_id"
echo "Default data view set to '${default_index}'." echo "Default data view set to '${default_index}'."

View File

@ -18,4 +18,5 @@ kibana:
- 0.1.15 Use quay.io/airshipit/kubernetes-entrypoint:latest-ubuntu_focal by default - 0.1.15 Use quay.io/airshipit/kubernetes-entrypoint:latest-ubuntu_focal by default
- 0.1.16 Add 2024.1 Ubuntu Jammy overrides - 0.1.16 Add 2024.1 Ubuntu Jammy overrides
- 0.1.17 Update script to use data views replacing deprecated api - 0.1.17 Update script to use data views replacing deprecated api
- 0.1.18 Add retry logic to create_kibana_index_patterns.sh
... ...