Skip to content

Commit

Permalink
Merge pull request #110 from GiladShapira94/add_headers_notebook
Browse files Browse the repository at this point in the history
Add test notebook for custom headers as part of the remote step
  • Loading branch information
MichaelSkralivetsky authored Jan 21, 2025
2 parents f6f72b3 + 38a2ed7 commit c61802f
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 0 deletions.
7 changes: 7 additions & 0 deletions serving_remote_step/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
metadata:
name: test-remote-step-headers
kind: project
spec:
source: ''
desired_state: online
conda: ''
5 changes: 5 additions & 0 deletions serving_remote_step/remotestep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


def handler(context,event):
context.logger.info(event.headers)
return event.headers
5 changes: 5 additions & 0 deletions serving_remote_step/serving-func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


def echo(event):
event["new"] = "event"
return event
328 changes: 328 additions & 0 deletions serving_remote_step/test_serving_remote_function.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3a7e0550-1d07-4b7f-a984-ff07099fcb10",
"metadata": {},
"outputs": [],
"source": [
"import mlrun\n",
"from mlrun.serving.remote import RemoteStep"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "08b63705-f2e7-429a-bb2e-a07496d191c6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> 2025-01-21 16:39:22,341 [info] Project loaded successfully: {\"project_name\":\"test-remote-step-headers\"}\n"
]
}
],
"source": [
"project = mlrun.get_or_create_project(\"test-remote-step-headers\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "59ec86e6-d312-42ba-8f73-7223944b4b04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting remotestep.py\n"
]
}
],
"source": [
"%%writefile remotestep.py\n",
"\n",
"\n",
"def handler(context,event):\n",
" context.logger.info(event.headers)\n",
" return event.headers"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2fb5c494-0924-4e8a-91c7-d8ccb292875a",
"metadata": {},
"outputs": [],
"source": [
"func = project.set_function('remotestep.py',image='mlrun/mlrun',kind='nuclio',name='remotestep')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ea1cd07a-9f54-4c08-a95e-4fdea98db17c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> 2025-01-21 16:39:22,466 [info] Starting remote function deploy\n",
"2025-01-21 16:39:22 (info) Deploying function\n",
"2025-01-21 16:39:22 (info) Building\n",
"2025-01-21 16:39:23 (info) Staging files and preparing base images\n",
"2025-01-21 16:39:23 (warn) Using user provided base image, runtime interpreter version is provided by the base image\n",
"2025-01-21 16:39:23 (info) Building processor image\n",
"2025-01-21 16:40:28 (info) Build complete\n",
"2025-01-21 16:40:36 (info) Function deploy complete\n",
"> 2025-01-21 16:40:44,105 [info] Successfully deployed function: {\"external_invocation_urls\":[\"test-remote-step-headers-remotestep.default-tenant.app.cust-cs-illl--3-6-0.iguazio-cd2.com/\"],\"internal_invocation_urls\":[\"nuclio-test-remote-step-headers-remotestep.default-tenant.svc.cluster.local:8080\"]}\n"
]
}
],
"source": [
"function = project.deploy_function(func.metadata.name)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0a1a5c0b-67c0-4867-93a8-42cea9900362",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> 2025-01-21 16:40:44,166 [info] Invoking function: {\"method\":\"GET\",\"path\":\"http://nuclio-test-remote-step-headers-remotestep.default-tenant.svc.cluster.local:8080/\"}\n"
]
},
{
"data": {
"text/plain": [
"{'Connection': 'keep-alive',\n",
" 'X-Nuclio-Target': 'test-remote-step-headers-remotestep',\n",
" 'Host': 'nuclio-test-remote-step-headers-remotestep.default-tenant.svc.cluster.local:8080',\n",
" 'User-Agent': 'python-requests/2.32.3',\n",
" 'Accept-Encoding': 'gzip, deflate',\n",
" 'Accept': '*/*'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function.function.invoke(\"/\",{})"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "19d2fc0d-c624-4386-92df-0f02e7237ad1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting serving-func.py\n"
]
}
],
"source": [
"%%writefile serving-func.py\n",
"\n",
"\n",
"def echo(event):\n",
" event[\"new\"] = \"event\"\n",
" return event"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e5d05863-4807-4068-a7c6-2b92be2991a5",
"metadata": {},
"outputs": [],
"source": [
"serving_func = project.set_function(func=\"serving-func.py\",image=\"mlrun/mlrun\",kind=\"serving\",name=\"serving-func\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "99bc294f-93bf-4b2a-ad2e-e4f3c79d74a8",
"metadata": {},
"outputs": [],
"source": [
"graph = serving_func.set_topology(\"flow\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "14ae1b84-08f9-4088-a5ba-67ed1510db08",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.43.0 (0)\n",
" -->\n",
"<!-- Title: mlrun&#45;flow Pages: 1 -->\n",
"<svg width=\"143pt\" height=\"188pt\"\n",
" viewBox=\"0.00 0.00 143.19 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n",
"<title>mlrun&#45;flow</title>\n",
"<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-184 139.19,-184 139.19,4 -4,4\"/>\n",
"<!-- _start -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>_start</title>\n",
"<polygon fill=\"lightgrey\" stroke=\"black\" points=\"70.84,-144.05 72.99,-144.15 75.12,-144.3 77.22,-144.49 79.28,-144.74 81.29,-145.03 83.24,-145.36 85.13,-145.75 86.95,-146.18 88.7,-146.65 90.36,-147.16 91.93,-147.71 93.41,-148.31 94.78,-148.94 96.06,-149.61 97.22,-150.31 98.28,-151.04 99.23,-151.8 100.06,-152.59 100.78,-153.41 101.38,-154.25 101.87,-155.11 102.24,-155.99 102.5,-156.89 102.65,-157.8 102.69,-158.72 102.63,-159.65 102.46,-160.59 102.19,-161.53 101.82,-162.47 101.36,-163.41 100.82,-164.35 100.19,-165.28 99.48,-166.2 98.69,-167.11 97.84,-168.01 96.92,-168.89 95.94,-169.75 94.91,-170.59 93.83,-171.41 92.7,-172.2 91.52,-172.96 90.31,-173.69 89.07,-174.39 87.8,-175.06 86.49,-175.69 85.17,-176.29 83.83,-176.84 82.46,-177.35 81.09,-177.82 79.69,-178.25 78.29,-178.64 76.88,-178.97 75.47,-179.26 74.04,-179.51 72.61,-179.7 71.18,-179.85 69.75,-179.95 68.31,-180 66.88,-180 65.44,-179.95 64.01,-179.85 62.57,-179.7 61.15,-179.51 59.72,-179.26 58.3,-178.97 56.89,-178.64 55.49,-178.25 54.1,-177.82 52.73,-177.35 51.36,-176.84 50.02,-176.29 48.69,-175.69 47.39,-175.06 46.12,-174.39 44.87,-173.69 43.66,-172.96 42.49,-172.2 41.36,-171.41 40.28,-170.59 39.24,-169.75 38.27,-168.89 37.35,-168.01 36.49,-167.11 35.71,-166.2 35,-165.28 34.37,-164.35 33.82,-163.41 33.37,-162.47 33,-161.53 32.73,-160.59 32.56,-159.65 32.49,-158.72 32.53,-157.8 32.68,-156.89 32.94,-155.99 33.32,-155.11 33.81,-154.25 34.41,-153.41 35.13,-152.59 35.96,-151.8 36.91,-151.04 37.96,-150.31 39.13,-149.61 40.41,-148.94 41.78,-148.31 43.26,-147.71 44.83,-147.16 46.49,-146.65 48.23,-146.18 50.06,-145.75 51.95,-145.36 53.9,-145.03 55.91,-144.74 57.97,-144.49 60.07,-144.3 62.2,-144.15 64.35,-144.05 66.51,-144 68.68,-144 70.84,-144.05\"/>\n",
"<text text-anchor=\"middle\" x=\"67.59\" y=\"-158.3\" font-family=\"Times,serif\" font-size=\"14.00\">start</text>\n",
"</g>\n",
"<!-- echo -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>echo</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"67.59\" cy=\"-90\" rx=\"32.49\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"67.59\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\">echo</text>\n",
"</g>\n",
"<!-- _start&#45;&gt;echo -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>_start&#45;&gt;echo</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M67.59,-143.7C67.59,-135.98 67.59,-126.71 67.59,-118.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"71.09,-118.1 67.59,-108.1 64.09,-118.1 71.09,-118.1\"/>\n",
"</g>\n",
"<!-- RemoteStep -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>RemoteStep</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"67.59\" cy=\"-18\" rx=\"67.69\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"67.59\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">RemoteStep</text>\n",
"</g>\n",
"<!-- echo&#45;&gt;RemoteStep -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>echo&#45;&gt;RemoteStep</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M67.59,-71.7C67.59,-63.98 67.59,-54.71 67.59,-46.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"71.09,-46.1 67.59,-36.1 64.09,-46.1 71.09,-46.1\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x7fe780d77d60>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph.to(name=\"echo\",handler=\"echo\").to(RemoteStep(url=\"http://\"+function.function.status.internal_invocation_urls[0],headers_expression=\"event['custom_headers']\",result_path=\"result\")).respond()\n",
"serving_func.plot()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6b283928-885f-4d18-9316-d0e8197917c9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> 2025-01-21 16:40:44,423 [info] Starting remote function deploy\n",
"2025-01-21 16:40:44 (info) Deploying function\n",
"2025-01-21 16:40:44 (info) Building\n",
"2025-01-21 16:40:45 (info) Staging files and preparing base images\n",
"2025-01-21 16:40:45 (warn) Using user provided base image, runtime interpreter version is provided by the base image\n",
"2025-01-21 16:40:45 (info) Building processor image\n",
"2025-01-21 16:41:50 (info) Build complete\n",
"2025-01-21 16:42:07 (info) Function deploy complete\n",
"> 2025-01-21 16:42:17,597 [info] Successfully deployed function: {\"external_invocation_urls\":[\"test-remote-step-headers-serving-func.default-tenant.app.cust-cs-illl--3-6-0.iguazio-cd2.com/\"],\"internal_invocation_urls\":[\"nuclio-test-remote-step-headers-serving-func.default-tenant.svc.cluster.local:8080\"]}\n"
]
}
],
"source": [
"serving_func = project.deploy_function(serving_func.metadata.name)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5572263a-ecb1-451d-bbd2-ed02c284a02a",
"metadata": {},
"outputs": [],
"source": [
"custom_header = {\"Test\":\"test\"} "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "cc39ca3d-12ae-4ece-be20-6d58bf0d230c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> 2025-01-21 16:42:17,712 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-test-remote-step-headers-serving-func.default-tenant.svc.cluster.local:8080/\"}\n"
]
}
],
"source": [
"res = serving_func.function.invoke(\"/\",{\"custom_headers\":custom_header})"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "df9faaec-c09d-49e4-8383-de4bcd86afc1",
"metadata": {},
"outputs": [],
"source": [
"assert res['result'].get(\"Test\") == custom_header.get(\"Test\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4de8e5df-042d-4f62-968b-cf3a80ddbde8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "mlrun-extended",
"language": "python",
"name": "conda-env-mlrun-extended-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit c61802f

Please sign in to comment.