amoghrajesh commented on code in PR #68831:
URL: https://github.com/apache/airflow/pull/68831#discussion_r3458410572
##########
airflow-core/tests/unit/serialization/test_serialized_objects.py:
##########
@@ -1389,6 +1389,45 @@ def
test_v1pod_serde_without_cncf_kubernetes_provider(self, monkeypatch):
_has_kubernetes.cache_clear()
+ def test_deserialized_v1pod_does_not_capture_unpicklable_config(self,
monkeypatch):
+ """A deserialized V1Pod must not capture the in-cluster Configuration.
+
+ In-cluster, the kubernetes client installs a process-global default
``Configuration`` whose
+ ``refresh_api_key_hook`` is an unpicklable local closure. Under
kubernetes-client v36,
+ ``ApiClient.__deserialize_model`` copies that config onto the pod and
every nested model, so a
+ naively deserialized ``pod_override`` cannot be pickled onto the
KubernetesExecutor queue and
+ crashes the scheduler. Deserializing through a fresh ``Configuration``
keeps the pod picklable.
+ """
+ import pickle
Review Comment:
Top level import pls :)
##########
airflow-core/tests/unit/utils/test_sqlalchemy.py:
##########
@@ -346,3 +346,38 @@ def test_result_processor_bad_pickled_obj(self, mocker):
# show that the pickled (bad) pod is now a good pod, and same as the
copy made
# before making it bad
assert result["pod_override"].to_dict() == copy_of_test_pod.to_dict()
+
+ def
test_ensure_pod_is_valid_after_unpickling_is_picklable_in_cluster(self,
monkeypatch):
+ """The repaired pod must not capture the unpicklable in-cluster
Configuration.
+
+ In-cluster, the kubernetes client installs a process-global default
``Configuration`` whose
+ ``refresh_api_key_hook`` is an unpicklable local closure. When the
repair branch re-deserializes
+ the pod it must round-trip through a fresh ``Configuration`` so it
stays picklable onto the
+ KubernetesExecutor queue.
+ """
+ from kubernetes.client import Configuration
Review Comment:
Same here (but this one can stay actually)
##########
airflow-core/src/airflow/serialization/serialized_objects.py:
##########
@@ -643,9 +643,9 @@ def deserialize(cls, encoded_var: Any) -> Any:
"Cannot deserialize POD objects without kubernetes
libraries. "
"Please install the `kubernetes` package."
)
- # kubernetes-client does not expose a public dict->model API; see
https://github.com/kubernetes-client/python/issues/977.
- pod = ApiClient()._ApiClient__deserialize_model(var, k8s.V1Pod)
- return pod
+ from airflow.utils.sqlalchemy import deserialize_pod_dict
Review Comment:
Any reason to have a local import?
##########
airflow-core/tests/unit/serialization/test_serialized_objects.py:
##########
@@ -1389,6 +1389,45 @@ def
test_v1pod_serde_without_cncf_kubernetes_provider(self, monkeypatch):
_has_kubernetes.cache_clear()
+ def test_deserialized_v1pod_does_not_capture_unpicklable_config(self,
monkeypatch):
+ """A deserialized V1Pod must not capture the in-cluster Configuration.
+
+ In-cluster, the kubernetes client installs a process-global default
``Configuration`` whose
+ ``refresh_api_key_hook`` is an unpicklable local closure. Under
kubernetes-client v36,
+ ``ApiClient.__deserialize_model`` copies that config onto the pod and
every nested model, so a
+ naively deserialized ``pod_override`` cannot be pickled onto the
KubernetesExecutor queue and
+ crashes the scheduler. Deserializing through a fresh ``Configuration``
keeps the pod picklable.
+ """
+ import pickle
+
+ k8s = pytest.importorskip("kubernetes.client.models")
+ from kubernetes.client import Configuration
+
+ def _make_unpicklable_hook():
+ def _refresh_api_key(config):
+ return None
+
+ return _refresh_api_key
+
+ dirty = Configuration()
+ dirty.refresh_api_key_hook = _make_unpicklable_hook()
+ monkeypatch.setattr(Configuration, "_default", dirty, raising=False)
+ _has_kubernetes.cache_clear()
+
+ pod = k8s.V1Pod(
+ metadata=k8s.V1ObjectMeta(name="test-pod"),
+ spec=k8s.V1PodSpec(containers=[k8s.V1Container(name="base",
image="airflow:3")]),
+ )
+ decoded =
BaseSerialization.deserialize(BaseSerialization.serialize(pod))
+
+ assert isinstance(decoded, k8s.V1Pod)
+ # The top-level pod and every nested model must carry a clean,
picklable Configuration.
+ pickle.dumps(decoded)
+ assert decoded.local_vars_configuration.refresh_api_key_hook is None
+ assert
decoded.spec.containers[0].local_vars_configuration.refresh_api_key_hook is None
+
+ _has_kubernetes.cache_clear()
Review Comment:
Is this really needed? I guess its bleeding from other tests but yeah just
flagging
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]