forked from faasm/faasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knative.py
374 lines (290 loc) · 10.5 KB
/
knative.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import os
from copy import copy
from os.path import join
from subprocess import call
from invoke import task
from tasks.util.config import get_faasm_config
from tasks.util.env import PROJ_ROOT, FUNC_DIR
from tasks.util.files import clean_dir
from tasks.util.ibm import get_ibm_kubeconfig
from tasks.util.endpoints import get_invoke_host_port
from tasks.util.version import get_faasm_version
K8S_DIR = join(PROJ_ROOT, "k8s")
BARE_METAL_CONF = join(K8S_DIR, "bare-metal")
BARE_METAL_REMOTE_CONF = join(K8S_DIR, "bare-metal-remote")
LOCAL_CONF = join(K8S_DIR, "local")
COMMON_CONF = join(K8S_DIR, "common")
IBM_CONF = join(K8S_DIR, "ibm")
LEGACY_CONF = join(K8S_DIR, "legacy")
#
# Notes on Knative client
# https://github.com/knative/client/blob/master/docs/cmd/kn_service_create.md
#
# Configuring the scheduler: https://knative.dev/docs/serving/configuring-the-autoscaler/
#
# TODO: Use the knative autoscaler rather than hard-coding a number of workers
FAASM_WORKER_ANNOTATIONS = [
"autoscaling.knative.dev/enable-scale-to-zero=false",
"autoscaling.knative.dev/stable-window=120s", # Longer window means fewer knative interventions
]
FAASM_WORKER_CONCURRENCY = 0
NATIVE_WORKER_ANNOTATIONS = [
"autoscaling.knative.dev/enable-scale-to-zero=true",
"autoscaling.knative.dev/stable-window=20s",
]
KNATIVE_FUNC_PREFIX = "faasm-"
NATIVE_WORKER_IMAGE_PREFIX = "faasm/knative-native-"
KNATIVE_NATIVE_PY_NAME = "{}python".format(KNATIVE_FUNC_PREFIX)
KNATIVE_NATIVE_PY_IMAGE = "faasm/knative-native-python"
FAASM_WORKER_NAME = "{}worker".format(KNATIVE_FUNC_PREFIX)
FAASM_WORKER_IMAGE = "faasm/knative-worker"
ONE_MIN = 60000
THIRTY_SECS = 30000
KNATIVE_ENV = {
"REDIS_STATE_HOST": "redis-state",
"REDIS_QUEUE_HOST": "redis-queue",
"HOST_TYPE": "knative",
"LOG_LEVEL": "info",
"CGROUP_MODE": "off",
"NETNS_MODE": "off",
"PYTHON_PRELOAD": "off", # Switch on/ off preloading of Python runtime
"TF_CODEGEN": "on", # Switch on/ off up-front codegen for TF
"SGD_CODEGEN": "off", # Switch on/ off up-front codegen for SGD
"PYTHON_CODEGEN": "off", # Switch on/ off up-front codegen for Python
"MAX_IN_FLIGHT_RATIO": "1",
"NO_SCHEDULER": "1", # Turn on/ off Faasm scheduler
"MAX_WORKERS_PER_FUNCTION": "4", # This limit is per-host. We only want one instance per core
"THREADS_PER_WORKER": "100", # This is how many threads are available in total per host (across all functions)
"BOUND_TIMEOUT": str(THIRTY_SECS), # How long a bound worker sticks around for
"UNBOUND_TIMEOUT": str(10 * ONE_MIN), # How long an unbound worker sticks around for
"GLOBAL_MESSAGE_TIMEOUT": str(2 * ONE_MIN), # How long things wait for messages on global bus
}
def _fn_name(function):
return "{}{}".format(KNATIVE_FUNC_PREFIX, function.replace("_", "-"))
def _native_image_name(function):
return "{}{}".format(NATIVE_WORKER_IMAGE_PREFIX, function)
def _kubectl_cmd(path, action, env=None):
cmd = [
"kubectl", action, "-f", path
]
shell_env_dict = os.environ.copy()
if env:
shell_env_dict.update(env)
cmd_str = " ".join(cmd)
print(cmd_str)
ret_code = call(cmd_str, shell=True, env=shell_env_dict)
if ret_code != 0:
raise RuntimeError("Command failed: {}".format(cmd_str))
def _kubectl_apply(path, env=None):
_kubectl_cmd(path, "apply", env=env)
def _kubectl_delete(path, env=None):
_kubectl_cmd(path, "delete", env=env)
@task
def delete_knative_worker(ctx, hard=False):
# Clear redis queue
flush_cmd = "kubectl exec -n faasm redis-queue -- redis-cli flushall"
call(flush_cmd, shell=True)
_delete_knative_fn("worker", hard)
@task
def deploy_knative(ctx, replicas, local=False, ibm=False):
faasm_conf = get_faasm_config()
shell_env = {}
if ibm:
# IBM requires specifying custom kubeconfig
shell_env["KUBECONFIG"] = get_ibm_kubeconfig()
extra_env = {
"FUNCTION_STORAGE": "ibm",
"IBM_API_KEY": faasm_conf["IBM"]["api_key"],
}
else:
extra_env = {
"FUNCTION_STORAGE": "fileserver",
"FILESERVER_URL": "http://upload:8002",
}
# Deploy the other K8s stuff (e.g. redis)
_kubectl_apply(join(COMMON_CONF, "namespace.yml"), env=shell_env)
_kubectl_apply(COMMON_CONF, env=shell_env)
_kubectl_apply(BARE_METAL_CONF)
if local:
_kubectl_apply(LOCAL_CONF)
else:
_kubectl_apply(BARE_METAL_REMOTE_CONF)
_deploy_knative_fn(
FAASM_WORKER_NAME,
FAASM_WORKER_IMAGE,
replicas,
FAASM_WORKER_CONCURRENCY,
FAASM_WORKER_ANNOTATIONS,
extra_env=extra_env,
shell_env=shell_env
)
@task
def delete_knative_full(ctx, local=False):
# First hard-delete the worker
delete_knative_worker(ctx, hard=True)
# Delete common stuff
_kubectl_delete(COMMON_CONF)
_kubectl_delete(BARE_METAL_CONF)
# Delete env-specific stuff
if local:
_kubectl_delete(LOCAL_CONF)
else:
_kubectl_delete(BARE_METAL_REMOTE_CONF)
def _delete_knative_fn(name, hard):
func_name = _fn_name(name)
if hard:
cmd = [
"kn", "service", "delete", func_name, "--namespace=faasm"
]
cmd_str = " ".join(cmd)
print(cmd_str)
call(cmd_str, shell=True)
else:
# Delete the pods (they'll respawn)
label = "serving.knative.dev/service={}".format(func_name)
cmd = "kubectl -n faasm delete pods -l {} --wait=false --now".format(label)
call(cmd, shell=True)
def _deploy_knative_fn(name, image, replicas, concurrency, annotations, extra_env=None, shell_env=None):
version = get_faasm_version()
image = "{}:{}".format(image, version)
cmd = [
"kn", "service", "create", name,
"--image", image,
"--namespace", "faasm",
"--force",
]
cmd.extend({
"--min-scale={}".format(replicas),
"--max-scale={}".format(replicas),
"--concurrency-limit={}".format(concurrency) if concurrency else "",
})
# Add annotations
for annotation in annotations:
cmd.append("--annotation {}".format(annotation))
# Add standard environment
for key, value in KNATIVE_ENV.items():
cmd.append("--env {}={}".format(key, value))
# Add extra environment
extra_env = extra_env if extra_env else {}
for key, value in extra_env.items():
cmd.append("--env {}={}".format(key, value))
cmd_string = " ".join(cmd)
print(cmd_string)
shell_env_dict = os.environ.copy()
if shell_env:
shell_env_dict.update(shell_env)
call(cmd_string, shell=True, env=shell_env_dict)
@task
def build_knative_native(ctx, user, func, host=False, clean=False, nopush=False):
if host:
build_dir = join(PROJ_ROOT, "build", "knative_native")
target = "{}-knative".format(func)
clean_dir(build_dir, clean)
cmd = [
"cmake",
"-DCMAKE_CXX_COMPILER=/usr/bin/clang++",
"-DCMAKE_C_COMPILER=/usr/bin/clang",
"-DFAASM_BUILD_TYPE=knative-native",
"-DCMAKE_BUILD_TYPE=Debug",
"-DFAASM_AWS_SUPPORT=OFF",
PROJ_ROOT
]
call(" ".join(cmd), cwd=build_dir, shell=True)
make_cmd = "cmake --build . --target {} -- -j".format(target)
call(make_cmd, cwd=build_dir, shell=True)
else:
# Build the container
version = get_faasm_version()
tag_name = "{}:{}".format(_native_image_name(func), version)
cmd = [
"docker",
"build",
"--no-cache" if clean else "",
"-t", tag_name,
"--build-arg", "FAASM_VERSION={}".format(version),
"--build-arg", "FAASM_USER={}".format(user),
"--build-arg", "FAASM_FUNC={}".format(func),
"-f", "docker/knative-native.dockerfile",
"."
]
env = copy(os.environ)
env["DOCKER_BUILDKIT"] = "1"
cmd_string = " ".join(cmd)
print(cmd_string)
res = call(cmd_string, shell=True, cwd=PROJ_ROOT)
if res != 0:
print("Building container failed")
return 1
# Push the container
if not nopush:
cmd = "docker push {}".format(tag_name)
call(cmd, shell=True, cwd=PROJ_ROOT)
@task
def deploy_knative_native(ctx, user, func, replicas):
func_name = _fn_name(func)
image_name = _native_image_name(func)
_do_deploy_knative_native(func_name, image_name, replicas)
@task
def deploy_knative_native_python(ctx, replicas):
func_name = KNATIVE_NATIVE_PY_NAME
image_name = KNATIVE_NATIVE_PY_IMAGE
_do_deploy_knative_native(func_name, image_name, replicas)
def _do_deploy_knative_native(func_name, image_name, replicas):
faasm_config = get_faasm_config()
if not faasm_config.has_section("Faasm"):
print("Must have faasm config set up with Faasm section")
return 1
# Host and port required for chaining native functions
invoke_host, invoke_port = get_invoke_host_port()
_deploy_knative_fn(
func_name,
image_name,
replicas,
1,
NATIVE_WORKER_ANNOTATIONS,
extra_env={
"COLD_START_DELAY_MS": "1000",
"FAASM_INVOKE_HOST": invoke_host,
"FAASM_INVOKE_PORT": invoke_port,
},
)
@task
def delete_knative_native(ctx, user, func, hard=False):
_delete_knative_fn(func, hard)
@task
def delete_knative_native_python(ctx, hard=False):
_delete_knative_fn("python", hard)
@task
def knative_native_local(ctx, user, func):
img_name = _native_image_name(func)
_do_knative_native_local(img_name)
@task
def knative_native_python_local(ctx, host=False):
if host:
working_dir = FUNC_DIR
env = copy(os.environ)
env.update({
"LOG_LEVEL": "debug",
"FAASM_INVOKE_HOST": "0.0.0.0",
"FAASM_INVOKE_PORT": "8080",
"HOST_TYPE": "knative",
})
call("./run_knative_native.sh", cwd=working_dir, env=env, shell=True)
else:
img_name = "faasm/knative-native-python"
_do_knative_native_local(img_name)
def _do_knative_native_local(img_name):
version = get_faasm_version()
img_name = "{}:{}".format(img_name, version)
cmd = [
"docker", "run",
"-p 8080:8080",
"--env LOG_LEVEL=debug",
"--env FAASM_INVOKE_HOST=0.0.0.0",
"--env FAASM_INVOKE_PORT=8080",
"--env HOST_TYPE=knative",
img_name
]
cmd_string = " ".join(cmd)
print(cmd_string)
call(cmd_string, shell=True, cwd=PROJ_ROOT)