Documentation

Kubernetes CronJob

A CronJob that reports its result instead of silently ending up Failed.

A CronJob is the quietest kind of job: the pod dies, the job ends up Failed, and all of that is visible only to whoever happens to be watching the cluster. Reporting outwards settles it.

yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-report
spec:
  schedule: "0 3 * * *"
  timeZone: "Europe/Moscow"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 3
  jobTemplate:
    spec:
      backoffLimit: 2
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: job
              image: your/image:latest
              env:
                - name: PING
                  valueFrom:
                    secretKeyRef:
                      name: tickwatch
                      key: ping-url
              command: ["/bin/sh", "-c"]
              args:
                - |
                  set -o pipefail
                  wget -q -T 10 -O /dev/null "$PING/start" || true
                  if out=$(/app/run.sh 2>&1); then
                    echo "$out" | tail -c 10000 | \
                      wget -q -T 10 -O /dev/null --post-file=- "$PING" || true
                  else
                    code=$?
                    echo "$out" | tail -c 10000 | \
                      wget -q -T 10 -O /dev/null --post-file=- "$PING/$code" || true
                    exit $code
                  fi

Why a Secret, not a ConfigMap

The ping key is the right to report on behalf of your job. Whoever holds it can keep the monitor green while nothing actually runs. It exposes no data, but it does not belong in a shared ConfigMap either.

Kubernetes CronJob · Tickwatch