11import json
22import os
3- from typing import Type
3+ from typing import Dict , Optional , Type
44
55import requests
66from pydantic import ValidationError
99from dstack ._internal .core .models .fleets import FleetSpec
1010from dstack ._internal .core .models .gateways import GatewaySpec
1111from dstack ._internal .core .models .volumes import VolumeSpec
12+ from dstack .api .server ._fleets import _get_fleet_spec_excludes
13+ from dstack .api .server ._runs import _get_run_spec_excludes
1214from dstack .plugins import ApplyPolicy , ApplySpec , Plugin , RunSpec , get_plugin_logger
1315from dstack .plugins .builtin .rest_plugin import (
1416 FleetSpecRequest ,
@@ -44,12 +46,17 @@ def _check_request_rejected(self, response: SpecApplyResponse):
4446 logger .error (f"Plugin service rejected apply request: { response .error } " )
4547 raise ServerClientError (f"Apply request rejected: { response .error } " )
4648
47- def _call_plugin_service (self , spec_request : SpecApplyRequest , endpoint : str ) -> ApplySpec :
49+ def _call_plugin_service (
50+ self ,
51+ spec_request : SpecApplyRequest ,
52+ endpoint : str ,
53+ excludes : Optional [Dict ],
54+ ) -> ApplySpec :
4855 response = None
4956 try :
5057 response = requests .post (
5158 f"{ self ._plugin_service_uri } { endpoint } " ,
52- json = spec_request .dict (),
59+ json = spec_request .dict (exclude = { "spec" : excludes } ),
5360 headers = {"accept" : "application/json" , "Content-Type" : "application/json" },
5461 timeout = PLUGIN_REQUEST_TIMEOUT_SEC ,
5562 )
@@ -75,10 +82,11 @@ def _on_apply(
7582 user : str ,
7683 project : str ,
7784 spec : ApplySpec ,
85+ excludes : Optional [Dict ] = None ,
7886 ) -> ApplySpec :
7987 try :
8088 spec_request = request_cls (user = user , project = project , spec = spec )
81- spec_json = self ._call_plugin_service (spec_request , endpoint )
89+ spec_json = self ._call_plugin_service (spec_request , endpoint , excludes )
8290 response = response_cls (** spec_json )
8391 self ._check_request_rejected (response )
8492 return response .spec
@@ -88,7 +96,13 @@ def _on_apply(
8896
8997 def on_run_apply (self , user : str , project : str , spec : RunSpec ) -> RunSpec :
9098 return self ._on_apply (
91- RunSpecRequest , RunSpecResponse , "/apply_policies/on_run_apply" , user , project , spec
99+ RunSpecRequest ,
100+ RunSpecResponse ,
101+ "/apply_policies/on_run_apply" ,
102+ user ,
103+ project ,
104+ spec ,
105+ excludes = _get_run_spec_excludes (spec ),
92106 )
93107
94108 def on_fleet_apply (self , user : str , project : str , spec : FleetSpec ) -> FleetSpec :
@@ -99,6 +113,7 @@ def on_fleet_apply(self, user: str, project: str, spec: FleetSpec) -> FleetSpec:
99113 user ,
100114 project ,
101115 spec ,
116+ excludes = _get_fleet_spec_excludes (spec ),
102117 )
103118
104119 def on_volume_apply (self , user : str , project : str , spec : VolumeSpec ) -> VolumeSpec :
0 commit comments