@@ -77,7 +77,7 @@ def get_k8s_client() -> Client:
77
77
network_api = k8sclient .NetworkingV1Api (),
78
78
)
79
79
except ConfigException as e :
80
- raise ContainerEngineInitError (str (e ))
80
+ raise ContainerEngineInitError (str (e )) from e
81
81
82
82
83
83
class Engine (ContainerEngine ):
@@ -93,6 +93,8 @@ def __init__(
93
93
94
94
self ._set_namespace ()
95
95
self .secret_name = f"activation-secret-{ activation_id } "
96
+ self .job_name = None
97
+ self .pod_name = None
96
98
97
99
def start (self , request : ContainerRequest , log_handler : LogHandler ) -> str :
98
100
# TODO : Should this be compatible with the previous version
@@ -237,10 +239,12 @@ def update_logs(self, container_id: str, log_handler: LogHandler) -> None:
237
239
log_handler .flush ()
238
240
log_handler .set_log_read_at (dt )
239
241
else :
240
- LOGGER . warning ( f"Pod with label { container_id } not found." )
241
- log_handler . write (
242
- f"Pod with label { container_id } not found." , True
242
+ msg = (
243
+ f"Pod with label { container_id } has unhandled state: "
244
+ f"{ container_status . state } ."
243
245
)
246
+ LOGGER .warning (msg )
247
+ log_handler .write (msg , flush = True )
244
248
245
249
# ContainerUpdateLogsError handled by the manager
246
250
except ApiException as e :
@@ -259,7 +263,7 @@ def _get_job_pod(self, job_name: str) -> k8sclient.V1Pod:
259
263
return result .items [0 ]
260
264
except ApiException as e :
261
265
LOGGER .error (f"API Exception { e } " )
262
- raise ContainerNotFoundError (str (e ))
266
+ raise ContainerNotFoundError (str (e )) from e
263
267
264
268
def _create_container_spec (
265
269
self ,
@@ -358,7 +362,7 @@ def _create_service(self, port: int) -> None:
358
362
LOGGER .info (f"Service already exists: { service_name } " )
359
363
except ApiException as e :
360
364
LOGGER .error (f"API Exception { e } " )
361
- raise ContainerStartError (str (e ))
365
+ raise ContainerStartError (str (e )) from e
362
366
363
367
def _delete_services (self , log_handler : LogHandler ) -> None :
364
368
try :
@@ -378,7 +382,7 @@ def _delete_services(self, log_handler: LogHandler) -> None:
378
382
log_handler .write (f"Service { service_name } is deleted." , True )
379
383
except ApiException as e :
380
384
LOGGER .error (f"API Exception { e } " )
381
- raise ContainerCleanupError (str (e ))
385
+ raise ContainerCleanupError (str (e )) from e
382
386
383
387
def _create_job (
384
388
self ,
@@ -415,7 +419,7 @@ def _create_job(
415
419
LOGGER .info (f"Submitted Job template: { self .job_name } ," )
416
420
except ApiException as e :
417
421
LOGGER .error (f"API Exception { e } " )
418
- raise ContainerStartError (str (e ))
422
+ raise ContainerStartError (str (e )) from e
419
423
420
424
return job_result
421
425
@@ -446,7 +450,7 @@ def _delete_job(self, log_handler: LogHandler) -> None:
446
450
except ApiException as e :
447
451
raise ContainerCleanupError (
448
452
f"Stop of { self .job_name } Failed: \n { e } "
449
- )
453
+ ) from e
450
454
451
455
def _wait_for_pod_to_start (self , log_handler : LogHandler ) -> None :
452
456
watcher = watch .Watch ()
@@ -479,7 +483,8 @@ def _wait_for_pod_to_start(self, log_handler: LogHandler) -> None:
479
483
except ApiException as e :
480
484
raise ContainerStartError (
481
485
f"Pod { self .pod_name } failed with error { e } "
482
- )
486
+ ) from e
487
+
483
488
finally :
484
489
watcher .stop ()
485
490
@@ -488,14 +493,16 @@ def _set_namespace(self) -> None:
488
493
"/var/run/secrets/kubernetes.io/serviceaccount/namespace"
489
494
)
490
495
try :
491
- with open (namespace_file , "r" ) as namespace_ref :
496
+ with open (
497
+ namespace_file , mode = "r" , encoding = "utf-8"
498
+ ) as namespace_ref :
492
499
self .namespace = namespace_ref .read ()
493
500
494
501
LOGGER .info (f"Namespace is { self .namespace } " )
495
- except FileNotFoundError :
502
+ except FileNotFoundError as e :
496
503
message = f"Namespace file { namespace_file } does not exist."
497
504
LOGGER .error (message )
498
- raise ContainerEngineInitError (message )
505
+ raise ContainerEngineInitError (message ) from e
499
506
500
507
def _create_secret (
501
508
self ,
@@ -542,7 +549,7 @@ def _create_secret(
542
549
LOGGER .info (f"Created secret: name: { self .secret_name } " )
543
550
except ApiException as e :
544
551
LOGGER .error (f"API Exception { e } " )
545
- raise ContainerStartError (str (e ))
552
+ raise ContainerStartError (str (e )) from e
546
553
547
554
def _delete_secret (self , log_handler : LogHandler ) -> None :
548
555
try :
@@ -573,4 +580,4 @@ def _delete_secret(self, log_handler: LogHandler) -> None:
573
580
log_handler .write (message , True )
574
581
except ApiException as e :
575
582
LOGGER .error (f"API Exception { e } " )
576
- raise ContainerCleanupError (str (e ))
583
+ raise ContainerCleanupError (str (e )) from e
0 commit comments