@@ -446,7 +446,7 @@ def destroy(self, namespace):
446446 except KubeException :
447447 break
448448
449- def run (self , name , image , entrypoint , command ):
449+ def run (self , name , image , entrypoint , command , ** kwargs ):
450450 """Run a one-off command."""
451451 logger .debug ('run {}, img {}, entrypoint {}, cmd "{}"' .format (
452452 name , image , entrypoint , command ))
@@ -470,8 +470,12 @@ def run(self, name, image, entrypoint, command):
470470 args = [command [1 :- 1 ]]
471471
472472 js_template = json .loads (template )
473- js_template ['spec' ]['containers' ][0 ]['command' ] = [entrypoint ]
474- js_template ['spec' ]['containers' ][0 ]['args' ] = args
473+ containers = js_template ['spec' ]['containers' ][0 ]
474+ containers ['command' ] = [entrypoint ]
475+ containers ['args' ] = args
476+
477+ self ._set_environment (containers , ** kwargs )
478+
475479 url = self ._api ("/namespaces/{}/pods" , appname )
476480 resp = self .session .post (url , json = js_template )
477481 if unhealthy (resp .status_code ):
@@ -508,6 +512,39 @@ def run(self, name, image, entrypoint, command):
508512 return err_code , data
509513 return 0 , data
510514
515+ def _set_environment (self , json_data , ** kwargs ):
516+ app_type = kwargs .get ('app_type' )
517+ mem = kwargs .get ('memory' , {}).get (app_type )
518+ cpu = kwargs .get ('cpu' , {}).get (app_type )
519+ env = kwargs .get ('envs' , {})
520+
521+ if env :
522+ for key , value in env .items ():
523+ json_data ["env" ].append ({
524+ "name" : key ,
525+ "value" : str (value )
526+ })
527+
528+ # Inject debugging if workflow is in debug mode
529+ if os .environ .get ("DEBUG" , False ):
530+ json_data ["env" ].append ({
531+ "name" : "DEBUG" ,
532+ "value" : "1"
533+ })
534+
535+ if mem or cpu :
536+ json_data ["resources" ] = {"limits" : {}}
537+
538+ if mem :
539+ if mem [- 2 :- 1 ].isalpha () and mem [- 1 ].isalpha ():
540+ mem = mem [:- 1 ]
541+
542+ mem = mem + "i"
543+ json_data ["resources" ]["limits" ]["memory" ] = mem
544+
545+ if cpu :
546+ json_data ["resources" ]["limits" ]["cpu" ] = cpu
547+
511548 def state (self , name ):
512549 """Display the state of a container."""
513550 try :
@@ -896,36 +933,8 @@ def _create_rc(self, namespace, name, image, command, **kwargs): # noqa
896933 containers [0 ]['args' ] = args
897934 loc = locals ().copy ()
898935 loc .update (re .match (MATCH , container_fullname ).groupdict ())
899- mem = kwargs .get ('memory' , {}).get (app_type )
900- cpu = kwargs .get ('cpu' , {}).get (app_type )
901- env = kwargs .get ('envs' , {})
902936
903- if env :
904- for key , value in env .items ():
905- containers [0 ]["env" ].append ({
906- "name" : key ,
907- "value" : str (value )
908- })
909-
910- # Inject debugging if workflow is in debug mode
911- if os .environ .get ("DEBUG" , False ):
912- containers [0 ]["env" ].append ({
913- "name" : "DEBUG" ,
914- "value" : "1"
915- })
916-
917- if mem or cpu :
918- containers [0 ]["resources" ] = {"limits" : {}}
919-
920- if mem :
921- if mem [- 2 :- 1 ].isalpha () and mem [- 1 ].isalpha ():
922- mem = mem [:- 1 ]
923-
924- mem = mem + "i"
925- containers [0 ]["resources" ]["limits" ]["memory" ] = mem
926-
927- if cpu :
928- containers [0 ]["resources" ]["limits" ]["cpu" ] = cpu
937+ self ._set_environment (containers [0 ], ** kwargs )
929938
930939 # add in healtchecks
931940 if kwargs .get ('healthcheck' ):
0 commit comments