# services/logging/bootstrap-jobs/opensearch-dashboards-setup-job.yaml
# One-off job for logging/opensearch-dashboards-setup-4.
# Purpose: opensearch dashboards setup 4 (see container args/env in this file).
# Run by setting spec.suspend to false, reconcile, then set it back to true.
# Safe to delete the finished Job/pod; it should not run continuously.
apiVersion: batch/v1
kind: Job
metadata:
  name: opensearch-dashboards-setup-4
  namespace: logging
spec:
  suspend: true
  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

              if [ ! -s /config/objects.ndjson ]; then
                echo "Saved objects file not found at /config/objects.ndjson" >&2
                exit 1
              fi

              import_code="$(curl -sS -o /tmp/import.json -w "%{http_code}" -X POST \
                "${OSD_URL}/api/saved_objects/_import?overwrite=true" \
                -H 'osd-xsrf: true' \
                -F file=@/config/objects.ndjson)"

              if [ "${import_code}" != "200" ]; then
                echo "Saved object import failed with status ${import_code}:" >&2
                cat /tmp/import.json >&2
                exit 1
              fi

              settings_code="$(curl -sS -o /tmp/settings.json -w "%{http_code}" -X POST \
                "${OSD_URL}/api/opensearch-dashboards/settings" \
                -H 'Content-Type: application/json' \
                -H 'osd-xsrf: true' \
                -d '{"changes":{"defaultIndex":"kube-logs"}}')"

              if [ "${settings_code}" != "200" ]; then
                echo "Default index update failed with status ${settings_code}:" >&2
                cat /tmp/settings.json >&2
                exit 1
              fi
          volumeMounts:
            - name: objects
              mountPath: /config
              readOnly: true
      volumes:
        - name: objects
          configMap:
            name: opensearch-dashboards-objects
