-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatedns.py
71 lines (64 loc) · 2.4 KB
/
updatedns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from area53 import route53
from boto.route53.exception import DNSServerError
from kubernetes import client, config
from datetime import datetime
import socket
import time
def getKeysByValue(dictOfElements, valueToFind):
listOfKeys = list()
listOfItems = dictOfElements.items()
for item in listOfItems:
if item[1] == valueToFind:
listOfKeys.append(item[0])
return listOfKeys
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
hostnames = {
"api.bayescluster.com":"default/ambassador",
"ide.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"mlflow.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"grafana.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"kibana.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"superset.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"jenkins.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"manager.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"airbyte.bayescluster.com":"ingress-nginx/ingress-nginx-controller",
"spark.bayescluster.com":"default/spark-ui-proxy",
"recruit.bayescluster.com":"default/docassemble-nginx-ingress"
}
addresses = {}
v1 = client.CoreV1Api()
print("Listing services with their IPs:")
ret = v1.list_service_for_all_namespaces(watch=False)
for i in ret.items:
key = "{}/{}".format(i.metadata.namespace, i.metadata.name)
if i.status.load_balancer.ingress is not None:
hostname = i.status.load_balancer.ingress[0].hostname
while True:
try:
ip = socket.gethostbyname(hostname)
break
except:
print("Cannot resolve hostname. Retrying in 10 seconds")
time.sleep(10)
pass
if key in hostnames.values():
hosts = getKeysByValue(hostnames,key)
for host in hosts:
addresses[host] = ip
print("Service",key,"has IP",ip)
datestr = '"Last update %s."' % datetime.utcnow().strftime('%Y-%m-%d %H:%M')
for i, (k, v) in enumerate(addresses.items()):
fqdn = k
dot = k[:k.rfind(".")].rfind(".")
subdomain = k[0:dot]
domain = k[dot+1:]
print(subdomain)
print(domain)
zone = route53.get_zone(domain)
arec = zone.get_a(fqdn)
try:
zone.add_a(fqdn, v, 900)
except:
zone.update_a(fqdn, v, 900)
print(arec)