This commit is contained in:
2025-08-25 20:09:20 +02:00
parent af5ad897c3
commit 891b723d1a

View File

@@ -66,15 +66,38 @@ spec:
command: ["/bin/bash","-ceu"]
args:
- |
IP="$(cat /work/ip)"
IP="$(cat /work/ip | tr -d '\r\n ')"
TMP=/work/Corefile
NEW=/work/Corefile.new
# Corefile laden
kubectl -n dns get cm coredns-corefile -o jsonpath='{.data.Corefile}' > "$TMP"
OLD="$(grep -oE 'answer \"\{\{\.Name\}\}[^"]* IN A [0-9.]+' "$TMP" | grep -oE '[0-9.]+' | head -n1 || true)"
if [ "$IP" = "$OLD" ] && [ -n "$OLD" ]; then
echo "IPv4 inchangée: $OLD"; exit 0
# Alte IPv4 aus Antwortzeilen extrahieren (falls vorhanden)
OLD="$(grep -Eo '^ *answer \"\{\{\.Name\}\}.* IN A [0-9.]+\"?$' "$TMP" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -n1 || true)"
# Wenn identisch und OLD vorhanden → nichts tun
if [ -n "${OLD:-}" ] && [ "$IP" = "$OLD" ]; then
echo "IPv4 unverändert: $OLD"
exit 0
fi
sed -E -i "s/(answer \"\{\{\.Name\}\}.* IN A )([0-9\\.]+)/\1${IP}/" "$TMP"
kubectl -n dns create configmap coredns-corefile --from-file=Corefile="$TMP" -o yaml --dry-run=client | kubectl apply -f -
kubectl -n dns rollout restart deploy/coredns-auth
# Neue Datei mit ausgetauschter IPv4 bauen (alle passenden answer-Zeilen)
cp "$TMP" "$NEW"
sed -E -i "s/(answer \"\{\{\.Name\}\}[^\"']* IN A )([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/\1${IP}/g" "$NEW"
# Wenn sich die Datei effektiv nicht geändert hat → nichts tun
if cmp -s "$TMP" "$NEW"; then
echo "Corefile unverändert, kein Restart nötig."
exit 0
fi
# ConfigMap nur bei Änderung updaten
kubectl -n dns create configmap coredns-corefile --from-file=Corefile="$NEW" -o yaml --dry-run=client | kubectl apply -f -
# Deployment nur bei Änderung neu starten: Annotation mit IP aktualisieren
kubectl -n dns patch deploy coredns-auth --type=merge -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"ddns-last-ip\":\"$IP\"}}}}}"
echo "CoreDNS aktualisiert: ${OLD:-<none>} -> $IP"
volumeMounts:
- { name: work, mountPath: /work }