10
0

Add replica support
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Lauri Võsandi 2022-09-24 08:43:25 +03:00
parent 061916c1d5
commit e13ef93836

View File

@ -1,13 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import asyncio import asyncio
import ecs_logging import ecs_logging
import logging import logging
import os import os
import sys import signal
import socket
from kubernetes_asyncio.client.api_client import ApiClient from kubernetes_asyncio.client.api_client import ApiClient
from kubernetes_asyncio import client, config, watch from kubernetes_asyncio import client, config, watch
from time import time from time import time
parser = argparse.ArgumentParser()
parser.add_argument("--replica", action="store_true")
args = parser.parse_args()
# Get the Logger # Get the Logger
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
@ -17,14 +23,6 @@ handler = logging.StreamHandler()
handler.setFormatter(ecs_logging.StdlibFormatter()) handler.setFormatter(ecs_logging.StdlibFormatter())
logger.addHandler(handler) logger.addHandler(handler)
ZONEFILE_TEMPLATE = """
@ IN SOA ns1.%(zone)s. hostmaster.%(zone)s. (2022060820 300 300 300 300)
NS ns1.%(zone)s.
ns1 A %(ip)s
"""
_, ip = sys.argv
PATH_CONFIG = "/var/bind/bind.conf" PATH_CONFIG = "/var/bind/bind.conf"
PATH_ZONEFILE = "/var/bind/db.%s" PATH_ZONEFILE = "/var/bind/db.%s"
@ -32,11 +30,21 @@ active_zones = set()
def update_config(): def update_config():
master = socket.gethostbyname("bind-primary")
with open(PATH_CONFIG + ".part", "w") as fh: with open(PATH_CONFIG + ".part", "w") as fh:
for zone in active_zones: for zone in active_zones:
path = PATH_ZONEFILE % zone path = PATH_ZONEFILE % zone
fh.write("zone \"%s\" { type master; file \"%s\"; };\n" % (zone, path)) if args.replica:
fh.write("zone \"%s\" { type slave; masters { %s key zone-transfer; }; };\n" % (zone, master))
else:
fh.write("zone \"%s\" { type master; file \"%s\"; };\n" % (zone, path))
os.rename(PATH_CONFIG + ".part", PATH_CONFIG) os.rename(PATH_CONFIG + ".part", PATH_CONFIG)
logger.debug("File %s updated", PATH_CONFIG)
try:
with open("/var/bind/named.pid") as fh:
os.kill(int(fh.read()), signal.SIGHUP)
except FileNotFoundError:
logger.warn("File /var/bind/named.pid not found, assuming Bind not running yet")
async def update_loop(v1, apps_api, api_instance, revision): async def update_loop(v1, apps_api, api_instance, revision):
@ -50,15 +58,15 @@ async def update_loop(v1, apps_api, api_instance, revision):
if event["type"] == "ADDED": if event["type"] == "ADDED":
zone = event["object"]["metadata"]["name"] zone = event["object"]["metadata"]["name"]
active_zones.add(zone) active_zones.add(zone)
path = PATH_ZONEFILE % zone if not args.replica:
logger.info("Adding zone: %s", zone) path = PATH_ZONEFILE % zone
if not os.path.exists(path): logger.info("Adding zone: %s", zone)
with open(path + ".part", "w") as fh: if not os.path.exists(path):
fh.write(ZONEFILE_TEMPLATE % { with open(path + ".part", "w") as fh:
"ip": ip, fh.write("@ IN SOA ns1.%(zone)s. hostmaster.%(zone)s. (1 300 300 300 300)\n" % locals())
"zone": zone fh.write(" NS ns1.%(zone)s.\n" % locals())
}) fh.write("ns1 A 127.0.0.1\n")
os.rename(path + ".part", path) os.rename(path + ".part", path)
update_config() update_config()
elif event["type"] == "DELETED": elif event["type"] == "DELETED":
logger.info("Removing zone: %s", zone) logger.info("Removing zone: %s", zone)