-
Notifications
You must be signed in to change notification settings - Fork 131
/
migrate-dbaas.py
200 lines (174 loc) · 7.67 KB
/
migrate-dbaas.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
import json
import subprocess
def get_clusters(cluster_type):
output = subprocess.check_output(["kubectl", "get", cluster_type, "-o", "json"])
return json.loads(output)
def create_cluster(cluster):
cluster_name = cluster["metadata"]["name"]
p = subprocess.Popen(["kubectl", "apply", "-f", "-"], stdin=subprocess.PIPE)
p.communicate(json.dumps(cluster).encode('utf-8'))
output = subprocess.check_output(["kubectl", "get", "db", cluster_name, "-o", "json"])
uid = json.loads(output).get("metadata", {}).get("uid")
meta_patch = {
"metadata": {
"ownerReferences": [
{
"apiVersion": "dbaas.percona.com/v1",
"blockOwnerDeletion": True,
"controller": True,
"kind": "DatabaseCluster",
"name": cluster_name,
"uid": uid,
},
],
}
}
cluster_type = cluster["spec"]["databaseType"]
print(subprocess.check_output(["kubectl", "patch", f"{cluster_type}/{cluster_name}","--type", "merge", "-p", json.dumps(meta_patch)]))
def strip_none(data):
if isinstance(data, dict):
return {k:strip_none(v) for k, v in data.items() if k is not None and v is not None}
elif isinstance(data, list):
return [strip_none(item) for item in data if item is not None]
elif isinstance(data, tuple):
return tuple(strip_none(item) for item in data if item is not None)
elif isinstance(data, set):
return {strip_none(item) for item in data if item is not None}
else:
return data
def convert_pxc(cluster):
database_cluster = {
"apiVersion": "dbaas.percona.com/v1",
"kind": "DatabaseCluster",
"metadata": {
"name": cluster.get("metadata", {}).get("name", ""),
},
"spec": {
"databaseType": "pxc",
"databaseConfig": cluster.get("spec", {}).get("pxc", {}).get("configuration", ""),
"databaseImage": cluster.get("spec", {}).get("pxc", {}).get("image", ""),
"secretsName": cluster.get("spec", {}).get("secretsName", ""),
"pause": cluster.get("spec", {}).get("pxc", {}).get("pause", None),
"clusterSize": cluster.get("spec", {}).get("pxc", {}).get("size"),
"loadBalancer": {
},
"monitoring": {},
"dbInstance": {}
}
}
if cluster.get("spec", {}).get("haproxy", None):
lb = cluster.get("spec", {}).get("haproxy", {})
database_cluster["spec"]["loadBalancer"] = {
"type": "haproxy",
"exposeType": lb.get("serviceType", None),
"image": lb.get("image"),
"size": lb.get("size"),
"configuration": lb.get("configuration", None),
"annotations": lb.get("annotations", None),
"trafficPolicy": lb.get("externalTrafficPolicy", None),
"resources": lb.get("resources"),
}
if cluster.get("spec", {}).get("proxysql", None):
lb = cluster.get("spec", {}).get("proxysql", {})
database_cluster["spec"]["loadBalancer"] = {
"type": "proxysql",
"exposeType": lb.get("serviceType", None),
"image": lb.get("image"),
"size": lb.get("size"),
"configuration": lb.get("configuration", None),
"annotations": lb.get("annotations", None),
"trafficPolicy": lb.get("externalTrafficPolicy", None),
"resources": lb.get("resources"),
}
if cluster.get("spec", {}).get("pmm", {}).get("enabled", False):
mon = cluster.get("spec", {}).get("pmm", {})
database_cluster["spec"]["monitoring"] = {
"pmm" : {
"image": mon.get("image"),
"serverHost": mon.get("serverHost", None),
"serverUser": mon.get("serverUser", None),
"publicAddress": mon.get("serverHost", None),
"login": mon.get("serverUser", None),
"password": mon.get("password", None),
},
"resources": mon.get("resources"),
}
volume_spec = cluster.get("spec", {}).get("pxc", {}).get("volumeSpec", {}).get("persistentVolumeClaim", {})
limits = cluster.get("spec", {}).get("pxc", {}).get("resources", {}).get("limits", {})
database_cluster["spec"]["dbInstance"] = {
"cpu": limits.get("cpu"),
"memory": limits.get("memory"),
"diskSize": volume_spec.get("resources", {}).get("requests", {}).get("storage"),
"storageClassName": volume_spec.get("storageClassName", None)
}
return database_cluster
def convert_psmdb(cluster):
database_cluster = {
"apiVersion": "dbaas.percona.com/v1",
"kind": "DatabaseCluster",
"metadata": {
"name": cluster.get("metadata", {}).get("name", ""),
},
"spec": {
"databaseType": "psmdb",
"databaseImage": cluster.get("spec", {}).get("image"),
"secretsName": cluster.get("spec", {}).get("secrets", {}).get("users"),
"pause": cluster.get("spec", {}).get("pause", None),
"loadBalancer": {
},
"monitoring": {},
"dbInstance": {}
}
}
replsets = cluster.get("spec", {}).get("replsets", [])
if len(replsets) == 0:
print("Cluster has no replicasets configured. Skipping")
return
database_cluster["spec"]["databaseConfig"] = replsets[0].get("configuration", "")
database_cluster["spec"]["clusterSize"] = replsets[0].get("size")
mongos = cluster.get("spec", {}).get("sharding", {}).get("mongos", None)
if mongos:
database_cluster["spec"]["loadBalancer"] = {
"type": "mongos",
"exposeType": mongos.get("expose", {}).get("exposeType", None),
"image": mongos.get("image"),
"size": mongos.get("size"),
"configuration": mongos.get("configuration", None),
"annotations": mongos.get("expose", {}).get("serviceAnnotations", None),
"loadBalancerSourceRanges": mongos.get("expose", {}).get("loadBalancerSourceRanges", None),
"trafficPolicy": mongos.get("externalTrafficPolicy", None),
"resources": mongos.get("resources"),
}
if cluster.get("spec", {}).get("pmm", {}).get("enabled", False):
mon = cluster.get("spec", {}).get("pmm", {})
database_cluster["spec"]["monitoring"] = {
"pmm" : {
"image": mon.get("image"),
"serverHost": mon.get("serverHost", None),
"serverUser": mon.get("serverUser", None),
"publicAddress": mon.get("serverHost", None),
"login": mon.get("serverUser", None),
"password": mon.get("password", None),
},
"resources": mon.get("resources"),
}
volume_spec = replsets[0].get("volumeSpec", {}).get("persistentVolumeClaim", {})
limits = replsets[0].get("resources", {}).get("limits", {})
database_cluster["spec"]["dbInstance"] = {
"cpu": limits.get("cpu"),
"memory": limits.get("memory"),
"diskSize": volume_spec.get("resources", {}).get("requests", {}).get("storage"),
"storageClassName": volume_spec.get("storageClassName", None)
}
return database_cluster
if __name__ == '__main__':
clusters = []
for cluster in get_clusters("pxc").get("items", []):
del cluster["status"]
clusters.append(convert_pxc(cluster))
for cluster in get_clusters("psmdb").get("items", []):
del cluster["status"]
clusters.append(convert_psmdb(cluster))
for cluster in clusters:
create_cluster(strip_none(cluster))