This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 543e47af836 fix(kubernetes): Account for job- prefix when truncating 
job names (#58391)
543e47af836 is described below

commit 543e47af8364d265f62fcc860a06966c6e5564b1
Author: Diego Álvarez <[email protected]>
AuthorDate: Mon Nov 24 12:09:04 2025 +0100

    fix(kubernetes): Account for job- prefix when truncating job names (#58391)
    
    Kubernetes automatically creates a label `batch.kubernetes.io/job-name`
    with the job name as its value. This label value must not exceed 63
    characters, which means the job name itself must be ≤ 63 characters.
    
    Previously, the "job-" prefix was added after name truncation, which
    could cause job names to exceed the 63 character limit (e.g., 67
    characters), resulting in Kubernetes API validation errors:
    
      Job.batch "...job-name..." is invalid: metadata.labels: Invalid value:
      "...job-name...": must be no more than 63 characters
    
    This fix calculates the available length for the base name (63 - 4 = 59)
    and passes this reduced max_length to the name generation functions
    (create_unique_id and add_unique_suffix), ensuring the final name with
    prefix never exceeds 63 characters.
    
    The prefix is now stored in a module-level constant JOB_NAME_PREFIX for
    maintainability.
    
    Co-authored-by: Diego Álvarez García <[email protected]>
    Co-authored-by: Javier Prieto Cepeda <[email protected]>
    Co-authored-by: Javi Prieto <[email protected]>
---
 .../src/airflow/providers/cncf/kubernetes/operators/job.py   | 12 +++++++++---
 .../tests/unit/cncf/kubernetes/operators/test_job.py         |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git 
a/providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/job.py
 
b/providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/job.py
index 3d397eb5490..e136e7f564a 100644
--- 
a/providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/job.py
+++ 
b/providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/job.py
@@ -35,6 +35,7 @@ from airflow.configuration import conf
 from airflow.exceptions import AirflowException, 
AirflowProviderDeprecationWarning
 from airflow.providers.cncf.kubernetes.hooks.kubernetes import KubernetesHook
 from airflow.providers.cncf.kubernetes.kubernetes_helper_functions import (
+    POD_NAME_MAX_LENGTH,
     add_unique_suffix,
     create_unique_id,
 )
@@ -56,6 +57,8 @@ if TYPE_CHECKING:
 
 log = logging.getLogger(__name__)
 
+JOB_NAME_PREFIX = "job-"
+
 
 class KubernetesJobOperator(KubernetesPodOperator):
     """
@@ -378,15 +381,18 @@ class KubernetesJobOperator(KubernetesPodOperator):
 
         job = self.reconcile_jobs(job_template, job)
 
+        # Account for job name prefix when generating/truncating the name
+        max_base_length = POD_NAME_MAX_LENGTH - len(JOB_NAME_PREFIX)
+
         if not job.metadata.name:
             job.metadata.name = create_unique_id(
-                task_id=self.task_id, unique=self.random_name_suffix, 
max_length=80
+                task_id=self.task_id, unique=self.random_name_suffix, 
max_length=max_base_length
             )
         elif self.random_name_suffix:
             # user has supplied job name, we're just adding suffix
-            job.metadata.name = add_unique_suffix(name=job.metadata.name)
+            job.metadata.name = add_unique_suffix(name=job.metadata.name, 
max_len=max_base_length)
 
-        job.metadata.name = f"job-{job.metadata.name}"
+        job.metadata.name = f"{JOB_NAME_PREFIX}{job.metadata.name}"
 
         if not job.metadata.namespace:
             hook_namespace = self.hook.get_namespace()
diff --git 
a/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_job.py 
b/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_job.py
index 08a8f1ff370..978d9fb877b 100644
--- a/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_job.py
+++ b/providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_job.py
@@ -481,7 +481,7 @@ class TestKubernetesJobOperator:
         job = k.build_job_request_obj({})
         assert (
             re.match(
-                r"job-a{71}-[a-z0-9]{8}",
+                r"job-a{50}-[a-z0-9]{8}",
                 job.metadata.name,
             )
             is not None

Reply via email to