3030 build_pod_spec ,
3131 build_pod_template_spec ,
3232)
33- from src .deployments .core .configs .service import ServiceConfig , build_service
33+ from src .deployments .core .configs .service import ServiceConfig , ServiceSpecType , build_service
3434from src .deployments .core .configs .statefulset import StatefulSetConfig , build_stateful_set
3535
3636logger = logging .getLogger (__name__ )
@@ -42,7 +42,9 @@ class StatefulSetBuilder(BaseModel):
4242 def with_image_in_container (
4343 self , image : Image , container_name : str , * , overwrite : bool = False
4444 ) -> Self :
45- with_image_for_container (self .config , container_name , image , overwrite = overwrite )
45+ with_image_for_container (
46+ config = self .config , image = image , container_name = container_name , overwrite = overwrite
47+ )
4648 return self
4749
4850 def with_replicas (self , replicas : int ) -> Self :
@@ -115,8 +117,78 @@ def build(self) -> V1StatefulSet:
115117class PodBuilder (BaseModel ):
116118 config : PodConfig = Field (default_factory = PodConfig )
117119
118- def with_app (self , app : str , * , overwrite : bool = False ) -> Self :
119- self .config .with_app (app , overwrite = overwrite )
120+ def model_post_init (self , __context ) -> None :
121+ self ._register_dependencies ()
122+
123+ @property
124+ def name (self ) -> str | None :
125+ return self .config .name
126+
127+ @name .setter
128+ def name (self , value : str ) -> None :
129+ self .config .name = value
130+ self ._reconcile ("name" )
131+
132+ @property
133+ def namespace (self ) -> str | None :
134+ return self .config .namespace
135+
136+ @namespace .setter
137+ def namespace (self , value : str ) -> None :
138+ self .config .namespace = value
139+ self ._reconcile ("namespace" )
140+
141+ @property
142+ def app (self ) -> str | None :
143+ try :
144+ return self .config .labels ["app" ]
145+ except (TypeError , KeyError ):
146+ return None
147+
148+ @app .setter
149+ def app (self , value : str ) -> None :
150+ self .config .with_app (value , overwrite = True )
151+ self ._reconcile ("app" )
152+
153+ def with_name (self , name : str ) -> Self :
154+ self .name = name
155+ return self
156+
157+ def with_namespace (self , namespace : str ) -> Self :
158+ self .namespace = namespace
159+ return self
160+
161+ def with_app (self , app : str ) -> Self :
162+ self .app = app
163+ return self
164+
165+ def with_image_in_container (
166+ self , image : Image , container_name : str , * , overwrite : bool = False
167+ ) -> Self :
168+ with_image_for_container (
169+ config = self .config , image = image , container_name = container_name , overwrite = overwrite
170+ )
171+ return self
172+
173+ def _register_dependencies (self ) -> None :
174+ self ._dependency_registry = {}
175+ for cls in type (self ).mro ():
176+ for name , maybe_method in cls .__dict__ .items ():
177+ fields = getattr (maybe_method , "_depends_on_fields" , None )
178+ if fields :
179+ for field in fields :
180+ self ._dependency_registry .setdefault (field , []).append (name )
181+
182+ def _reconcile (self , changed_field : Optional [str ] = None ) -> Self :
183+ if changed_field is None :
184+ return self
185+
186+ for method_name in self ._dependency_registry .get (changed_field , []):
187+ method = getattr (self , method_name )
188+ required_fields = getattr (method , "_depends_on_fields" , set ())
189+ if all (getattr (self , field , None ) is not None for field in required_fields ):
190+ method ()
191+
120192 return self
121193
122194 def build (self ) -> V1Pod :
@@ -142,10 +214,12 @@ def with_selector(self, key: str, value: str) -> Self:
142214 self .config .service_spec .with_selector (key , value )
143215 return self
144216
145- def with_port (self , port : V1ServicePort ) -> Self :
146- if self .config .service_spec .ports is None :
147- self .config .service_spec .ports = []
148- self .config .service_spec .ports .append (port )
217+ def with_port (self , new_port : V1ServicePort ) -> Self :
218+ self .config .service_spec .with_port (new_port )
219+ return self
220+
221+ def with_type (self , spec_type : ServiceSpecType ) -> Self :
222+ self .config .service_spec .spec_type = spec_type
149223 return self
150224
151225 def with_publish_not_ready_addresses (self , value : bool = True ) -> Self :
0 commit comments