-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
340 lines (289 loc) · 16.5 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import logging
import kopf
import kubernetes
import requests
import resources.autoscaler_agent
import resources.common
import resources.compute_node
import resources.control_plane
import resources.pageserver
import resources.safekeeper
import resources.storage_broker
@kopf.on.startup()
def configure(settings: kopf.OperatorSettings, **_):
settings.posting.level = logging.ERROR
settings.networking.connect_timeout = 10
settings.networking.request_timeout = 60
settings.watching.server_timeout = 10 * 60
settings.persistence.finalizer = 'neon.tech/neon-finalizer'
settings.persistence.progress_storage = kopf.MultiProgressStorage([
kopf.AnnotationsProgressStorage(prefix='neon.tech'),
kopf.StatusProgressStorage(field='status.neon-operator'),
])
@kopf.on.startup()
async def startup(logger, **kwargs):
logger.info("Startup completed.")
def default_resource_limits():
return kubernetes.client.V1ResourceRequirements(
requests={
"cpu": "100m",
"memory": "200Mi",
},
limits={
"cpu": "100m",
"memory": "200Mi",
},
)
@kopf.on.create("neontenants")
def create_tenant(spec, name, namespace, **_):
kopf.info(spec, reason='CreatingTenant', message=f'Creating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
check_for_pre_requisites(kube_client, namespace, name)
# TODO: Get the NeonDeployment object linked to this tenant
# Get the storage credentials from the NeonDeployment object
# Get the resource limits from the NeonDeployment object
# neon_deployment = (kubernetes.client
# .ApiextensionsV1Api(kube_client)
# (name="neon-deployment", namespace=namespace))
pageserver_resources = spec.get('pageServer').get('resources')
if pageserver_resources is None:
pageserver_resources = default_resource_limits()
remote_storage_bucket_endpoint = spec.get('storageConfig').get('endpoint')
remote_storage_bucket_name = spec.get('storageConfig').get('bucketName')
remote_storage_bucket_region = spec.get('storageConfig').get('bucketRegion')
remote_storage_prefix_in_bucket = spec.get('storageConfig').get('prefixInBucket')
compute_node_resources = spec.get('computeNode').get('resources')
if compute_node_resources is None:
compute_node_resources = default_resource_limits()
# TODO: Update the tenant crd with the tenant id
# Deploy the pageserver
resources.pageserver.deploy_pageserver(kube_client=kube_client,
namespace=namespace,
resources=pageserver_resources,
remote_storage_endpoint=remote_storage_bucket_endpoint,
remote_storage_bucket_name=remote_storage_bucket_name,
remote_storage_bucket_region=remote_storage_bucket_region,
remote_storage_prefix_in_bucket=remote_storage_prefix_in_bucket)
# Deploy the compute nodes
resources.compute_node.deploy_compute_node(kube_client=kube_client,
namespace=namespace,
resources=compute_node_resources)
pageserver_url = f"http://pageserver.{namespace}.svc.cluster.local:6400"
# Call the api to create the tenant using requests post method to pageserver_url/v1/tenant
# If the response is not 200, raise kopf.PermanentError(f"Failed to create tenant {namespace}/{name}")
# If the response is 200, kopf.adopt the tenant
request = {
}
response = requests.post(f"{pageserver_url}/v1/tenant", json=request)
if response.status_code != 200:
raise kopf.PermanentError(f"Failed to create tenant {namespace}/{name}")
else:
tenant_id = response.text
kopf.info(spec, reason='CreatingTenant', message=f'Created {namespace}/{name}/{tenant_id}.')
kopf.adopt(spec)
@kopf.on.update("neontenants")
def update_tenant(spec, name, namespace, **_):
kopf.info(spec, reason='UpdatingTenant', message=f'Updating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
check_for_pre_requisites(kube_client, namespace, name)
pageserver_url = f"http://pageserver.{namespace}.svc.cluster.local:6400"
# Call the api to update the tenant
request = {}
response = requests.put(f"{pageserver_url}/v1/tenant/config", json=request)
@kopf.on.delete("neontenants")
def delete_tenant(spec, name, namespace, **_):
kopf.info(spec, reason='DeletingTenant', message=f'Deleting {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
check_for_pre_requisites(kube_client, namespace, name)
pageserver_url = f"http://pageserver.{namespace}.svc.cluster.local:6400"
@kopf.on.create("neontimelines")
def create_timeline(spec, name, namespace, **_):
kopf.info(spec, reason='CreatingTimeline', message=f'Creating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
check_for_pre_requisites(kube_client, namespace, name)
pageserver_url = f"http://pageserver.{namespace}.svc.cluster.local:6400"
# Call the api to create the timeline
request = {}
response = requests.post(f"{pageserver_url}/v1/timeline", json=request)
if response.status_code != 200:
raise kopf.PermanentError(f"Failed to create timeline {namespace}/{name}")
else:
timeline_id = response.text
kopf.info(spec, reason='CreatingTimeline', message=f'Created {namespace}/{name}/{timeline_id}.')
kopf.adopt(spec)
@kopf.on.update("neontimelines")
def update_timeline(spec, name, namespace, **_):
kopf.info(spec, reason='UpdatingTimeline', message=f'Updating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
check_for_pre_requisites(kube_client, namespace, name)
pageserver_url = f"http://pageserver.{namespace}.svc.cluster.local:6400"
# Call the api to update the timeline
request = {}
response = requests.put(f"{pageserver_url}/v1/timeline", json=request)
@kopf.on.delete("neontimelines")
def delete_timeline(spec, name, namespace, **_):
kopf.info(spec, reason='DeletingTimeline', message=f'Deleting {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
@kopf.on.create("neondeployments")
def create_deployment(spec, name, namespace, **_):
kopf.info(spec, reason='CreatingDeployment', message=f'Creating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
aws_access_key_id = spec.get('storageConfig').get('credentials').get('awsAccessKeyID')
aws_secret_access_key = spec.get('storageConfig').get('credentials').get('awsSecretAccessKey')
if aws_access_key_id is None or aws_secret_access_key is None:
raise kopf.PermanentError(f"Storage credentials are missing for NeonDeployment {namespace}/{name}")
# Assign resource requests and limits
compute_node_resources = spec.get('computeNode').get('resources')
if compute_node_resources is None:
compute_node_resources = default_resource_limits()
storage_broker_resources = spec.get('storageBroker').get('resources')
if storage_broker_resources is None:
storage_broker_resources = default_resource_limits()
control_plane_resources = spec.get('controlPlane').get('resources')
if control_plane_resources is None:
control_plane_resources = default_resource_limits()
pageserver_resources = spec.get('pageServer').get('resources')
if pageserver_resources is None:
pageserver_resources = default_resource_limits()
safekeeper_resources = spec.get('safeKeeper').get('resources')
if safekeeper_resources is None:
safekeeper_resources = default_resource_limits()
# Storage Configuration
remote_storage_bucket_endpoint = spec.get('storageConfig').get('endpoint')
remote_storage_bucket_name = spec.get('storageConfig').get('bucketName')
remote_storage_bucket_region = spec.get('storageConfig').get('bucketRegion')
remote_storage_prefix_in_bucket = spec.get('storageConfig').get('prefixInBucket')
# All of the above must be present
if remote_storage_bucket_endpoint is None or remote_storage_bucket_name is None or remote_storage_bucket_region is None or remote_storage_prefix_in_bucket is None:
raise kopf.PermanentError(f"Storage configuration is missing for NeonDeployment {namespace}/{name}")
try:
# Deploy the storage credentials secret
resources.common.deploy_secret(kube_client, namespace, aws_access_key_id, aws_secret_access_key)
# Deploy the storage broker
resources.storage_broker.deploy_storage_broker(kube_client, namespace)
# Deploy the safekeeper
resources.safekeeper.deploy_safekeeper(kube_client=kube_client,
namespace=namespace,
resources=safekeeper_resources,
remote_storage_bucket_endpoint=remote_storage_bucket_endpoint,
remote_storage_bucket_name=remote_storage_bucket_name,
remote_storage_bucket_region=remote_storage_bucket_region,
remote_storage_prefix_in_bucket=remote_storage_prefix_in_bucket)
# Deploy the control plane
resources.control_plane.deploy_control_plane(kube_client=kube_client,
namespace=namespace,
resources=control_plane_resources)
except Exception as e:
raise kopf.PermanentError(f"Failed to create NeonDeployment {namespace}/{name}: {e}")
@kopf.on.update("neondeployments")
def update_deployment(spec, name, namespace, **_):
kopf.info(spec, reason='UpdatingDeployment', message=f'Updating {namespace}/{name}.')
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
aws_access_key_id = spec.get('storageConfig').get('credentials').get('awsAccessKeyID')
aws_secret_access_key = spec.get('storageConfig').get('credentials').get('awsSecretAccessKey')
if aws_access_key_id is None or aws_secret_access_key is None:
raise kopf.PermanentError(f"Storage credentials are missing for NeonDeployment {namespace}/{name}")
# Preassign resource requests and limits
compute_node_resources = spec.get('computeNode').get('resources')
if compute_node_resources is None:
compute_node_resources = default_resource_limits()
storage_broker_resources = spec.get('storageBroker').get('resources')
if storage_broker_resources is None:
storage_broker_resources = default_resource_limits()
control_plane_resources = spec.get('controlPlane').get('resources')
if control_plane_resources is None:
control_plane_resources = default_resource_limits()
pageserver_resources = spec.get('pageServer').get('resources')
if pageserver_resources is None:
pageserver_resources = default_resource_limits()
safekeeper_resources = spec.get('safeKeeper').get('resources')
if safekeeper_resources is None:
safekeeper_resources = default_resource_limits()
# Storage Configuration
remote_storage_bucket_endpoint = spec.get('storageConfig').get('endpoint')
remote_storage_bucket_name = spec.get('storageConfig').get('bucketName')
remote_storage_bucket_region = spec.get('storageConfig').get('bucketRegion')
remote_storage_prefix_in_bucket = spec.get('storageConfig').get('prefixInBucket')
# All of the above must be present
if remote_storage_bucket_endpoint is None or remote_storage_bucket_name is None or remote_storage_bucket_region is None or remote_storage_prefix_in_bucket is None:
raise kopf.PermanentError(f"Storage configuration is missing for NeonDeployment {namespace}/{name}")
try:
# Update the storage credentials secret
resources.common.update_secret(kube_client, namespace, aws_access_key_id, aws_secret_access_key)
# Update the storage broker
resources.storage_broker.update_storage_broker(kube_client, namespace)
# Update the safekeeper
resources.safekeeper.update_safekeeper(kube_client, namespace, safekeeper_resources,
remote_storage_bucket_endpoint,
remote_storage_bucket_name,
remote_storage_bucket_region,
remote_storage_prefix_in_bucket
)
# Update the control plane
resources.control_plane.update_control_plane(kube_client, namespace, control_plane_resources)
# Update the pageserver
resources.pageserver.update_pageserver(kube_client, namespace, pageserver_resources)
# Update the compute nodes
resources.compute_node.update_compute_node(kube_client, namespace, compute_node_resources)
except Exception as e:
raise kopf.PermanentError(f"Failed to update NeonDeployment {namespace}/{name}: {e}")
@kopf.on.delete("neondeployments")
def delete_deployment(spec, name, namespace, **_):
kopf.info(spec, reason='DeletingDeployment', message=f'Deleting {namespace}/{name}.')
# We delete all the deployments and services
kubernetes.config.load_incluster_config()
kube_client = kubernetes.client.ApiClient()
# Delete the compute nodes
resources.compute_node.delete_compute_node(kube_client, namespace)
# Delete the pageserver
resources.pageserver.delete_pageserver(kube_client, namespace)
# Delete the control plane
resources.control_plane.delete_control_plane(kube_client, namespace)
# Delete the safekeeper
resources.safekeeper.delete_safekeeper(kube_client, namespace)
# Delete the storage broker
resources.storage_broker.delete_storage_broker(kube_client, namespace)
# Delete the storage credentials secret
resources.common.delete_secret(kube_client, namespace)
@kopf.on.login()
def login(**kwargs):
return kopf.login_with_service_account(**kwargs) or kopf.login_with_kubeconfig(**kwargs)
@kopf.on.cleanup()
async def cleanup_fn(logger, **kwargs):
logger.info("Cleanup completed.")
pass
def check_for_pre_requisites(kube_client, namespace, name):
"""
Check for the pre-requisites for NeonTenant deployment
"""
safekeepker_statefulset = (kubernetes.client
.AppsV1Api(kube_client)
.read_namespaced_stateful_set(name="safekeeper", namespace=namespace))
if safekeepker_statefulset is None:
raise kopf.PermanentError(f"Safekeeper statefulset is missing for NeonTenant {namespace}/{name}")
if safekeepker_statefulset['status']['ready_replicas'] is None:
raise kopf.PermanentError(f"Safekeeper statefulset is not ready for NeonTenant {namespace}/{name}")
safekeepker_svc = kubernetes.client.CoreV1Api(kube_client).read_namespaced_service(name="pageserver",
safekeeper=namespace)
if safekeepker_svc is None:
raise kopf.PermanentError(f"Safekeeper service is missing for NeonTenant {namespace}/{name}")
# Check for storage_broker deployment
storage_broker_deployment = (kubernetes.client
.AppsV1Api(kube_client)
.read_namespaced_deployment(name="storage-broker", namespace=namespace))
if storage_broker_deployment is None:
raise kopf.PermanentError(f"Storage broker deployment is missing for NeonTenant {namespace}/{name}")
if storage_broker_deployment['status']['ready_replicas'] is None:
raise kopf.PermanentError(f"Storage broker deployment is not ready for NeonTenant {namespace}/{name}")
storage_broker_svc = kubernetes.client.CoreV1Api(kube_client).read_namespaced_service(name="storage-broker",
namespace=namespace)
if storage_broker_svc is None:
raise kopf.PermanentError(f"Storage broker service is missing for NeonTenant {namespace}/{name}")