forked from celery/Celery-Kubernetes-Operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_utils.py
164 lines (138 loc) · 4.7 KB
/
update_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
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
import constants
from models.worker_spec import (
args_list_from_spec_params
)
def update_all_deployments(api, apps_api_instance, spec, status, namespace):
worker_deployment = update_worker_deployment(
apps_api_instance, spec, status, namespace
)
flower_deployment = update_flower_deployment(
apps_api_instance, spec, status, namespace
)
flower_svc = update_flower_service(
api, spec, status, namespace
)
children = [
{
'name': worker_deployment.metadata.name,
'replicas': worker_deployment.spec.replicas,
'kind': constants.DEPLOYMENT_KIND,
'type': constants.WORKER_TYPE
},
{
'name': flower_deployment.metadata.name,
'replicas': flower_deployment.spec.replicas,
'kind': constants.DEPLOYMENT_KIND,
'type': constants.FLOWER_TYPE
},
{
'name': flower_svc.metadata.name,
'spec': flower_svc.spec.to_dict(),
'kind': constants.SERVICE_KIND,
'type': constants.FLOWER_TYPE
}
]
return {
'children': children,
'children_count': len(children),
'status': constants.STATUS_UPDATED
}
def get_curr_deployment_from_handler_status(handler_name, status, child_type):
"""
Get current deployment name from handler's status
@param: handler_name - which handler to get from
@param: child_type - worker or flower
@returns: current deployment name
"""
for child in status.get(handler_name).get('children'):
if child.get('type') == child_type and child.get('kind') == constants.DEPLOYMENT_KIND: # NOQA
return child.get('name')
return None
def get_curr_deployment_name(status, child_type):
"""
Get current deployment name from parent's status
@param: child_type - worker or flower
@returns: current deployment name
"""
if status.get('update_fn'):
return get_curr_deployment_from_handler_status('update_fn', status, child_type)
return get_curr_deployment_from_handler_status('create_fn', status, child_type)
def update_worker_deployment(apps_api_instance, spec, status, namespace):
worker_spec = spec['workerSpec']
worker_spec_dict = {
'args': args_list_from_spec_params(
celery_app=spec['common']['celeryApp'],
queues=worker_spec['queues'],
loglevel=worker_spec['logLevel'],
concurrency=worker_spec['concurrency']
),
'command': ["celery"],
'image': spec['common']['image'],
'name': f"{spec['common']['appName']}-celery-worker",
'resources': worker_spec['resources']
}
# JSON way of submitting spec to deploy/patch
patch_body = {
"spec": {
"replicas": worker_spec['numOfWorkers'],
"template": {
"spec": {
"containers": [
worker_spec_dict
]
}
}
}
}
worker_deployment_name = get_curr_deployment_name(
status, constants.WORKER_TYPE
)
return apps_api_instance.patch_namespaced_deployment(
worker_deployment_name, namespace, patch_body
)
def update_flower_deployment(apps_api_instance, spec, status, namespace):
flower_spec = spec['flowerSpec']
flower_spec_dict = {
'args': [spec['common']['celeryApp']],
'command': ['flower'],
'image': spec['common']['image'],
'name': f"{spec['common']['appName']}-flower",
'ports': [
{"containerPort": 5555}
],
'resources': flower_spec['resources']
}
# JSON way of submitting spec to deploy/patch
patch_body = {
"spec": {
"replicas": flower_spec['replicas'],
"template": {
"spec": {
"containers": [
flower_spec_dict
]
}
}
}
}
flower_deployment_name = get_curr_deployment_name(
status, constants.FLOWER_TYPE
)
return apps_api_instance.patch_namespaced_deployment(
flower_deployment_name, namespace, patch_body
)
def update_flower_service(api, spec, status, namespace):
# Only app_name change will affect flower service
patch_body = {
"spec": {
"selector": {
"run": f"{spec['common']['appName']}-flower"
}
}
}
flower_svc_name = get_curr_deployment_name(
status, constants.FLOWER_TYPE
) # flower svc is named same as flower deployment
return api.patch_namespaced_service(
flower_svc_name, namespace, patch_body
)