-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubesdb.py
executable file
·78 lines (60 loc) · 2.08 KB
/
kubesdb.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
72
73
74
75
76
77
78
#!/usr/bin/env python
#
# Create database based on kube secrets
#
import os
import yaml
import signal
import sys
import logging
import time
from base64 import b64decode
import kubernetes
from urllib3.exceptions import ReadTimeoutError
import pymysql.cursors
from modules.config import Config
from modules.database import Database
from modules.VERSION import Version
logging.basicConfig(
level=logging.INFO, # if appDebug else logging.INFO,
format="%%(asctime)s kubesdb %s %%(levelname)s: %%(message)s" % Version,
datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger("kubesdb")
logger.setLevel(logging.INFO)
c = Config()
logger.info("Starting up...")
def watch_loop(db):
if "KUBERNETES_SERVICE_HOST" in os.environ:
kubernetes.config.load_incluster_config()
else:
kubernetes.config.load_kube_config()
v1 = kubernetes.client.CoreV1Api()
w = kubernetes.watch.Watch()
logger.info("Watching for secrets in namespace %s with label %s" % (c.namespace, c.label))
for event in w.stream(v1.list_namespaced_secret, c.namespace, label_selector=c.label):
event_type = event['type']
secret = event['object']
if event_type == "ADDED":
data = secret.data
if "database" and "password" and "username" in secret.data:
database = b64decode(data['database']).decode("utf-8")
username = b64decode(data['username']).decode("utf-8")
password = b64decode(data['password']).decode("utf-8")
db.create(database, username, password)
else:
logging.info("Secret: %s %s - secret malformed" % (event_type, secret.metadata.name))
else:
logging.info("Secret: %s %s - event not supported" % (event_type, secret.metadata.name))
db = Database(c)
while True:
try:
watch_loop(db)
except KeyboardInterrupt:
logging.info("Keyboard interrupt")
db.close()
sys.exit()
except:
logging.exception("could not watch for Kubernetes service changes")
time.sleep(5)
print("End of script")