titan-iac/services/logging/opensearch-dashboards-setup-job.yaml

64 lines
2.2 KiB
YAML

# services/logging/opensearch-dashboards-setup-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: opensearch-dashboards-setup-1
namespace: logging
spec:
backoffLimit: 3
ttlSecondsAfterFinished: 3600
template:
spec:
restartPolicy: OnFailure
nodeSelector:
node-role.kubernetes.io/worker: "true"
hardware: rpi5
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: hardware
operator: In
values:
- rpi5
containers:
- name: setup
image: alpine:3.20
command: ["/bin/sh", "-c"]
args:
- |
set -euo pipefail
apk add --no-cache curl >/dev/null
OSD_URL="http://opensearch-dashboards.logging.svc.cluster.local:5601"
for attempt in $(seq 1 60); do
code="$(curl -s -o /dev/null -w "%{http_code}" "${OSD_URL}/api/status" || true)"
if [ "${code}" = "200" ]; then
break
fi
sleep 5
done
if ! curl -s -o /dev/null -w "%{http_code}" "${OSD_URL}/api/status" | grep -q "200"; then
echo "OpenSearch Dashboards did not become ready in time" >&2
exit 1
fi
create_view() {
view_id="$1"
title="$2"
curl -sS -X POST "${OSD_URL}/api/saved_objects/index-pattern/${view_id}?overwrite=true" \
-H 'Content-Type: application/json' \
-H 'osd-xsrf: true' \
-d "{\"attributes\":{\"title\":\"${title}\",\"timeFieldName\":\"@timestamp\"}}" >/dev/null
}
create_view kube-logs "kube-*"
create_view journald-logs "journald-*"
curl -sS -X POST "${OSD_URL}/api/opensearch-dashboards/settings" \
-H 'Content-Type: application/json' \
-H 'osd-xsrf: true' \
-d '{"changes":{"defaultIndex":"kube-logs"}}' >/dev/null