# =============================================================================
# ConfigMap: backup-restore-test-scripts — ADR-074 M4
# =============================================================================
# 掛載備份還原驗證腳本到 CronJob Pod
#
# 2026-04-12 ogt (ADR-074 M4)
# =============================================================================

apiVersion: v1
kind: ConfigMap
metadata:
  name: backup-restore-test-scripts
  namespace: velero
  labels:
    app: awoooi
    component: backup-restore-test
    adr: "074-m4"
data:
  cron_backup_restore_test.sh: |
    #!/bin/sh
    set -e

    TEXTFILE="/var/lib/node_exporter/textfile_collector/backup_restore_test.prom"
    NAMESPACE="${VELERO_NAMESPACE:-velero}"
    BACKUP_NAME="${VELERO_BACKUP_NAME:-awoooi-daily}"
    EXIT_CODE=0

    echo "=== backup-restore-test: $(date '+%Y-%m-%d %H:%M:%S %Z') ==="
    echo "Backup: ${BACKUP_NAME}  Namespace: ${NAMESPACE}"

    velero restore create \
      --from-backup "${BACKUP_NAME}" \
      --namespace-mappings "${NAMESPACE}:restore-test-dry" \
      --dry-run \
      --wait \
      2>&1 || EXIT_CODE=$?

    TS=$(date +%s%3N)
    mkdir -p "$(dirname "${TEXTFILE}")"

    if [ "${EXIT_CODE}" -eq 0 ]; then
      echo "backup restore dry-run OK"
      printf '# HELP awoooi_backup_restore_test_success 1 = last backup restore dry-run succeeded\n# TYPE awoooi_backup_restore_test_success gauge\nawoooi_backup_restore_test_success 1 %s\n# HELP awoooi_backup_restore_test_timestamp_seconds Unix timestamp of last test run\n# TYPE awoooi_backup_restore_test_timestamp_seconds gauge\nawoooi_backup_restore_test_timestamp_seconds %s %s\n' "${TS}" "$(date +%s)" "${TS}" > "${TEXTFILE}"
      exit 0
    else
      echo "backup restore dry-run FAILED (exit ${EXIT_CODE})"
      printf '# HELP awoooi_backup_restore_test_success 1 = last backup restore dry-run succeeded\n# TYPE awoooi_backup_restore_test_success gauge\nawoooi_backup_restore_test_success 0 %s\n# HELP awoooi_backup_restore_test_timestamp_seconds Unix timestamp of last test run\n# TYPE awoooi_backup_restore_test_timestamp_seconds gauge\nawoooi_backup_restore_test_timestamp_seconds %s %s\n' "${TS}" "$(date +%s)" "${TS}" > "${TEXTFILE}"
      exit 1
    fi
