apiVersion: batch/v1 kind: CronJob metadata: name: blackbox-target-sync namespace: monitoring spec: schedule: "*/15 * * * *" concurrencyPolicy: Forbid successfulJobsHistoryLimit: 3 failedJobsHistoryLimit: 5 startingDeadlineSeconds: 300 jobTemplate: spec: backoffLimit: 2 activeDeadlineSeconds: 300 template: metadata: labels: app.kubernetes.io/name: blackbox-target-sync spec: serviceAccountName: blackbox-sync restartPolicy: Never containers: - name: sync image: alpine/k8s:1.36.0 imagePullPolicy: IfNotPresent env: - name: WHM_TOKEN valueFrom: secretKeyRef: name: whm-api-token key: token - name: WHM_HOST valueFrom: secretKeyRef: name: whm-api-token key: host command: - /bin/sh - -ec args: - | echo "Installing required packages..." apk add --no-cache curl jq ca-certificates >/dev/null API_URL="https://${WHM_HOST}:2087/json-api/listaccts?api.version=1" echo "Requesting account list from WHM host: ${WHM_HOST}" RESPONSE="$( curl \ --silent \ --show-error \ --fail-with-body \ --connect-timeout 10 \ --max-time 60 \ --header "Authorization: whm root:${WHM_TOKEN}" \ "${API_URL}" )" API_RESULT="$( printf '%s' "${RESPONSE}" | jq -r '.metadata.result // 0' )" if [ "${API_RESULT}" != "1" ]; then echo "ERROR: WHM API returned an unsuccessful result." printf '%s' "${RESPONSE}" | jq . exit 1 fi printf '%s' "${RESPONSE}" | jq -r ' .data.acct[]? | .domain | select(type == "string") | select(length > 0) ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | grep -E '^[A-Za-z0-9.-]+\.[A-Za-z]{2,}$' | tr '[:upper:]' '[:lower:]' | sort -u \ > /tmp/domains.txt DOMAIN_COUNT="$( wc -l < /tmp/domains.txt | tr -d ' ' )" echo "Found ${DOMAIN_COUNT} unique main domains." if [ "${DOMAIN_COUNT}" -eq 0 ]; then echo "ERROR: WHM returned zero valid domains." echo "Refusing to overwrite the existing ConfigMap." exit 1 fi jq -R -s ' split("\n") | map(select(length > 0)) | map("https://" + .) | [ { targets: ., labels: { environment: "production", source: "whm-listaccts" } } ] ' /tmp/domains.txt > /tmp/targets.json echo "Generated target file:" jq . /tmp/targets.json TARGET_COUNT="$( jq 'map(.targets | length) | add // 0' /tmp/targets.json )" if [ "${TARGET_COUNT}" -eq 0 ]; then echo "ERROR: Generated target file contains zero targets." exit 1 fi kubectl create configmap blackbox-targets \ --namespace monitoring \ --from-file=targets.json=/tmp/targets.json \ --dry-run=client \ --output=yaml | kubectl apply --filename=- echo "Successfully synchronized ${TARGET_COUNT} Blackbox targets." resources: requests: cpu: 25m memory: 32Mi limits: cpu: 200m memory: 128Mi