You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ep...@apache.org on 2022/06/29 15:20:18 UTC

[airflow] 33/45: Return empty dict if Pod JSON encoding fails (#24478)

This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch v2-3-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 4ab96bac198b27f7c558326e526be22e945e4c4d
Author: Daniel Standish <15...@users.noreply.github.com>
AuthorDate: Wed Jun 15 12:47:07 2022 -0700

    Return empty dict if Pod JSON encoding fails (#24478)
    
    When UI unpickles executor_configs with outdated k8s objects it can run into the same issue as the scheduler does (see https://github.com/apache/airflow/issues/23727).
    
    Our JSON encoder therefore needs to suppress encoding errors arising from pod serialization, and fallback to a default value.
---
 airflow/utils/json.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/airflow/utils/json.py b/airflow/utils/json.py
index 9fc6495982..326a2f1b43 100644
--- a/airflow/utils/json.py
+++ b/airflow/utils/json.py
@@ -16,6 +16,7 @@
 # specific language governing permissions and limitations
 # under the License.
 
+import logging
 from datetime import date, datetime
 from decimal import Decimal
 
@@ -33,6 +34,8 @@ except ImportError:
 
 # Dates and JSON encoding/decoding
 
+log = logging.getLogger(__name__)
+
 
 class AirflowJsonEncoder(JSONEncoder):
     """Custom Airflow json encoder implementation."""
@@ -81,6 +84,21 @@ class AirflowJsonEncoder(JSONEncoder):
         elif k8s is not None and isinstance(obj, (k8s.V1Pod, k8s.V1ResourceRequirements)):
             from airflow.kubernetes.pod_generator import PodGenerator
 
-            return PodGenerator.serialize_pod(obj)
+            def safe_get_name(pod):
+                """
+                We're running this in an except block, so we don't want it to
+                fail under any circumstances, e.g. by accessing an attribute that isn't there
+                """
+                try:
+                    return pod.metadata.name
+                except Exception:
+                    return None
+
+            try:
+                return PodGenerator.serialize_pod(obj)
+            except Exception:
+                log.warning("JSON encoding failed for pod %s", safe_get_name(obj))
+                log.debug("traceback for pod JSON encode error", exc_info=True)
+                return {}
 
         raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")