Skip to content

Commit 9caaedd

Browse files
committed
Support gateway: true in service configurations
Allow setting `gateway: true` in service configurations, which will enforce that the service runs on the default gateway or is rejected if one isn't available. **Compatibility**: pre-0.20.1 clients will not be able to list (`dstack ps`), get, or update (`dstack apply`) services that were created by 0.20.1+ clients with the `gateway: true` property.
1 parent aec51dc commit 9caaedd

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/dstack/_internal/core/models/configurations.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ class ServiceConfigurationParams(CoreModel):
725725
Field(
726726
description=(
727727
"The name of the gateway. Specify boolean `false` to run without a gateway."
728-
" Omit to run with the default gateway"
728+
" Specify boolean `true` to run with the default gateway."
729+
" Omit to run with the default gateway if there is one, or without a gateway otherwise"
729730
),
730731
),
731732
] = None
@@ -795,16 +796,6 @@ def convert_replicas(cls, v: Range[int]) -> Range[int]:
795796
raise ValueError("The minimum number of replicas must be greater than or equal to 0")
796797
return v
797798

798-
@validator("gateway")
799-
def validate_gateway(
800-
cls, v: Optional[Union[bool, str]]
801-
) -> Optional[Union[Literal[False], str]]:
802-
if v == True:
803-
raise ValueError(
804-
"The `gateway` property must be a string or boolean `false`, not boolean `true`"
805-
)
806-
return v
807-
808799
@root_validator()
809800
def validate_scaling(cls, values):
810801
scaling = values.get("scaling")

src/dstack/_internal/server/services/services/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ async def register_service(session: AsyncSession, run_model: RunModel, run_spec:
5555
gateway = await get_project_default_gateway_model(
5656
session=session, project=run_model.project
5757
)
58+
if gateway is None and run_spec.configuration.gateway == True:
59+
raise ResourceNotExistsError(
60+
"The service requires a gateway, but there is no default gateway in the project"
61+
)
5862

5963
if gateway is not None:
6064
service_spec = await _register_service_in_gateway(session, run_model, run_spec, gateway)

src/tests/_internal/server/routers/test_runs.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,13 @@ def mock_gateway_connections(self) -> Generator[None, None, None]:
20132013
"https://gateway.default-gateway.example",
20142014
id="submits-to-default-gateway",
20152015
),
2016+
pytest.param(
2017+
[("default-gateway", True), ("non-default-gateway", False)],
2018+
True,
2019+
"https://test-service.default-gateway.example",
2020+
"https://gateway.default-gateway.example",
2021+
id="submits-to-default-gateway-when-gateway-true",
2022+
),
20162023
pytest.param(
20172024
[("default-gateway", True), ("non-default-gateway", False)],
20182025
"non-default-gateway",
@@ -2108,7 +2115,7 @@ async def test_return_error_if_specified_gateway_not_exists(
21082115
}
21092116

21102117
@pytest.mark.asyncio
2111-
async def test_return_error_if_specified_gateway_is_true(
2118+
async def test_return_error_if_specified_gateway_is_true_and_no_gateway_exists(
21122119
self, test_db, session: AsyncSession, client: AsyncClient
21132120
) -> None:
21142121
user = await create_user(session=session, global_role=GlobalRole.USER)
@@ -2123,5 +2130,12 @@ async def test_return_error_if_specified_gateway_is_true(
21232130
headers=get_auth_headers(user.token),
21242131
json={"run_spec": run_spec},
21252132
)
2126-
assert response.status_code == 422
2127-
assert "must be a string or boolean `false`, not boolean `true`" in response.text
2133+
assert response.status_code == 400
2134+
assert response.json() == {
2135+
"detail": [
2136+
{
2137+
"msg": "The service requires a gateway, but there is no default gateway in the project",
2138+
"code": "resource_not_exists",
2139+
}
2140+
]
2141+
}

0 commit comments

Comments
 (0)