-
Notifications
You must be signed in to change notification settings - Fork 239
/
functions.py
408 lines (360 loc) · 12.7 KB
/
functions.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import traceback
from distutils.util import strtobool
from http import HTTPStatus
from typing import List
from fastapi import APIRouter, BackgroundTasks, Depends, Query, Request, Response
from fastapi.concurrency import run_in_threadpool
from sqlalchemy.orm import Session
import mlrun.api.db.session
import mlrun.api.schemas
import mlrun.api.utils.background_tasks
from mlrun.api.api import deps
from mlrun.api.api.utils import get_run_db_instance, log_and_raise
from mlrun.api.utils.singletons.db import get_db
from mlrun.api.utils.singletons.k8s import get_k8s
from mlrun.builder import build_runtime
from mlrun.config import config
from mlrun.run import new_function
from mlrun.runtimes import RuntimeKinds, runtime_resources_map
from mlrun.runtimes.function import deploy_nuclio_function, get_nuclio_deploy_status
from mlrun.utils import get_in, logger, parse_versioned_object_uri, update_in
router = APIRouter()
# curl -d@/path/to/func.json http://localhost:8080/func/prj/7?tag=0.3.2
@router.post("/func/{project}/{name}")
async def store_function(
request: Request,
project: str,
name: str,
tag: str = "",
versioned: bool = False,
auth_verifier: deps.AuthVerifier = Depends(deps.AuthVerifier),
db_session: Session = Depends(deps.get_db_session),
):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason="bad JSON body")
logger.debug(data)
logger.info("store function: project=%s, name=%s, tag=%s", project, name, tag)
hash_key = await run_in_threadpool(
get_db().store_function,
db_session,
data,
name,
project,
tag=tag,
versioned=versioned,
leader_session=auth_verifier.auth_info.session,
)
return {
"hash_key": hash_key,
}
# curl http://localhost:8080/log/prj/7?tag=0.2.3
@router.get("/func/{project}/{name}")
def get_function(
project: str,
name: str,
tag: str = "",
hash_key="",
db_session: Session = Depends(deps.get_db_session),
):
func = get_db().get_function(db_session, name, project, tag, hash_key)
return {
"func": func,
}
@router.delete(
"/projects/{project}/functions/{name}", status_code=HTTPStatus.NO_CONTENT.value
)
def delete_function(
project: str, name: str, db_session: Session = Depends(deps.get_db_session),
):
get_db().delete_function(db_session, project, name)
return Response(status_code=HTTPStatus.NO_CONTENT.value)
# curl http://localhost:8080/funcs?project=p1&name=x&label=l1&label=l2
@router.get("/funcs")
def list_functions(
project: str = config.default_project,
name: str = None,
tag: str = None,
labels: List[str] = Query([], alias="label"),
db_session: Session = Depends(deps.get_db_session),
):
funcs = get_db().list_functions(db_session, name, project, tag, labels)
return {
"funcs": list(funcs),
}
# curl -d@/path/to/job.json http://localhost:8080/build/function
@router.post("/build/function")
@router.post("/build/function/")
async def build_function(
request: Request,
auth_verifier: deps.AuthVerifier = Depends(deps.AuthVerifier),
db_session: Session = Depends(deps.get_db_session),
):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason="bad JSON body")
logger.info(f"build_function:\n{data}")
function = data.get("function")
with_mlrun = strtobool(data.get("with_mlrun", "on"))
skip_deployed = data.get("skip_deployed", False)
mlrun_version_specifier = data.get("mlrun_version_specifier")
fn, ready = await run_in_threadpool(
_build_function,
db_session,
auth_verifier.auth_info,
function,
with_mlrun,
skip_deployed,
mlrun_version_specifier,
)
return {
"data": fn.to_dict(),
"ready": ready,
}
# curl -d@/path/to/job.json http://localhost:8080/start/function
@router.post("/start/function", response_model=mlrun.api.schemas.BackgroundTask)
@router.post("/start/function/", response_model=mlrun.api.schemas.BackgroundTask)
async def start_function(
request: Request,
background_tasks: BackgroundTasks,
auth_verifier: deps.AuthVerifier = Depends(deps.AuthVerifier),
db_session: Session = Depends(deps.get_db_session),
):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason="bad JSON body")
logger.info("Got request to start function", body=data)
function = await run_in_threadpool(_parse_start_function_body, db_session, data)
background_task = await run_in_threadpool(
mlrun.api.utils.background_tasks.Handler().create_background_task,
db_session,
auth_verifier.auth_info.session,
function.metadata.project,
background_tasks,
_start_function,
function,
auth_verifier.auth_info,
)
return background_task
# curl -d@/path/to/job.json http://localhost:8080/status/function
@router.post("/status/function")
@router.post("/status/function/")
async def function_status(request: Request):
data = None
try:
data = await request.json()
except ValueError:
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason="bad JSON body")
resp = await run_in_threadpool(_get_function_status, data)
return {
"data": resp,
}
# curl -d@/path/to/job.json http://localhost:8080/build/status
@router.get("/build/status")
@router.get("/build/status/")
def build_status(
name: str = "",
project: str = "",
tag: str = "",
offset: int = 0,
logs: bool = True,
last_log_timestamp: float = 0.0,
verbose: bool = False,
auth_verifier: deps.AuthVerifier = Depends(deps.AuthVerifier),
db_session: Session = Depends(deps.get_db_session),
):
fn = get_db().get_function(db_session, name, project, tag)
if not fn:
log_and_raise(HTTPStatus.NOT_FOUND.value, name=name, project=project, tag=tag)
# nuclio deploy status
if fn.get("kind") in RuntimeKinds.nuclio_runtimes():
(
state,
address,
nuclio_name,
last_log_timestamp,
text,
) = get_nuclio_deploy_status(
name, project, tag, last_log_timestamp=last_log_timestamp, verbose=verbose
)
if state == "ready":
logger.info("Nuclio function deployed successfully", name=name)
if state == "error":
logger.error(f"Nuclio deploy error, {text}", name=name)
update_in(fn, "status.nuclio_name", nuclio_name)
update_in(fn, "status.state", state)
update_in(fn, "status.address", address)
versioned = False
if state == "ready":
# Versioned means the version will be saved in the DB forever, we don't want to spam
# the DB with intermediate or unusable versions, only successfully deployed versions
versioned = True
get_db().store_function(
db_session,
fn,
name,
project,
tag,
versioned=versioned,
leader_session=auth_verifier.auth_info.session,
)
return Response(
content=text,
media_type="text/plain",
headers={
"x-mlrun-function-status": state,
"x-mlrun-last-timestamp": str(last_log_timestamp),
"x-mlrun-address": address,
"x-mlrun-name": nuclio_name,
},
)
# job deploy status
state = get_in(fn, "status.state", "")
pod = get_in(fn, "status.build_pod", "")
image = get_in(fn, "spec.build.image", "")
out = b""
if not pod:
if state == "ready":
image = image or get_in(fn, "spec.image")
return Response(
content=out,
media_type="text/plain",
headers={
"function_status": state,
"function_image": image,
"builder_pod": pod,
},
)
logger.info(f"get pod {pod} status")
state = get_k8s().get_pod_status(pod)
logger.info(f"pod state={state}")
if state == "succeeded":
logger.info("build completed successfully")
state = mlrun.api.schemas.FunctionState.ready
if state in ["failed", "error"]:
logger.error(f"build {state}, watch the build pod logs: {pod}")
state = mlrun.api.schemas.FunctionState.error
if logs and state != "pending":
resp = get_k8s().logs(pod)
if resp:
out = resp.encode()[offset:]
update_in(fn, "status.state", state)
if state == mlrun.api.schemas.FunctionState.ready:
update_in(fn, "spec.image", image)
versioned = False
if state == mlrun.api.schemas.FunctionState.ready:
versioned = True
get_db().store_function(
db_session,
fn,
name,
project,
tag,
versioned=versioned,
leader_session=auth_verifier.auth_info.session,
)
return Response(
content=out,
media_type="text/plain",
headers={
"x-mlrun-function-status": state,
"function_status": state,
"function_image": image,
"builder_pod": pod,
},
)
def _build_function(
db_session,
auth_info: mlrun.api.schemas.AuthInfo,
function,
with_mlrun,
skip_deployed,
mlrun_version_specifier,
):
fn = None
ready = None
try:
fn = new_function(runtime=function)
run_db = get_run_db_instance(db_session, auth_info.session)
fn.set_db_connection(run_db)
fn.save(versioned=False)
if fn.kind in RuntimeKinds.nuclio_runtimes():
mlrun.api.api.utils.ensure_function_has_auth_set(fn, auth_info)
deploy_nuclio_function(fn)
# deploy only start the process, the get status API is used to check readiness
ready = False
else:
ready = build_runtime(
fn, with_mlrun, mlrun_version_specifier, skip_deployed
)
fn.save(versioned=True)
logger.info("Fn:\n %s", fn.to_yaml())
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason=f"runtime error: {err}")
return fn, ready
def _parse_start_function_body(db_session, data):
url = data.get("functionUrl")
if not url:
log_and_raise(
HTTPStatus.BAD_REQUEST.value,
reason="runtime error: functionUrl not specified",
)
project, name, tag, hash_key = parse_versioned_object_uri(url)
runtime = get_db().get_function(db_session, name, project, tag, hash_key)
if not runtime:
log_and_raise(
HTTPStatus.BAD_REQUEST.value,
reason=f"runtime error: function {url} not found",
)
return new_function(runtime=runtime)
def _start_function(function, auth_info: mlrun.api.schemas.AuthInfo):
db_session = mlrun.api.db.session.create_session()
try:
resource = runtime_resources_map.get(function.kind)
if "start" not in resource:
log_and_raise(
HTTPStatus.BAD_REQUEST.value,
reason="runtime error: 'start' not supported by this runtime",
)
try:
run_db = get_run_db_instance(db_session, auth_info.session)
function.set_db_connection(run_db)
mlrun.api.api.utils.ensure_function_has_auth_set(function, auth_info)
# resp = resource["start"](fn) # TODO: handle resp?
resource["start"](function)
function.save(versioned=False)
logger.info("Fn:\n %s", function.to_yaml())
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason=f"runtime error: {err}")
finally:
mlrun.api.db.session.close_session(db_session)
def _get_function_status(data):
logger.info(f"function_status:\n{data}")
selector = data.get("selector")
kind = data.get("kind")
if not selector or not kind:
log_and_raise(
HTTPStatus.BAD_REQUEST.value,
reason="runtime error: selector or runtime kind not specified",
)
resource = runtime_resources_map.get(kind)
if "status" not in resource:
log_and_raise(
HTTPStatus.BAD_REQUEST.value,
reason="runtime error: 'status' not supported by this runtime",
)
resp = None
try:
resp = resource["status"](selector)
logger.info("status: %s", resp)
except Exception as err:
logger.error(traceback.format_exc())
log_and_raise(HTTPStatus.BAD_REQUEST.value, reason=f"runtime error: {err}")