Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spark-on-k8s sensor logs - properly pass defined namespace to pod log call #11199

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions airflow/providers/cncf/kubernetes/sensors/spark_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ def __init__(
self.kubernetes_conn_id = kubernetes_conn_id
self.hook = KubernetesHook(conn_id=self.kubernetes_conn_id)

def _log_driver(self, application_state: str) -> None:
def _log_driver(self, application_state: str, response: dict) -> None:
if not self.attach_log:
return
driver_pod_name = f"{self.application_name}-driver"
status_info = response["status"]
if "driverInfo" not in status_info:
return
driver_info = status_info["driverInfo"]
if "podName" not in driver_info:
return
driver_pod_name = driver_info["podName"]
namespace = response["metadata"]["namespace"]
log_method = self.log.error if application_state in self.FAILURE_STATES else self.log.info
try:
log = ""
for line in self.hook.get_pod_logs(driver_pod_name):
for line in self.hook.get_pod_logs(driver_pod_name, namespace=namespace):
log += line.decode()
log_method(log)
except client.rest.ApiException as e:
Expand All @@ -97,7 +104,7 @@ def poke(self, context: Dict) -> bool:
except KeyError:
return False
if self.attach_log and application_state in self.FAILURE_STATES + self.SUCCESS_STATES:
self._log_driver(application_state)
self._log_driver(application_state, response)
if application_state in self.FAILURE_STATES:
raise AirflowException("Spark application failed with state: %s" % application_state)
elif application_state in self.SUCCESS_STATES:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def test_driver_logging_failure(
task_id="test_task_id",
)
self.assertRaises(AirflowException, sensor.poke, None)
mock_log_call.assert_called_once_with("spark_pi-driver")
mock_log_call.assert_called_once_with("spark-pi-driver", namespace="default")
error_log_call.assert_called_once_with(TEST_POD_LOG_RESULT)

@patch(
Expand All @@ -719,7 +719,7 @@ def test_driver_logging_completed(
task_id="test_task_id",
)
sensor.poke(None)
mock_log_call.assert_called_once_with("spark_pi-driver")
mock_log_call.assert_called_once_with("spark-pi-2020-02-24-1-driver", namespace="default")
log_info_call = info_log_call.mock_calls[1]
log_value = log_info_call[1][0]
self.assertEqual(log_value, TEST_POD_LOG_RESULT)
Expand Down