66
77from jinja2 import Template
88from kubernetes import client , config
9- from .utils import get_manager_pod
109
1110import state_machine_operator .defaults as defaults
1211import state_machine_operator .utils as utils
@@ -403,13 +402,49 @@ def get_metric_events(self, pod, log):
403402 except Exception as e :
404403 print (f"Error parsing custom metric for { pod .metadata .name } : { e } " )
405404 return
406-
405+
407406 # Metadata about the Job to associate to
408407 job_name = pod .metadata .labels ["job-name" ]
409408
410409 # In the message we send the metrics, job name, and step name
411410 return {"job_name" : job_name , "step_name" : self .job_desc ["name" ], "metrics" : events }
412411
412+ def send_kubernetes_event (self , pod , metrics ):
413+ """
414+ Send a kubernetes event instead of metrics via the state machine.
415+
416+ This function is currently not used.
417+ """
418+ v1 = client .CoreV1Api ()
419+ now = datetime .datetime .utcnow ()
420+ job_name = pod .metadata .labels ["job-name" ]
421+ job_uid = pod .metadata .labels ["controller-uid" ]
422+
423+ event = client .CoreV1Event (
424+ metadata = client .V1ObjectMeta (
425+ generate_name = job_name ,
426+ ),
427+ involved_object = client .V1ObjectReference (
428+ kind = "Job" ,
429+ api_version = "batch/v1" ,
430+ namespace = pod .metadata .namespace ,
431+ name = job_name ,
432+ uid = job_uid ,
433+ ),
434+ reason = "CustomMetric" ,
435+ message = json .dumps (metrics ),
436+ first_timestamp = now .strftime ("%Y-%m-%dT%H:%M:%SZ" ),
437+ last_timestamp = now .strftime ("%Y-%m-%dT%H:%M:%SZ" ),
438+ type = "Normal" ,
439+ source = client .V1EventSource (component = "state-machine" ),
440+ )
441+
442+ try :
443+ v1 .create_namespaced_event (pod .metadata .namespace , event )
444+ except Exception as e :
445+ print (f"Error creating event: { e } " )
446+ return metrics
447+
413448 def cancel_jobs (self , joblist ):
414449 """
415450 For the given job list, cancel each job. This is not currently use,
@@ -466,7 +501,7 @@ def save_log(self, job=None):
466501 Save a log identifier for a finished pod (job)
467502 """
468503 # No job, no purpose to save
469- if not Job :
504+ if not job :
470505 return
471506 api = client .CoreV1Api ()
472507
@@ -486,7 +521,6 @@ def save_log(self, job=None):
486521 # For metrics, we assume logs coming from main (index 0) pod
487522 # This can change if needed
488523 for i , pod in enumerate (pods ):
489- print (f"Saving log for { pod .metadata .name } " )
490524 try :
491525 log = api .read_namespaced_pod_log (
492526 name = pod .metadata .name ,
@@ -495,6 +529,8 @@ def save_log(self, job=None):
495529 timestamps = True ,
496530 )
497531 if i == 0 :
532+ print (f"Saving log for { pod .metadata .name } " )
533+
498534 # We assume lead pod (index 0) is of interest
499535 metrics = self .adapter .get_metric_events (pod , log )
500536 if metrics :
0 commit comments