---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: monitoring
  name: uptimerobot-heartbeat
spec:
  schedule: "*/15 * * * *"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          annotations:
            botkube.io/disable: "true"
        spec:
          restartPolicy: Never
          containers:
          - name: uptimerobot-heartbeat
            image: curlimages/curl:7.74.0
            imagePullPolicy: IfNotPresent
            env:
            - name: HEALTHCHECK_URL
              valueFrom:
                secretKeyRef:
                  name: uptimerobot-heartbeat
                  key: url
            command:
            - "/bin/sh"
            - "-ec"
            - |
              set -o nounset
              set -o errexit
              if [ -z "${HEALTHCHECK_URL}" ]; then
                printf "%s - Error - Missing HEALTHCHECK_URL environment variable" "$(date -u)"
                exit 1
              fi
              status_code=$(curl --connect-timeout 10 --max-time 30 -I -s -o /dev/null -w '%{http_code}' "${HEALTHCHECK_URL}")
              if [ "${status_code}" != 200 ]; then
                printf "%s - ERROR - Heartbeat request failed, http code: %s" "$(date -u)" "${status_code}"
                exit 1
              fi
              printf "%s - Success - Heartbeat request received and processed successfully" "$(date -u)"
              exit 0
