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

Fix SparkKubernetesOperator spark name. #42427

Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 10 additions & 4 deletions airflow/providers/cncf/kubernetes/operators/spark_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(
image: str | None = None,
code_path: str | None = None,
namespace: str = "default",
name: str = "default",
name: str | None = None,
application_file: str | None = None,
template_spec=None,
get_logs: bool = True,
Expand All @@ -103,7 +103,6 @@ def __init__(
self.code_path = code_path
self.application_file = application_file
self.template_spec = template_spec
self.name = self.create_job_name()
self.kubernetes_conn_id = kubernetes_conn_id
self.startup_timeout_seconds = startup_timeout_seconds
self.reattach_on_restart = reattach_on_restart
Expand Down Expand Up @@ -161,8 +160,13 @@ def manage_template_specs(self):
return template_body

def create_job_name(self):
initial_name = add_unique_suffix(name=self.task_id, max_len=MAX_LABEL_LEN)
return re.sub(r"[^a-z0-9-]+", "-", initial_name.lower())
name = (
self.name or self.template_body.get("spark", {}).get("metadata", {}).get("name") or self.task_id
)

updated_name = add_unique_suffix(name=name, max_len=MAX_LABEL_LEN)

return re.sub(r"[^a-z0-9-]+", "-", updated_name.lower())
gopidesupavan marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def _get_pod_identifying_label_string(labels) -> str:
Expand Down Expand Up @@ -282,6 +286,8 @@ def custom_obj_api(self) -> CustomObjectsApi:
return CustomObjectsApi()

def execute(self, context: Context):
self.name = self.create_job_name()
romsharon98 marked this conversation as resolved.
Show resolved Hide resolved

self.log.info("Creating sparkApplication.")
self.launcher = CustomObjectLauncher(
name=self.name,
Expand Down
96 changes: 96 additions & 0 deletions tests/providers/cncf/kubernetes/operators/test_spark_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,102 @@ def test_create_application_from_yaml_json(
version="v1beta2",
)

def test_create_application_from_yaml_json_and_use_name_from_metadata(
self,
mock_create_namespaced_crd,
mock_get_namespaced_custom_object_status,
mock_cleanup,
mock_create_job_name,
mock_get_kube_client,
mock_create_pod,
mock_await_pod_start,
mock_await_pod_completion,
mock_fetch_requested_container_logs,
data_file,
):
op = SparkKubernetesOperator(
application_file=data_file("spark/application_test.yaml").as_posix(),
kubernetes_conn_id="kubernetes_default_kube_config",
task_id="create_app_and_use_name_from_metadata",
)
context = create_context(op)
op.execute(context)
TEST_APPLICATION_DICT["metadata"]["name"] = op.name
gopidesupavan marked this conversation as resolved.
Show resolved Hide resolved
mock_create_namespaced_crd.assert_called_with(
body=TEST_APPLICATION_DICT,
group="sparkoperator.k8s.io",
namespace="default",
plural="sparkapplications",
version="v1beta2",
)
assert op.name.startswith("default_yaml")

op = SparkKubernetesOperator(
application_file=data_file("spark/application_test.json").as_posix(),
kubernetes_conn_id="kubernetes_default_kube_config",
task_id="create_app_and_use_name_from_metadata",
)
context = create_context(op)
op.execute(context)
TEST_APPLICATION_DICT["metadata"]["name"] = op.name
mock_create_namespaced_crd.assert_called_with(
body=TEST_APPLICATION_DICT,
group="sparkoperator.k8s.io",
namespace="default",
plural="sparkapplications",
version="v1beta2",
)
assert op.name.startswith("default_json")

def test_create_application_from_yaml_json_and_use_name_from_operator_args(
self,
mock_create_namespaced_crd,
mock_get_namespaced_custom_object_status,
mock_cleanup,
mock_create_job_name,
mock_get_kube_client,
mock_create_pod,
mock_await_pod_start,
mock_await_pod_completion,
mock_fetch_requested_container_logs,
data_file,
):
op = SparkKubernetesOperator(
application_file=data_file("spark/application_test.yaml").as_posix(),
kubernetes_conn_id="kubernetes_default_kube_config",
task_id="default_yaml",
name="test-spark",
)
context = create_context(op)
op.execute(context)
TEST_APPLICATION_DICT["metadata"]["name"] = op.name
mock_create_namespaced_crd.assert_called_with(
body=TEST_APPLICATION_DICT,
group="sparkoperator.k8s.io",
namespace="default",
plural="sparkapplications",
version="v1beta2",
)
assert op.name.startswith("test-spark")

op = SparkKubernetesOperator(
application_file=data_file("spark/application_test.json").as_posix(),
kubernetes_conn_id="kubernetes_default_kube_config",
task_id="default_json",
name="test-spark",
)
context = create_context(op)
op.execute(context)
TEST_APPLICATION_DICT["metadata"]["name"] = op.name
mock_create_namespaced_crd.assert_called_with(
body=TEST_APPLICATION_DICT,
group="sparkoperator.k8s.io",
namespace="default",
plural="sparkapplications",
version="v1beta2",
)
assert op.name.startswith("test-spark")

def test_new_template_from_yaml(
self,
mock_create_namespaced_crd,
Expand Down