@@ -224,6 +224,8 @@ def _set_container(self, namespace, container_name, data, **kwargs):
224224
225225 self ._set_health_checks (data , env , ** kwargs )
226226
227+ self ._set_lifecycle_hooks (data , env , ** kwargs )
228+
227229 def _set_resources (self , container , kwargs ):
228230 """ Set CPU/memory resource management manifest """
229231 app_type = kwargs .get ("app_type" )
@@ -278,6 +280,38 @@ def _set_health_checks(self, container, env, **kwargs):
278280 elif kwargs .get ('routable' , False ):
279281 self ._default_readiness_probe (container , kwargs .get ('build_type' ), env .get ('PORT' , None )) # noqa
280282
283+ def _set_lifecycle_hooks (self , container , env , ** kwargs ):
284+ app_type = kwargs .get ("app_type" )
285+ lifecycle_post_start = kwargs .get ('lifecycle_post_start' , {})
286+ lifecycle_post_start = lifecycle_post_start .get (app_type )
287+ lifecycle_pre_stop = kwargs .get ('lifecycle_pre_stop' , {})
288+ lifecycle_pre_stop = lifecycle_pre_stop .get (app_type )
289+ lifecycle = defaultdict (dict )
290+ if lifecycle_post_start or lifecycle_pre_stop :
291+ lifecycle = defaultdict (dict )
292+
293+ if lifecycle_post_start :
294+ lifecycle ["postStart" ] = {
295+ 'exec' : {
296+ "command" : [
297+ "/bin/bash" ,
298+ "-c" ,
299+ "{0}" .format (lifecycle_post_start )
300+ ]
301+ }
302+ }
303+ if lifecycle_pre_stop :
304+ lifecycle ["preStop" ] = {
305+ 'exec' : {
306+ "command" : [
307+ "/bin/bash" ,
308+ "-c" ,
309+ "{0}" .format (lifecycle_pre_stop )
310+ ]
311+ }
312+ }
313+ container ["lifecycle" ] = dict (lifecycle )
314+
281315 def _default_readiness_probe (self , container , build_type , port = None ):
282316 # Update only the application container with the health check
283317 if build_type == "buildpack" :
@@ -345,6 +379,15 @@ def _default_dockerapp_readiness_probe(self, port, delay=5, timeout=5, period_se
345379 }
346380 return readinessprobe
347381
382+ def _set_custom_termination_period (self , container , period_seconds = 900 ):
383+ """
384+ Applies a custom terminationGracePeriod only if provided as env variable.
385+ """
386+ terminationperiod = {
387+ 'terminationGracePeriodSeconds' : int (period_seconds )
388+ }
389+ container .update (terminationperiod )
390+
348391 def delete (self , namespace , name ):
349392 # get timeout info from pod
350393 pod = self .pod .get (namespace , name ).json ()
0 commit comments