forked from celery/Celery-Kubernetes-Operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_utils.py
112 lines (94 loc) · 3.02 KB
/
deployment_utils.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
import os
import kopf
import yaml
def deploy_celery_workers(apps_api, namespace, spec, logger):
path = os.path.join(
os.path.dirname(__file__),
'templates/deployments/celery_worker_deployment.yaml'
)
tmpl = open(path, 'rt').read()
celery_config = spec['workerSpec']
req_resources = celery_config['resources']['requests']
lim_resources = celery_config['resources']['limits']
text = tmpl.format(
namespace=namespace,
app_name=spec['common']['appName'],
celery_app=spec['common']['celeryApp'],
image=spec['common']['image'],
num_of_workers=celery_config['numOfWorkers'],
queues=celery_config['queues'],
loglevel=celery_config['logLevel'],
concurrency=celery_config['concurrency'],
lim_cpu=lim_resources['cpu'],
lim_mem=lim_resources['memory'],
req_cpu=req_resources['cpu'],
req_mem=req_resources['memory']
)
data = yaml.safe_load(text)
mark_as_child(data)
deployed_obj = apps_api.create_namespaced_deployment(
namespace=namespace,
body=data
)
logger.info(
f"Deployment for celery workers successfully created with name: %s",
deployed_obj.metadata.name
)
return deployed_obj
def deploy_flower(apps_api, namespace, spec, logger):
path = os.path.join(
os.path.dirname(__file__),
'templates/deployments/flower_deployment.yaml'
)
tmpl = open(path, 'rt').read()
flower_config = spec['flowerSpec']
req_resources = flower_config['resources']['requests']
lim_resources = flower_config['resources']['limits']
text = tmpl.format(
namespace=namespace,
app_name=spec['common']['appName'],
celery_app=spec['common']['celeryApp'],
image=spec['common']['image'],
replicas=flower_config['replicas'],
lim_cpu=lim_resources['cpu'],
lim_mem=lim_resources['memory'],
req_cpu=req_resources['cpu'],
req_mem=req_resources['memory']
)
data = yaml.safe_load(text)
mark_as_child(data)
deployed_obj = apps_api.create_namespaced_deployment(
namespace=namespace,
body=data
)
logger.info(
f"Deployment for celery flower successfully created with name: %s",
deployed_obj.metadata.name
)
return deployed_obj
def expose_flower_service(api, namespace, spec, logger):
path = os.path.join(
os.path.dirname(__file__),
'templates/services/flower_service.yaml'
)
tmpl = open(path, 'rt').read()
text = tmpl.format(
namespace=namespace,
app_name=spec['common']['appName']
)
data = yaml.safe_load(text)
mark_as_child(data)
svc_obj = api.create_namespaced_service(
namespace=namespace,
body=data
)
logger.info(
f"Flower service successfully created with name: %s",
svc_obj.metadata.name
)
return svc_obj
def mark_as_child(data):
"""
Marks the incoming data as child of celeryapplications
"""
kopf.adopt(data)