11# Ship serverless code as you write it. No builds, no deploys -- just run.
2- from typing import Any , ClassVar
2+ from typing import Any
33
4- # Ship serverless code as you write it. No builds, no deploys — just run.
54from pydantic import model_validator
65
7- from .constants import (
8- GPU_BASE_IMAGE_PYTHON_VERSION ,
9- get_image_name ,
10- local_python_version ,
11- )
6+ from .constants import DEFAULT_PYTHON_VERSION , get_image_name
127from .injection import build_injection_cmd
138from .load_balancer_sls_resource import (
149 CpuLoadBalancerSlsResource ,
2217class LiveServerlessMixin :
2318 """Common mixin for live serverless endpoints.
2419
25- Treats the Flash runtime image as a *default*: if the caller passes an
26- ``imageName`` (e.g. via ``Endpoint(image=...)`` in client mode), that
27- value wins. Otherwise the Flash runtime image for this resource type is
28- used so decorator-mode workloads continue to deploy the Flash wrapper.
29-
30- The default is applied via the ``@model_validator(mode="before")`` on each
31- concrete subclass (see ``_apply_default_live_image``); reads and writes of
32- ``imageName`` go through the normal Pydantic field machinery so model
33- serialization, drift detection, and ``setattr`` all stay consistent.
34- """ Configures process injection via dockerArgs for any base image .
35-
36- Sets a default base image (user can override via imageName ) and generates
37- dockerArgs to download , extract , and run the flash - worker tarball at container
38- start time . QB vs LB mode is determined by FLASH_ENDPOINT_TYPE env var at
39- runtime , not by the Docker image .
20+ Configures process injection via ``dockerArgs`` for any base image, and
21+ treats the Flash runtime image as a *default*: if the caller passes an
22+ ``imageName`` (e.g. via ``Endpoint(image=...)`` in client mode), that value
23+ wins. Otherwise the Flash runtime image for this resource type is applied by
24+ the ``@model_validator(mode="before")`` on each concrete subclass (see
25+ ``_apply_default_live_image``), so decorator-mode workloads continue to
26+ deploy the Flash wrapper.
27+
28+ The injection ``dockerArgs`` download, extract, and run the flash-worker
29+ tarball at container start; QB vs LB mode is determined by the
30+ ``FLASH_ENDPOINT_TYPE`` env var at runtime, not by the Docker image.
4031 """
4132
42- _image_type: ClassVar[str] = (
43- "" # override in subclasses: 'gpu', 'cpu', 'lb', 'lb-cpu'
44- )
33+ def _create_new_template (self ) -> PodTemplate :
34+ """Create template with dockerArgs for process injection."""
35+ template = super ()._create_new_template () # type: ignore[misc]
36+ template .dockerArgs = build_injection_cmd ()
37+ return template
4538
46- @property
47- def _live_image(self) -> str:
48- python_version = getattr(self, "python_version", None) or DEFAULT_PYTHON_VERSION
49- return get_image_name(self._image_type, python_version)
39+ def _configure_existing_template (self ) -> None :
40+ """Configure existing template, adding dockerArgs for injection if not user-set."""
41+ super ()._configure_existing_template () # type: ignore[misc]
42+ if self .template is not None and not self .template .dockerArgs : # type: ignore[attr-defined]
43+ self .template .dockerArgs = build_injection_cmd () # type: ignore[attr-defined]
5044
5145
5246def _apply_default_live_image (data : Any , image_type : str ):
@@ -63,18 +57,6 @@ def _apply_default_live_image(data: Any, image_type: str):
6357 data ["imageName" ] = get_image_name (image_type , python_version )
6458 return data
6559
66- def _create_new_template(self) -> PodTemplate:
67- """ Create template with dockerArgs for process injection ."""
68- template = super()._create_new_template() # type: ignore[misc]
69- template.dockerArgs = build_injection_cmd()
70- return template
71-
72- def _configure_existing_template(self) -> None:
73- """ Configure existing template , adding dockerArgs for injection if not user - set ."""
74- super()._configure_existing_template() # type: ignore[misc]
75- if self.template is not None and not self.template.dockerArgs: # type: ignore[attr-defined]
76- self.template.dockerArgs = build_injection_cmd() # type: ignore[attr-defined]
77-
7860
7961class LiveServerless (LiveServerlessMixin , ServerlessEndpoint ):
8062 """GPU-only live serverless endpoint."""
@@ -84,11 +66,6 @@ class LiveServerless(LiveServerlessMixin, ServerlessEndpoint):
8466 def set_live_serverless_template (cls , data : dict ):
8567 """Default to the GPU Flash runtime image when none is supplied."""
8668 return _apply_default_live_image (data , "gpu" )
87- """ Set default GPU image for Live Serverless ."""
88- if "imageName" not in data:
89- python_version = data.get("python_version") or GPU_BASE_IMAGE_PYTHON_VERSION
90- data["imageName"] = get_image_name("gpu", python_version)
91- return data
9269
9370
9471class CpuLiveServerless (LiveServerlessMixin , CpuServerlessEndpoint ):
@@ -99,11 +76,6 @@ class CpuLiveServerless(LiveServerlessMixin, CpuServerlessEndpoint):
9976 def set_live_serverless_template (cls , data : dict ):
10077 """Default to the CPU Flash runtime image when none is supplied."""
10178 return _apply_default_live_image (data , "cpu" )
102- """ Set default CPU image for Live Serverless ."""
103- if "imageName" not in data:
104- python_version = data.get("python_version") or local_python_version()
105- data["imageName"] = get_image_name("cpu", python_version)
106- return data
10779
10880
10981class LiveLoadBalancer (LiveServerlessMixin , LoadBalancerSlsResource ):
@@ -114,11 +86,6 @@ class LiveLoadBalancer(LiveServerlessMixin, LoadBalancerSlsResource):
11486 def set_live_lb_template (cls , data : dict ):
11587 """Default to the LB Flash runtime image when none is supplied."""
11688 return _apply_default_live_image (data , "lb" )
117- """ Set default image for Live Load - Balanced endpoint ."""
118- if "imageName" not in data:
119- python_version = data.get("python_version") or GPU_BASE_IMAGE_PYTHON_VERSION
120- data["imageName"] = get_image_name("lb", python_version)
121- return data
12289
12390
12491class CpuLiveLoadBalancer (LiveServerlessMixin , CpuLoadBalancerSlsResource ):
@@ -129,8 +96,3 @@ class CpuLiveLoadBalancer(LiveServerlessMixin, CpuLoadBalancerSlsResource):
12996 def set_live_cpu_lb_template (cls , data : dict ):
13097 """Default to the CPU LB Flash runtime image when none is supplied."""
13198 return _apply_default_live_image (data , "lb-cpu" )
132- """ Set default CPU image for Live Load - Balanced endpoint ."""
133- if "imageName" not in data:
134- python_version = data.get("python_version") or local_python_version()
135- data["imageName"] = get_image_name("lb-cpu", python_version )
136- return data
0 commit comments