49 lines
1.1 KiB
Bash
Executable File
49 lines
1.1 KiB
Bash
Executable File
#!/bin/sh
|
|
# Generates CoreDNS configmap YAML from records.yaml
|
|
# Usage: ./generate.sh records.yaml > coredns-custom.yaml
|
|
set -e
|
|
|
|
RECORDS_FILE="${1:-records.yaml}"
|
|
|
|
# Parse records.yaml, group by IP, generate CoreDNS template blocks
|
|
# Uses only POSIX tools (awk)
|
|
|
|
cat <<'HEADER'
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: coredns-custom
|
|
namespace: kube-system
|
|
data:
|
|
homelab.override: |
|
|
HEADER
|
|
|
|
awk '
|
|
/^[a-zA-Z0-9]/ && /:/ {
|
|
gsub(/"/, "")
|
|
split($0, parts, ":")
|
|
host = parts[1]
|
|
gsub(/^[ \t]+|[ \t]+$/, "", host)
|
|
ip = parts[2]
|
|
gsub(/^[ \t]+|[ \t]+$/, "", ip)
|
|
if (host != "" && ip != "") {
|
|
ips[ip] = ips[ip] ? ips[ip] "|" host : host
|
|
}
|
|
}
|
|
END {
|
|
for (ip in ips) {
|
|
n = split(ips[ip], hosts, "|")
|
|
regex = ""
|
|
for (i = 1; i <= n; i++) {
|
|
gsub(/\./, "\\.", hosts[i])
|
|
regex = regex ? regex "|" hosts[i] : hosts[i]
|
|
}
|
|
printf " template IN A {\n"
|
|
printf " match \"^(%s)\\.$\"\n", regex
|
|
printf " answer \"{{ .Name }} 60 IN A %s\"\n", ip
|
|
printf " fallthrough\n"
|
|
printf " }\n"
|
|
}
|
|
}
|
|
' "$RECORDS_FILE"
|