---
apiVersion: batch/v1
kind: CronJob
metadata:
  namespace: monitoring
  name: uptimerobot-heartbeat
spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: "Forbid"
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 5
  jobTemplate:
    spec:
      backoffLimit: 3
      ttlSecondsAfterFinished: 300
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: uptimerobot-heartbeat
              image: ghcr.io/k8s-at-home/kubectl:v1.22.0
              envFrom:
                - secretRef:
                    name: uptimerobot-heartbeat-url
              command:
                - "/bin/sh"
                - "-ec"
                - |
                  set -o nounset
                  set -o errexit
                  if [ -z "$UPTIMEROBOT_HEARTBEAT_URL" ]; then
                    printf "%s - Yikes - Missing UPTIMEROBOT_HEARTBEAT_URL environment variable" "$(date -u)"
                    exit 0
                  fi
                  status_code=$(curl --connect-timeout 10 --max-time 30 -I -s -o /dev/null -w '%{http_code}' "$UPTIMEROBOT_HEARTBEAT_URL")
                  if [ "${status_code}" != "200" ]; then
                    printf "%s - Yikes - Heartbeat request failed, http code: %s" "$(date -u)" "$status_code"
                    exit 0
                  fi
                  printf "%s - Success - Heartbeat request received and processed successfully" "$(date -u)"
