Skip to content

Commit 63b2bbd

Browse files
authored
Add random_name_suffix to SparkKubernetesOperator (#43800) (#43847)
* Add random_name_suffix to SparkKubernetesOperator (#43800) Prior to this change, `random_name_suffix` was only documented but not implemented as a configurable option. Passing this value as an argument had no effect. This commit introduces a `false` option for `random_name_suffix`, which prevents the generation of a random suffix for the pod name. For compatibility, the default value is set to `true`, ensuring the pod name will still conform to `MAX_LABEL_LEN = 63`. Fixes: #43800 * Add random_name_suffix to SparkKubernetesOperator (#43800) Prior to this change, `random_name_suffix` was only documented but not implemented as a configurable option. Passing this value as an argument had no effect. This commit introduces a `false` option for `random_name_suffix`, which prevents the generation of a random suffix for the pod name. For compatibility, the default value is set to `true`, ensuring the pod name will still conform to `MAX_LABEL_LEN = 63`. Fixes: #43800
1 parent 229750d commit 63b2bbd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

providers/src/airflow/providers/cncf/kubernetes/operators/spark_kubernetes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SparkKubernetesOperator(KubernetesPodOperator):
6868
state, or the execution is interrupted. If True (default), delete the
6969
pod; if False, leave the pod.
7070
:param kubernetes_conn_id: the connection to Kubernetes cluster
71+
:param random_name_suffix: If True, adds a random suffix to the pod name
7172
"""
7273

7374
template_fields = ["application_file", "namespace", "template_spec", "kubernetes_conn_id"]
@@ -94,6 +95,7 @@ def __init__(
9495
reattach_on_restart: bool = True,
9596
delete_on_termination: bool = True,
9697
kubernetes_conn_id: str = "kubernetes_default",
98+
random_name_suffix: bool = True,
9799
**kwargs,
98100
) -> None:
99101
if kwargs.get("xcom_push") is not None:
@@ -112,6 +114,7 @@ def __init__(
112114
self.get_logs = get_logs
113115
self.log_events_on_failure = log_events_on_failure
114116
self.success_run_history_limit = success_run_history_limit
117+
self.random_name_suffix = random_name_suffix
115118

116119
if self.base_container_name != self.BASE_CONTAINER_NAME:
117120
self.log.warning(
@@ -164,7 +167,11 @@ def create_job_name(self):
164167
self.name or self.template_body.get("spark", {}).get("metadata", {}).get("name") or self.task_id
165168
)
166169

167-
updated_name = add_unique_suffix(name=name, max_len=MAX_LABEL_LEN)
170+
if self.random_name_suffix:
171+
updated_name = add_unique_suffix(name=name, max_len=MAX_LABEL_LEN)
172+
else:
173+
# truncation is required to maintain the same behavior as before
174+
updated_name = name[:MAX_LABEL_LEN]
168175

169176
return self._set_name(updated_name)
170177

providers/tests/cncf/kubernetes/operators/test_spark_kubernetes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from datetime import date
2323
from unittest import mock
2424
from unittest.mock import patch
25+
from uuid import uuid4
2526

2627
import pendulum
2728
import pytest
@@ -31,6 +32,7 @@
3132
from airflow import DAG
3233
from airflow.models import Connection, DagRun, TaskInstance
3334
from airflow.providers.cncf.kubernetes.operators.spark_kubernetes import SparkKubernetesOperator
35+
from airflow.providers.cncf.kubernetes.pod_generator import MAX_LABEL_LEN
3436
from airflow.utils import db, timezone
3537
from airflow.utils.types import DagRunType
3638

@@ -828,3 +830,27 @@ def test_resolve_application_file_real_file_not_exists(create_task_instance_of_o
828830
task: SparkKubernetesOperator = ti.task
829831
with pytest.raises(TypeError, match="application_file body can't transformed into the dictionary"):
830832
_ = task.template_body
833+
834+
835+
@pytest.mark.parametrize(
836+
"random_name_suffix",
837+
[pytest.param(True, id="use-random_name_suffix"), pytest.param(False, id="skip-random_name_suffix")],
838+
)
839+
def test_create_job_name(random_name_suffix: bool):
840+
name = f"x{uuid4()}"
841+
op = SparkKubernetesOperator(task_id="task_id", name=name, random_name_suffix=random_name_suffix)
842+
pod_name = op.create_job_name()
843+
844+
if random_name_suffix:
845+
assert pod_name.startswith(name)
846+
assert pod_name != name
847+
else:
848+
assert pod_name == name
849+
850+
851+
def test_create_job_name_should_truncate_long_names():
852+
long_name = f"{uuid4()}" + "x" * MAX_LABEL_LEN
853+
op = SparkKubernetesOperator(task_id="task_id", name=long_name, random_name_suffix=False)
854+
pod_name = op.create_job_name()
855+
856+
assert pod_name == long_name[:MAX_LABEL_LEN]

0 commit comments

Comments
 (0)