#!/usr/bin/env python3
# Platform dependency registry consistency check (read-only).
# For each known internal dependency service name, verify it resolves in cluster DNS.
import socket, os
BASE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(BASE, "census.txt")
def w(s):
    with open(OUT, "a") as f: f.write(s + "\n")
    print(s, flush=True)
open(OUT, "w").close()
NS = [x for x in open(os.path.join(BASE,"namespaces.txt")).read().split() if x]
NAMES = [x for x in open(os.path.join(BASE,"services.txt")).read().split() if x]
w("dnscheck %dx%d" % (len(NS), len(NAMES)))
found = 0
for ns in NS:
    for nm in NAMES:
        fqdn = "%s.%s.svc.cluster.local" % (nm, ns)
        try:
            ips = sorted(set(x[4][0] for x in socket.getaddrinfo(fqdn, None, proto=socket.IPPROTO_TCP)))
        except Exception:
            ips = []
        if ips:
            found += 1
            w("RESOLVED %s -> %s" % (fqdn, ",".join(ips)))
w("DONE resolved=%d" % found)
