-
-
Notifications
You must be signed in to change notification settings - Fork 223
Add REST plugin for user-defined policies #2631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a756cf4
Add REST plugin
Nadine-H fa46f3a
Change rest-plugin to a builtin plugin
Nadine-H dc17969
Add plugin server example
Nadine-H a4bacac
Document rest-plugin in guides
Nadine-H e757a77
Refactor rest-plugin + polish models
Nadine-H f9036f5
Restructure rest_plugin modules
Nadine-H db04651
Doc updates
Nadine-H 32533ef
Additional type checks, type hints and field descriptions
Nadine-H 6f1f90e
Unskip postgres tests
Nadine-H File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import json | ||
| import os | ||
| from typing import Generic, TypeVar | ||
|
|
||
| import requests | ||
| from pydantic import BaseModel, ValidationError | ||
|
|
||
| from dstack._internal.core.errors import ServerClientError | ||
| from dstack._internal.core.models.fleets import FleetSpec | ||
| from dstack._internal.core.models.gateways import GatewaySpec | ||
| from dstack._internal.core.models.volumes import VolumeSpec | ||
| from dstack.plugins import ApplyPolicy, Plugin, RunSpec, get_plugin_logger | ||
| from dstack.plugins._models import ApplySpec | ||
|
Nadine-H marked this conversation as resolved.
Outdated
|
||
|
|
||
| logger = get_plugin_logger(__name__) | ||
|
|
||
| PLUGIN_SERVICE_URI_ENV_VAR_NAME = "DSTACK_PLUGIN_SERVICE_URI" | ||
| PLUGIN_REQUEST_TIMEOUT = 8 # in seconds | ||
|
|
||
| SpecType = TypeVar("SpecType", RunSpec, FleetSpec, VolumeSpec, GatewaySpec) | ||
|
|
||
|
|
||
| class SpecRequest(BaseModel, Generic[SpecType]): | ||
| user: str | ||
| project: str | ||
| spec: SpecType | ||
|
|
||
|
|
||
| RunSpecRequest = SpecRequest[RunSpec] | ||
| FleetSpecRequest = SpecRequest[FleetSpec] | ||
| VolumeSpecRequest = SpecRequest[VolumeSpec] | ||
| GatewaySpecRequest = SpecRequest[GatewaySpec] | ||
|
|
||
|
|
||
| class CustomApplyPolicy(ApplyPolicy): | ||
|
Nadine-H marked this conversation as resolved.
Outdated
|
||
| def __init__(self): | ||
| self._plugin_service_uri = os.getenv(PLUGIN_SERVICE_URI_ENV_VAR_NAME) | ||
| logger.info(f"Found plugin service at {self._plugin_service_uri}") | ||
| if not self._plugin_service_uri: | ||
| logger.error( | ||
| f"Cannot create policy because {PLUGIN_SERVICE_URI_ENV_VAR_NAME} is not set" | ||
| ) | ||
| raise ServerClientError(f"{PLUGIN_SERVICE_URI_ENV_VAR_NAME} is not set") | ||
|
|
||
| def _call_plugin_service(self, spec_request: SpecRequest, endpoint: str) -> ApplySpec: | ||
| response = None | ||
| try: | ||
| response = requests.post( | ||
| f"{self._plugin_service_uri}{endpoint}", | ||
| json=spec_request.dict(), | ||
| headers={"accept": "application/json", "Content-Type": "application/json"}, | ||
| timeout=PLUGIN_REQUEST_TIMEOUT, | ||
| ) | ||
| response.raise_for_status() | ||
| spec_json = json.loads(response.text) | ||
|
Nadine-H marked this conversation as resolved.
Outdated
|
||
| return spec_json | ||
| except requests.exceptions.ConnectionError as e: | ||
| logger.error( | ||
| f"Could not connect to plugin service at {self._plugin_service_uri}: %s", e | ||
| ) | ||
| raise e | ||
| except requests.RequestException as e: | ||
| logger.error("Request to the plugin service failed: %s", e) | ||
| if response: | ||
| logger.error(f"Error response from plugin service:\n{response.text}") | ||
| raise e | ||
| except ValidationError as e: | ||
| # Received 200 code but response body is invalid | ||
| logger.exception( | ||
| f"Plugin service returned invalid response:\n{response.text if response else None}" | ||
| ) | ||
| raise e | ||
|
|
||
| def on_run_apply(self, user: str, project: str, spec: RunSpec) -> RunSpec: | ||
| spec_request = RunSpecRequest(user=user, project=project, spec=spec) | ||
| spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_run_apply") | ||
| return RunSpec(**spec_json) | ||
|
|
||
| def on_fleet_apply(self, user: str, project: str, spec: FleetSpec) -> FleetSpec: | ||
| spec_request = FleetSpecRequest(user=user, project=project, spec=spec) | ||
| spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_fleet_apply") | ||
| return FleetSpec(**spec_json) | ||
|
|
||
| def on_volume_apply(self, user: str, project: str, spec: VolumeSpec) -> VolumeSpec: | ||
| spec_request = VolumeSpecRequest(user=user, project=project, spec=spec) | ||
| spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_volume_apply") | ||
| return VolumeSpec(**spec_json) | ||
|
|
||
| def on_gateway_apply(self, user: str, project: str, spec: GatewaySpec) -> GatewaySpec: | ||
| spec_request = GatewaySpecRequest(user=user, project=project, spec=spec) | ||
| spec_json = self._call_plugin_service(spec_request, "/apply_policies/on_gateway_apply") | ||
| return GatewaySpec(**spec_json) | ||
|
|
||
|
|
||
| class RESTPlugin(Plugin): | ||
| def get_apply_policies(self) -> list[ApplyPolicy]: | ||
| return [CustomApplyPolicy()] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.