-
Notifications
You must be signed in to change notification settings - Fork 177
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
Open
Nadine-H
wants to merge
7
commits into
dstackai:master
Choose a base branch
from
Nadine-H:add-rest-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+600
−58
Open
Changes from all commits
Commits
Show all changes
7 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 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 @@ | ||
3.11 |
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,30 @@ | ||
## Overview | ||
|
||
If you wish to hook up your own plugin server through `dstack` builtin `rest_plugin`, here's a basic example on how to do so. | ||
|
||
## Steps | ||
|
||
|
||
1. Install required dependencies for the plugin server: | ||
|
||
```bash | ||
uv sync | ||
``` | ||
|
||
1. Start the plugin server locally: | ||
|
||
```bash | ||
fastapi dev app/main.py | ||
``` | ||
|
||
1. Enable `rest_plugin` in `server/config.yaml`: | ||
|
||
```yaml | ||
plugins: | ||
- rest_plugin | ||
``` | ||
|
||
1. Point the `dstack` server to your plugin server: | ||
```bash | ||
export DSTACK_PLUGIN_SERVICE_URI=http://127.0.0.1:8000 | ||
``` |
Empty file.
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,56 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
|
||
from app.utils import configure_logging | ||
from dstack.plugins.builtin.rest_plugin import ( | ||
FleetSpecRequest, | ||
FleetSpecResponse, | ||
GatewaySpecRequest, | ||
GatewaySpecResponse, | ||
RunSpecRequest, | ||
RunSpecResponse, | ||
VolumeSpecRequest, | ||
VolumeSpecResponse, | ||
) | ||
|
||
configure_logging() | ||
logger = logging.getLogger(__name__) | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.post("/apply_policies/on_run_apply") | ||
async def on_run_apply(request: RunSpecRequest) -> RunSpecResponse: | ||
logger.info( | ||
f"Received run spec request from user {request.user} and project {request.project}" | ||
) | ||
response = RunSpecResponse(spec=request.spec, error=None) | ||
return response | ||
|
||
|
||
@app.post("/apply_policies/on_fleet_apply") | ||
async def on_fleet_apply(request: FleetSpecRequest) -> FleetSpecResponse: | ||
logger.info( | ||
f"Received fleet spec request from user {request.user} and project {request.project}" | ||
) | ||
response = FleetSpecResponse(request.spec, error=None) | ||
return response | ||
|
||
|
||
@app.post("/apply_policies/on_volume_apply") | ||
async def on_volume_apply(request: VolumeSpecRequest) -> VolumeSpecResponse: | ||
logger.info( | ||
f"Received volume spec request from user {request.user} and project {request.project}" | ||
) | ||
response = VolumeSpecResponse(request.spec, error=None) | ||
return response | ||
|
||
|
||
@app.post("/apply_policies/on_gateway_apply") | ||
async def on_gateway_apply(request: GatewaySpecRequest) -> GatewaySpecResponse: | ||
logger.info( | ||
f"Received gateway spec request from user {request.user} and project {request.project}" | ||
) | ||
response = GatewaySpecResponse(request.spec, error=None) | ||
return response |
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,7 @@ | ||
import logging | ||
import os | ||
|
||
|
||
def configure_logging(): | ||
log_level = os.getenv("LOG_LEVEL", "INFO").upper() | ||
logging.basicConfig(level=log_level) |
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,10 @@ | ||
[project] | ||
name = "dstack-plugin-server" | ||
version = "0.1.0" | ||
description = "Example plugin server" | ||
readme = "README.md" | ||
requires-python = ">=3.11" | ||
dependencies = [ | ||
"fastapi[standard]>=0.115.12", | ||
"dstack>=0.19.8" | ||
] |
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.
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,18 @@ | ||
# ruff: noqa: F401 | ||
from dstack.plugins.builtin.rest_plugin._models import ( | ||
FleetSpecRequest, | ||
FleetSpecResponse, | ||
GatewaySpecRequest, | ||
GatewaySpecResponse, | ||
RunSpecRequest, | ||
RunSpecResponse, | ||
SpecApplyRequest, | ||
SpecApplyResponse, | ||
VolumeSpecRequest, | ||
VolumeSpecResponse, | ||
) | ||
from dstack.plugins.builtin.rest_plugin._plugin import ( | ||
PLUGIN_SERVICE_URI_ENV_VAR_NAME, | ||
CustomApplyPolicy, | ||
RESTPlugin, | ||
) |
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,40 @@ | ||
from typing import Generic, TypeVar | ||
|
||
from pydantic import BaseModel | ||
|
||
from dstack._internal.core.models.fleets import FleetSpec | ||
from dstack._internal.core.models.gateways import GatewaySpec | ||
from dstack._internal.core.models.runs import RunSpec | ||
from dstack._internal.core.models.volumes import VolumeSpec | ||
|
||
SpecType = TypeVar("SpecType", RunSpec, FleetSpec, VolumeSpec, GatewaySpec) | ||
|
||
|
||
class SpecApplyRequest(BaseModel, Generic[SpecType]): | ||
user: str | ||
project: str | ||
spec: SpecType | ||
|
||
# Override dict() to remove __orig_class__ attribute and avoid "TypeError: Object of type _GenericAlias is not JSON serializable" | ||
# error. This issue doesn't happen though when running the code in pytest, only when running the server. | ||
def dict(self, *args, **kwargs): | ||
d = super().dict(*args, **kwargs) | ||
d.pop("__orig_class__", None) | ||
return d | ||
|
||
|
||
RunSpecRequest = SpecApplyRequest[RunSpec] | ||
FleetSpecRequest = SpecApplyRequest[FleetSpec] | ||
VolumeSpecRequest = SpecApplyRequest[VolumeSpec] | ||
GatewaySpecRequest = SpecApplyRequest[GatewaySpec] | ||
|
||
|
||
class SpecApplyResponse(BaseModel, Generic[SpecType]): | ||
spec: SpecType | ||
error: str | None = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As shown by the CI, |
||
|
||
|
||
RunSpecResponse = SpecApplyResponse[RunSpec] | ||
FleetSpecResponse = SpecApplyResponse[FleetSpec] | ||
VolumeSpecResponse = SpecApplyResponse[VolumeSpec] | ||
GatewaySpecResponse = SpecApplyResponse[GatewaySpec] |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these fields be documented with a brief description of what they are and their semantics (e.g. what it means if
error
is populated)? (here and in the request object)