Skip to content

Commit

Permalink
[Application] Populate resource configuration to sidecar (#5671)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerShor committed Jun 2, 2024
1 parent ebeac7d commit 49822c8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
4 changes: 4 additions & 0 deletions mlrun/runtimes/nuclio/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ def with_sidecar(
if args and sidecar["command"]:
sidecar["args"] = mlrun.utils.helpers.as_list(args)

# populate the sidecar resources from the function spec
if self.spec.resources:
sidecar["resources"] = self.spec.resources

def _set_sidecar(self, name: str) -> dict:
self.spec.config.setdefault("spec.sidecars", [])
sidecars = self.spec.config["spec.sidecars"]
Expand Down
61 changes: 51 additions & 10 deletions tests/runtimes/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
import mlrun.utils


@pytest.fixture
def igz_version_mock():
"""Application runtime uses access key api gateway which requires igz version >= 3.5.5,
so we need to mock the igz version to be 3.6.0 to pass the validation in the tests."""
original_igz_version = mlrun.mlconf.igz_version
mlrun.mlconf.igz_version = "3.6.0"
yield
mlrun.mlconf.igz_version = original_igz_version


def test_create_application_runtime():
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
"application-test", kind="application", image="mlrun/mlrun"
Expand All @@ -33,8 +43,7 @@ def test_create_application_runtime():
# _assert_function_handler(fn)


def test_create_application_runtime_with_command(rundb_mock):
mlrun.mlconf.igz_version = "3.6.0"
def test_create_application_runtime_with_command(rundb_mock, igz_version_mock):
fn: mlrun.runtimes.ApplicationRuntime = mlrun.new_function(
"application-test", kind="application", image="mlrun/mlrun", command="echo"
)
Expand All @@ -47,8 +56,7 @@ def test_create_application_runtime_with_command(rundb_mock):
# _assert_function_handler(fn)


def test_deploy_application_runtime(rundb_mock):
mlrun.mlconf.igz_version = "3.6.0"
def test_deploy_application_runtime(rundb_mock, igz_version_mock):
image = "my/web-app:latest"
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
"application-test", kind="application", image=image
Expand All @@ -57,8 +65,7 @@ def test_deploy_application_runtime(rundb_mock):
_assert_application_post_deploy_spec(fn, image)


def test_consecutive_deploy_application_runtime(rundb_mock):
mlrun.mlconf.igz_version = "3.6.0"
def test_consecutive_deploy_application_runtime(rundb_mock, igz_version_mock):
image = "my/web-app:latest"
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
"application-test", kind="application", image=image
Expand Down Expand Up @@ -160,8 +167,7 @@ def test_image_enriched_on_build_application_image(remote_builder_mock):
assert fn.status.state == mlrun.common.schemas.FunctionState.ready


def test_application_image_build(remote_builder_mock):
mlrun.mlconf.igz_version = "3.6.0"
def test_application_image_build(remote_builder_mock, igz_version_mock):
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
"application-test",
kind="application",
Expand All @@ -174,8 +180,7 @@ def test_application_image_build(remote_builder_mock):
)


def test_application_api_gateway(rundb_mock):
mlrun.mlconf.igz_version = "3.6.0"
def test_application_api_gateway(rundb_mock, igz_version_mock):
function_name = "application-test"
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
"application-test",
Expand All @@ -190,6 +195,42 @@ def test_application_api_gateway(rundb_mock):
assert function_name in api_gateway.spec.functions[0]


def test_application_runtime_resources(rundb_mock, igz_version_mock):
image = "my/web-app:latest"
app_name = "application-test"
fn: mlrun.runtimes.ApplicationRuntime = mlrun.code_to_function(
app_name,
kind="application",
image=image,
)
cpu_requests = "0.7"
memory_requests = "1.2Gi"
cpu_limits = "2"
memory_limits = "4Gi"
fn.with_requests(cpu=cpu_requests, mem=memory_requests)
fn.with_limits(cpu=cpu_limits, mem=memory_limits)

fn.deploy()

assert fn.spec.config["spec.sidecars"] == [
{
"image": image,
"name": f"{app_name}-sidecar",
"ports": [
{
"containerPort": 8050,
"name": "application-t-0",
"protocol": "TCP",
}
],
"resources": {
"requests": {"cpu": cpu_requests, "memory": memory_requests},
"limits": {"cpu": cpu_limits, "memory": memory_limits},
},
}
]


def _assert_function_code(fn, file_path=None):
file_path = (
file_path or mlrun.runtimes.ApplicationRuntime.get_filename_and_handler()[0]
Expand Down

0 comments on commit 49822c8

Please sign in to comment.