46 lines
1.7 KiB
Bash

#!/bin/sh
set -eu
ONE_SHOT=${ONE_SHOT:-false}
SWEEP_INTERVAL_SEC=${SWEEP_INTERVAL_SEC:-21600}
EMERGENCY_USAGE_PERCENT=${EMERGENCY_USAGE_PERCENT:-85}
LOG_RETENTION_DAYS=${LOG_RETENTION_DAYS:-7}
ORPHAN_POD_RETENTION_DAYS=${ORPHAN_POD_RETENTION_DAYS:-3}
JOURNAL_MAX_SIZE=${JOURNAL_MAX_SIZE:-200M}
sweep_once() {
usage=$(df -P /host | awk 'NR==2 {gsub(/%/,"",$5); print $5}') || usage=""
# Kubelet owns container images and runtime state, including garbage collection.
# Keep this legacy-named helper limited to host logs and package-cache cleanup.
# Kubelet owns active logs; retain every active UID, including quiet pods.
python3 /scripts/node_pod_log_cleanup.py --host-root /host \
--retention-days "${ORPHAN_POD_RETENTION_DAYS}"
if [ -d /host/var/log.hdd/containers ]; then
find /host/var/log.hdd/containers -xtype l -print -delete 2>/dev/null || true
fi
if [ -n "${usage}" ] && [ "${usage}" -ge "${EMERGENCY_USAGE_PERCENT}" ]; then
# Emergency pass for rootfs pressure on SD-backed nodes.
chroot /host /bin/sh -c "journalctl --vacuum-size='${JOURNAL_MAX_SIZE}' >/dev/null 2>&1 || true"
# Pod logs are handled only by the UID-aware check above.
find /host/var/log -path /host/var/log/pods -prune -o -type f -name "*.gz" -mtime +"${LOG_RETENTION_DAYS}" -print -exec rm -f {} \; 2>/dev/null || true
find /host/var/log.hdd -path /host/var/log.hdd/pods -prune -o -type f -name "*.gz" -mtime +"${LOG_RETENTION_DAYS}" -print -exec rm -f {} \; 2>/dev/null || true
chroot /host /bin/sh -c "if command -v apt-get >/dev/null 2>&1; then apt-get clean >/dev/null 2>&1 || true; fi"
fi
}
sweep_once
touch /tmp/initial-sweep-complete
if [ "${ONE_SHOT}" = "true" ]; then
exit 0
fi
while true; do
sleep "${SWEEP_INTERVAL_SEC}"
sweep_once
done