Fokko closed pull request #4218: [AIRFLOW-3378] KubernetesPodOperator does not 
delete on timeout failure
URL: https://github.com/apache/incubator-airflow/pull/4218
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/operators/kubernetes_pod_operator.py 
b/airflow/contrib/operators/kubernetes_pod_operator.py
index 99c6da11b3..f6c1f9d45e 100644
--- a/airflow/contrib/operators/kubernetes_pod_operator.py
+++ b/airflow/contrib/operators/kubernetes_pod_operator.py
@@ -124,13 +124,14 @@ def execute(self, context):
 
             launcher = pod_launcher.PodLauncher(kube_client=client,
                                                 extract_xcom=self.xcom_push)
-            (final_state, result) = launcher.run_pod(
-                pod,
-                startup_timeout=self.startup_timeout_seconds,
-                get_logs=self.get_logs)
-
-            if self.is_delete_operator_pod:
-                launcher.delete_pod(pod)
+            try:
+                (final_state, result) = launcher.run_pod(
+                    pod,
+                    startup_timeout=self.startup_timeout_seconds,
+                    get_logs=self.get_logs)
+            finally:
+                if self.is_delete_operator_pod:
+                    launcher.delete_pod(pod)
 
             if final_state != State.SUCCESS:
                 raise AirflowException(
diff --git a/tests/contrib/minikube/test_kubernetes_pod_operator.py 
b/tests/contrib/minikube/test_kubernetes_pod_operator.py
index f808a2f47f..e1dfce28c4 100644
--- a/tests/contrib/minikube/test_kubernetes_pod_operator.py
+++ b/tests/contrib/minikube/test_kubernetes_pod_operator.py
@@ -107,6 +107,27 @@ def test_image_pull_secrets_correctly_set(self, 
client_mock, launcher_mock):
         self.assertEqual(launcher_mock.call_args[0][0].image_pull_secrets,
                          fake_pull_secrets)
 
+    @mock.patch("airflow.contrib.kubernetes.pod_launcher.PodLauncher.run_pod")
+    
@mock.patch("airflow.contrib.kubernetes.pod_launcher.PodLauncher.delete_pod")
+    @mock.patch("airflow.contrib.kubernetes.kube_client.get_kube_client")
+    def test_pod_delete_even_on_launcher_error(self, client_mock, 
delete_pod_mock, run_pod_mock):
+        k = KubernetesPodOperator(
+            namespace='default',
+            image="ubuntu:16.04",
+            cmds=["bash", "-cx"],
+            arguments=["echo 10"],
+            labels={"foo": "bar"},
+            name="test",
+            task_id="task",
+            in_cluster=False,
+            cluster_context='default',
+            is_delete_operator_pod=True
+        )
+        run_pod_mock.side_effect = AirflowException('fake failure')
+        with self.assertRaises(AirflowException):
+            k.execute(None)
+        delete_pod_mock.assert_called()
+
     @staticmethod
     def test_working_pod():
         k = KubernetesPodOperator(


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to