video-iac/docker/coturn-dns/pod-node-register.py

63 lines
2.0 KiB
Python
Raw Normal View History

2025-04-06 22:57:45 +00:00
from ipaddress import ip_address, IPv4Address
2021-11-12 17:31:53 +00:00
from kubernetes import client, config
2021-08-09 19:37:53 +00:00
import boto3
import time
2021-08-12 17:40:06 +00:00
import os
2021-08-09 19:37:53 +00:00
2021-08-30 18:21:19 +00:00
HOSTED_ZONE=os.environ['HOSTED_ZONE']
2021-11-12 17:31:53 +00:00
COTURN_DOMAIN_NAME=os.environ['COTURN_DOMAIN_NAME']
2021-08-10 18:55:36 +00:00
2021-11-15 19:39:22 +00:00
config.load_incluster_config()
2021-11-12 17:31:53 +00:00
v1 = client.CoreV1Api()
2021-08-09 19:37:53 +00:00
2025-04-06 22:57:45 +00:00
def validIPAddress(IP: str) -> str:
try:
return "IPv4" if type(ip_address(IP)) is IPv4Address else "IPv6"
except ValueError:
return "Invalid"
2021-11-12 17:31:53 +00:00
while(True):
2026-01-07 02:45:02 +00:00
ips_set = set()
2021-08-09 19:37:53 +00:00
2026-01-07 14:27:29 +00:00
pods = v1.list_namespaced_pod(namespace="coturn", label_selector="app=coturn")
2025-04-06 22:57:45 +00:00
2021-11-12 17:31:53 +00:00
for i in pods.items:
2026-01-07 02:45:02 +00:00
if not i.spec.node_name:
continue
2021-11-12 17:31:53 +00:00
node_status = v1.read_node(name=i.spec.node_name)
for adr in node_status.status.addresses:
2025-04-06 22:57:45 +00:00
# only collect IPv4 addresses, because we are only updating A records here
if adr.type=="ExternalIP" and validIPAddress(adr.address) == "IPv4":
2026-01-07 02:45:02 +00:00
ips_set.add(adr.address)
2021-08-09 19:37:53 +00:00
2026-01-07 02:45:02 +00:00
ips = [{'Value': ip} for ip in sorted(list(ips_set))]
2021-08-09 19:37:53 +00:00
print("Node IPs: "+str(ips))
2021-11-12 17:31:53 +00:00
2026-01-07 02:45:02 +00:00
if not ips:
print("No IPs found to update. Sleeping.")
time.sleep(60)
continue
2021-08-09 19:37:53 +00:00
client = boto3.client('route53')
2026-01-07 02:45:02 +00:00
try:
response = client.change_resource_record_sets(
HostedZoneId=HOSTED_ZONE,
ChangeBatch= {
'Comment': 'COTURN NODES',
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': COTURN_DOMAIN_NAME,
'Type': 'A',
'TTL': 300,
'ResourceRecords': ips
}
}]
})
print("Successfully updated Route53: " + str(response['ChangeInfo']['Id']))
except Exception as e:
print(f"Error updating Route53: {e}")
2021-11-12 17:31:53 +00:00
time.sleep(60)