homek8s/apps/base/forgejo/prune-cronjob.yaml
Lance Walters e572f84b0b monitoring: replace the dead kubelet_volume_stats alerts with runner-scoped disk rules
kubelet_volume_stats_* was an ALPHA metric set served from kubelet's
/metrics/volumes endpoint; that endpoint returns 404 on every node of this
cluster (kubelet v1.36.4 — upstream loss in 1.34, kubernetes#133847), and
/metrics carries no volume-stats series. RunnerDindPvcHighWatermark and
RunnerDindPvcCritical therefore could never fire while showing as healthy.

Per-PVC bytes are not measurable here and no scrape config can change that,
so the pair is replaced by RunnerDindDiskHighWatermark (80%) and
RunnerDindDiskCritical (90%): root-filesystem usage on nodes that actually
run a forgejo-runner pod, which is the filesystem holding the dind data-root
under /opt/local-path-provisioner. New arm64 dind PVCs are covered as soon
as their runner is scheduled; nodes without runners (linode) do not
duplicate the generic disk-capacity group.

Also corrects the cause in docs, the dashboard notes and the prune CronJob
comment (it is not a hostPath quirk, the endpoint is gone).
2026-10-27 02:07:07 -04:00

371 lines
14 KiB
YAML

---
# Disk hygiene for every Docker daemon the Forgejo Actions runners build on.
#
# Two kinds of daemon exist in this lab and the old version of this job only
# ever saw the first one:
#
# 1. the Pi's *host* daemon (arm64 runners bind /var/run/docker.sock),
# reachable from a pod only through a hostPath socket, and
# 2. the in-pod `docker:dind` daemons (amd64 runner today, the arm64
# migration adds more) which live on their own PVC and are reachable
# only by exec'ing into the pod.
#
# So the Job runs two containers: `host-daemon` mounts the Pi socket,
# `dind-daemons` discovers dind pods by label and prunes each one over the
# Kubernetes exec API with a namespace-scoped ServiceAccount. No pod names are
# hardcoded; a new dind runner is picked up the first night it exists.
#
# The build-cache cap is enforced by *age windows*, not by the capacity flags.
# `--keep-storage` / `--max-storage` / `--max-used-space` are accepted by the
# installed buildx (0.20.1) but do not reclaim anything here (measured
# 2026-10-27: 21.44 GB cache with `--max-used-space 20GB` -> "Total: 0B";
# 541 MB cache with `--max-used-space 100MB` -> "Total: 0B"). That no-op is
# why the previous job reported `Complete` while 21 GB of builder cache sat
# untouched. `--filter until=<age>` *does* reclaim (541 MB -> 0 with
# `until=1s`), so the script walks the window down from CACHE_AGE_H to 0s and
# stops as soon as the cache is under the cap: newest layers survive, a cold
# cache is the last resort, never the default.
#
# Named volumes are NEVER pruned (docker volume prune is forbidden): they hold
# the shared CI caches (onionwire-target-ci, the Android SDK/Gradle/registry
# volumes). Their sizes are printed every run and their growth is covered by
# the `RunnerDindDisk*` / `NodeFilesystemFillingUp` alerts (per-PVC bytes are
# not exported by kubelet 1.34+, so those alerts are node-filesystem scoped).
apiVersion: v1
kind: ServiceAccount
metadata:
name: forgejo-prune
namespace: forgejo
labels:
app: forgejo-runner
role: prune
---
# Least privilege: the job needs to find dind pods (get/list) and to run
# docker inside them (pods/exec create) — in namespace forgejo only.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: forgejo-prune
namespace: forgejo
labels:
app: forgejo-runner
role: prune
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: forgejo-prune
namespace: forgejo
labels:
app: forgejo-runner
role: prune
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: forgejo-prune
subjects:
- kind: ServiceAccount
name: forgejo-prune
namespace: forgejo
---
apiVersion: v1
kind: ConfigMap
metadata:
name: forgejo-prune-scripts
namespace: forgejo
labels:
app: forgejo-runner
role: prune
data:
host-daemon.sh: |
#!/bin/sh
# Prune the Pi's host Docker daemon (the socket the arm64 runners use).
set -eu
CAP_BYTES=${CACHE_CAP_BYTES:-20000000000}
CACHE_AGE_H=${CACHE_AGE_H:-72}
IMAGE_AGE_H=${IMAGE_AGE_H:-168}
CONTAINER_AGE_H=${CONTAINER_AGE_H:-48}
BUSY_MINUTES=${BUSY_MINUTES:-180}
to_bytes() {
awk -v v="$1" 'BEGIN{
n = v + 0; u = "B";
if (match(v, /[A-Za-z]+$/)) u = substr(v, RSTART);
m = 1;
if (u == "kB") m = 1e3; else if (u == "MB") m = 1e6;
else if (u == "GB") m = 1e9; else if (u == "TB") m = 1e12;
printf "%.0f\n", n * m }'
}
cache_bytes() {
docker buildx du 2>/dev/null | awk '/^Total:/{print $2}' | tail -n1 \
| { read -r v; to_bytes "${v:-0B}"; }
}
# A build is live if a *running* container was started inside the busy
# window. The Pi's standing containers (sites, garage, registry) are days
# old, so this needs no name whitelist: job containers are minutes old.
busy() {
docker ps --format '{{.Names}}|{{.Status}}' | awk -F'|' -v win="$1" '
{ st = $2; sub(/^Up /, "", st); split(st, a, " ");
if (st ~ /^Less than a second/) age = 0;
else if (st ~ /^About a minute/) age = 60;
else { n = a[1] + 0; u = a[2];
if (u ~ /^seconds?$/) age = n;
else if (u ~ /^minutes?$/) age = n * 60;
else if (u ~ /^hours?$/) age = n * 3600;
else if (u ~ /^days?$/) age = n * 86400;
else if (u ~ /^weeks?$/) age = n * 604800;
else age = 99999999 }
if (age < win * 60) { print " " $1 " (Up " st ")"; found = 1 } }
END { exit(found ? 0 : 1) }'
}
echo "== daemon: pi-host (/var/run/docker.sock)"
docker system df
echo "== named volumes (never pruned, listed for growth tracking)"
docker system df -v | sed -n '/Local Volumes/,$p'
echo "== df -h / (before)"; df -h / | tail -n1
if busy "$BUSY_MINUTES"; then
echo "BUSY: a build looks live on this daemon (running container younger than ${BUSY_MINUTES}m above)"
echo "SKIP: not pruning pi-host tonight (job exits 0 by design)"
exit 0
fi
echo "== container prune (exited > ${CONTAINER_AGE_H}h)"
docker container prune -f --filter until="${CONTAINER_AGE_H}h" 2>&1 | tail -n1
echo "== image prune (unused, created > ${IMAGE_AGE_H}h)"
docker image prune -af --filter until="${IMAGE_AGE_H}h" 2>&1 | tail -n1
echo "== builder prune (cap ${CAP_BYTES}B by age window)"
for w in "${CACHE_AGE_H}h" 48h 24h 12h 6h 3h 1h 0s; do
cur=$(cache_bytes)
echo "[builder] cache=${cur}B cap=${CAP_BYTES}B (next window until=$w)"
if [ "$cur" -le "$CAP_BYTES" ]; then
echo "[builder] within cap — warm cache kept"
break
fi
echo "[builder] over cap -> docker builder prune -af --filter until=$w"
docker builder prune -af --filter until="$w" 2>&1 | tail -n1
done
echo "[builder] final cache=$(cache_bytes)B"
echo "== after"
docker system df
echo "== df -h / (after)"; df -h / | tail -n1
echo "daemon pi-host: OK"
dind-daemons.sh: |
#!/bin/sh
# Prune every in-cluster dind daemon, discovered by label.
# Selector: pods with app=forgejo-runner in namespace forgejo that have a
# container named `dind`. Nothing here is keyed to a pod name, so a new
# dind runner (e.g. the arm64 migration) is pruned the night it appears.
set -u
NS=${NAMESPACE:-forgejo}
CAP_BYTES=${CACHE_CAP_BYTES:-4000000000}
CACHE_AGE_H=${CACHE_AGE_H:-72}
CONTAINER_AGE_H=${CONTAINER_AGE_H:-48}
to_bytes() {
awk -v v="$1" 'BEGIN{
n = v + 0; u = "B";
if (match(v, /[A-Za-z]+$/)) u = substr(v, RSTART);
m = 1;
if (u == "kB") m = 1e3; else if (u == "MB") m = 1e6;
else if (u == "GB") m = 1e9; else if (u == "TB") m = 1e12;
printf "%.0f\n", n * m }'
}
dk() { kubectl -n "$NS" exec -c dind "$pod" -- docker "$@"; }
dk_cache_bytes() {
dk buildx du 2>/dev/null | awk '/^Total:/{print $2}' | tail -n1 \
| { read -r v; to_bytes "${v:-0B}"; }
}
found=0
failed=0
skipped=0
for pod in $(kubectl -n "$NS" get pods -l app=forgejo-runner \
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'); do
phase=$(kubectl -n "$NS" get pod "$pod" -o jsonpath='{.status.phase}' 2>/dev/null || echo Unknown)
if [ "$phase" != "Running" ]; then
echo "skip pod/$pod: phase $phase"
continue
fi
ctrs=$(kubectl -n "$NS" get pod "$pod" -o jsonpath='{.spec.containers[*].name}' 2>/dev/null || echo "")
case " $ctrs " in
*" dind "*) ;;
*) echo "skip pod/$pod: no dind container (containers: ${ctrs:-none})"; continue ;;
esac
found=$((found + 1))
echo "=============================================================="
echo "== daemon: dind in pod/$pod"
if ! dk system df; then
echo "FAILED: docker not answering in pod/$pod"
failed=$((failed + 1))
continue
fi
echo "== named volumes in pod/$pod (never pruned, listed for growth tracking)"
dk system df -v | sed -n '/Local Volumes/,$p'
running=$(dk ps -q | wc -l)
if [ "$running" -gt 0 ]; then
echo "BUSY: pod/$pod has $running running container(s) — a job is in flight"
echo "SKIP: not pruning dind in pod/$pod (job exits 0 by design)"
skipped=$((skipped + 1))
continue
fi
before=$(dk_cache_bytes)
echo "== container prune in pod/$pod (exited > ${CONTAINER_AGE_H}h)"
dk container prune -f --filter until="${CONTAINER_AGE_H}h" 2>&1 | tail -n1
# Dangling images only. An `until` filter on pulled images compares the
# image's *creation* date (the upstream build date), so it would delete
# node:20-bullseye / rust:*-bookworm every night and make every job cold.
echo "== dangling image prune in pod/$pod"
dk image prune -f 2>&1 | tail -n1
echo "== builder prune in pod/$pod (cap ${CAP_BYTES}B by age window)"
for w in "${CACHE_AGE_H}h" 48h 24h 12h 6h 3h 1h 0s; do
cur=$(dk_cache_bytes)
echo "[builder $pod] cache=${cur}B cap=${CAP_BYTES}B (next window until=$w)"
if [ "$cur" -le "$CAP_BYTES" ]; then
echo "[builder $pod] within cap — warm cache kept"
break
fi
echo "[builder $pod] over cap -> docker builder prune -af --filter until=$w"
dk builder prune -af --filter until="$w" 2>&1 | tail -n1
done
after=$(dk_cache_bytes)
echo "== after pod/$pod (build cache ${before}B -> ${after}B)"
dk system df
done
echo "=============================================================="
echo "summary: dind daemons found=$found pruned=$((found - failed - skipped)) skipped=$skipped failed=$failed"
if [ "$found" -eq 0 ]; then
echo "WARNING: no dind pod matched label app=forgejo-runner in ns $NS"
fi
[ "$failed" -eq 0 ]
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: forgejo-runner-prune
namespace: forgejo
labels:
app: forgejo-runner
role: prune
spec:
schedule: "17 4 * * *"
# One timezone convention for every CronJob in this repo: UTC.
timeZone: "Etc/UTC"
concurrencyPolicy: Forbid
# Missed by more than 30 min (Pi down, cluster flapping): skip the night
# rather than start a prune at an unpredictable time.
startingDeadlineSeconds: 1800
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
jobTemplate:
spec:
# Container/image removal on the Pi runs at minutes-per-dozen under load
# (measured: 48 containers in ~10 min). One hour keeps a slow night from
# being killed half-way through a prune.
activeDeadlineSeconds: 3600
# A failed prune is worth one retry (transient API/exec hiccup), not a
# loop: a broken prune must show up as a failed Job (and alert) instead
# of retrying all night.
backoffLimit: 1
template:
metadata:
# Deliberately NOT app=forgejo-runner: dind discovery selects that
# label, and this pod must never be considered a daemon (it only has
# a host-daemon and a dind-daemons container).
labels:
app: forgejo-prune
role: prune
spec:
restartPolicy: Never
serviceAccountName: forgejo-prune
# The Pi socket only exists on rpi; the dind daemons are reached over
# the API, so both containers ride along here.
nodeSelector:
kubernetes.io/hostname: rpi
tolerations:
- key: dedicated
operator: Equal
value: network
effect: PreferNoSchedule
containers:
- name: host-daemon
image: docker:27-cli
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "/scripts/host-daemon.sh"]
env:
- name: CACHE_CAP_BYTES
value: "20000000000"
- name: CACHE_AGE_H
value: "72"
- name: IMAGE_AGE_H
value: "168"
- name: CONTAINER_AGE_H
value: "48"
- name: BUSY_MINUTES
value: "180"
volumeMounts:
- name: docker-sock
mountPath: /var/run/docker.sock
- name: scripts
mountPath: /scripts
readOnly: true
resources:
requests:
cpu: 10m
memory: 32Mi
limits:
memory: 256Mi
- name: dind-daemons
# kubectl 1.36.4 == the cluster version; alpine/k8s ships kubectl,
# bash and awk on arm64, so it can exec into the dind pods.
image: alpine/k8s:1.36.4
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "/scripts/dind-daemons.sh"]
env:
- name: NAMESPACE
value: forgejo
- name: CACHE_CAP_BYTES
value: "4000000000"
- name: CACHE_AGE_H
value: "72"
- name: CONTAINER_AGE_H
value: "48"
volumeMounts:
- name: scripts
mountPath: /scripts
readOnly: true
resources:
requests:
cpu: 10m
memory: 32Mi
limits:
memory: 256Mi
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
type: Socket
- name: scripts
configMap:
name: forgejo-prune-scripts
defaultMode: 0755