1717logger = logging .getLogger (__name__ )
1818
1919# Used for one off command runs on pods
20+ POD_BTEMPLATE = """\
21+ {
22+ "kind": "Pod",
23+ "apiVersion": "$version",
24+ "metadata": {
25+ "name": "$id"
26+ },
27+ "spec": {
28+ "containers": [
29+ {
30+ "name": "$id",
31+ "image": "quay.io/deisci/slugrunner:v2-beta",
32+ "env": [
33+ {
34+ "name":"PORT",
35+ "value":"5000"
36+ },
37+ {
38+ "name":"SLUG_URL",
39+ "value":"$image"
40+ },
41+ {
42+ "name": "DOCKERIMAGE",
43+ "value":"1"
44+ }
45+ ],
46+ "volumeMounts":[
47+ {
48+ "name":"minio-user",
49+ "mountPath":"/var/run/secrets/object/store",
50+ "readOnly":true
51+ }
52+ ]
53+ }
54+ ],
55+ "volumes":[
56+ {
57+ "name":"minio-user",
58+ "secret":{
59+ "secretName":"minio-user"
60+ }
61+ }
62+ ],
63+ "restartPolicy": "Never"
64+ }
65+ }
66+ """
67+
2068POD_TEMPLATE = """\
2169 {
2270 "kind": "Pod",
@@ -384,13 +432,17 @@ def run(self, name, image, entrypoint, command):
384432 name , image , entrypoint , command ))
385433 appname = name .split ('_' )[0 ]
386434 name = name .replace ('.' , '-' ).replace ('_' , '-' )
435+ imgurl = self .registry + '/' + image
436+ POD = POD_TEMPLATE
437+ if image .startswith ('http://' ) or image .startswith ('https://' ):
438+ POD = POD_BTEMPLATE
439+ imgurl = image
387440 l = {
388441 'id' : name ,
389442 'version' : self .apiversion ,
390- 'image' : self . registry + '/' + image ,
443+ 'image' : imgurl ,
391444 }
392-
393- template = string .Template (POD_TEMPLATE ).substitute (l )
445+ template = string .Template (POD ).substitute (l )
394446 if command .startswith ('-c ' ):
395447 args = command .split (' ' , 1 )
396448 args [1 ] = args [1 ][1 :- 1 ]
@@ -404,36 +456,36 @@ def run(self, name, image, entrypoint, command):
404456 resp = self .session .post (url , json = js_template )
405457 if unhealthy (resp .status_code ):
406458 error (resp , 'create Pod in Namespace "{}"' , appname )
407-
408- while (1 ):
409- parsed_json = {}
410- status = 404
411- reason = ''
412- data = ''
413- for _ in range (5 ):
459+ parsed_json = {}
460+ status = 404
461+ reason = ''
462+ data = ''
463+ duration = 30
464+ iteration = 1
465+ while (iteration < duration ):
466+ try :
414467 status , reason , data = self ._get_pod (name , appname )
415- if unhealthy (status ):
416- time .sleep (1 )
417- continue
418-
419468 parsed_json = json .loads (data )
469+ if parsed_json ['status' ]['phase' ] == 'Succeeded' :
470+ status , data , reason = self ._pod_log (name , appname )
471+ self ._delete_pod (name , appname )
472+ return 0 , data
473+ if parsed_json ['status' ]['phase' ] == 'Running' :
474+ if iteration > 28 :
475+ duration = duration + 1
476+ except :
420477 break
421-
422- if unhealthy (status ):
423- error (resp , 'create Pod in Namespace "{}"' , appname )
424-
425- if parsed_json ['status' ]['phase' ] == 'Succeeded' :
426- status , data , reason = self ._pod_log (name , appname )
427- self ._delete_pod (name , appname )
428- return 0 , data
429- elif parsed_json ['status' ]['phase' ] == 'Failed' :
430- pod_state = parsed_json ['status' ]['containerStatuses' ][0 ]['state' ]
431- err_code = pod_state ['terminated' ]['exitCode' ]
432- self ._delete_pod (name , appname )
433- return err_code , data
434-
478+ iteration = iteration + 1
435479 time .sleep (1 )
436480
481+ if iteration >= duration :
482+ error (resp , 'Pod start took more than 30 seconds' , appname )
483+ return 0 , data
484+ if parsed_json ['status' ]['phase' ] == 'Failed' :
485+ pod_state = parsed_json ['status' ]['containerStatuses' ][0 ]['state' ]
486+ err_code = pod_state ['terminated' ]['exitCode' ]
487+ self ._delete_pod (name , appname )
488+ return err_code , data
437489 return 0 , data
438490
439491 def state (self , name ):
0 commit comments