Kubernetes pod returning error message to Airflow scheduler

1/11/2022

I'm scheduling jobs to run on Kubernetes using Airflow, with the KubernetesPodOperator. When the Pod fails it returns the output yaml which can also be retrieved with this command

kubectl get pod mypod -o yaml

To include "custom" error messages in the pod error I understood that you can send it to the /dev/termination-log as described here (https://kubernetes.io/docs/tasks/debug-application-cluster/determine-reason-pod-failure/). Like so:

echo "Sleep expired" > /dev/termination-log"

On the Pods I'm running DBT and DBT creates a run_results.json (https://docs.getdbt.com/reference/artifacts/run-results-json) which contains structured data regarding the tasks I run in my pods. So what I'm doing in my pod is that I write this file to the termination log:

cat run_results.json >> /dev/termination-log

Then in the output yaml I can find this information in the containerStatuses.state.terminated.message attribute. The issue I'm running in to is that by the time I get the run_results.json in Airflow the formatting is all messed up. It looks something like:

  'message': '{"metadata": '
        '{"dbt_schema_version": '
        '"https://schemas.getdbt.com/dbt/run-results/v2.json", '
        '"dbt_version": '
        '"0.20.2", '
        '"generated_at": '
        '"2022-01-10T13:09:24.634232Z", '
        '"invocation_id": '
        '"xxxxxxx123", '
        '"env": '
        '{}}, '
        '"results": '
        '[{"status": '
        '"pass", '
        '"timing": '
        '[{"name": '
        '"compile", '
        '"started_at": '
        '"2022-01-10T13:09:21.871022Z", '
        '"completed_at": '
        '"2022-01-10T13:09:22.277936Z"}, '

When it initially looks like this:

{"metadata": {"dbt_schema_version": "https://schemas.getdbt.com/dbt/run-results/v2.json", "dbt_version": "0.20.2", "generated_at": "2022-01-11T10:20:35.633208Z", "invocation_id": "xxxxx123", "env": {}}, "results": 

The output from the pod is in yaml format, the run_results.json is a json format and then in Airflow it somehow converts this to a dictionary, and I end up with this mess.

How can I retrieve the run_results.json which is generated on the pod and use it in Airflow to generate meaningful error messages?

-- Anton
airflow
dbt
error-handling
kubernetes
python

0 Answers