diff --git a/airflow/kubernetes/pod_generator.py b/airflow/kubernetes/pod_generator.py index dd268ebec2b62..6a37dd642ab20 100644 --- a/airflow/kubernetes/pod_generator.py +++ b/airflow/kubernetes/pod_generator.py @@ -467,12 +467,10 @@ def make_unique_pod_id(pod_id: str) -> str: return None safe_uuid = uuid.uuid4().hex # safe uuid will always be less than 63 chars - trimmed_pod_id = pod_id[:MAX_LABEL_LEN] - - # Since we use '.' as separator we need to remove all the occurences of '-' if any - # in the trimmed_pod_id as the regex does not allow '-' followed by '.'. - safe_pod_id = f"{trimmed_pod_id.rstrip('-')}.{safe_uuid}" + # Strip trailing '-' and '.' as they cant be followed by '.' + trimmed_pod_id = pod_id[:MAX_LABEL_LEN].rstrip('-.') + safe_pod_id = f"{trimmed_pod_id}.{safe_uuid}" return safe_pod_id diff --git a/tests/kubernetes/test_pod_generator.py b/tests/kubernetes/test_pod_generator.py index 02af18b00696d..ac33cd4b2eef9 100644 --- a/tests/kubernetes/test_pod_generator.py +++ b/tests/kubernetes/test_pod_generator.py @@ -663,6 +663,9 @@ def test_pod_name_confirm_to_max_length(self, _, pod_id): ("pod-name-with-double-hyphen--", "pod-name-with-double-hyphen"), ("pod0-name", "pod0-name"), ("simple", "simple"), + ("pod-name-with-dot.", "pod-name-with-dot"), + ("pod-name-with-double-dot..", "pod-name-with-double-dot"), + ("pod-name-with-hyphen-dot-.", "pod-name-with-hyphen-dot"), ) ) def test_pod_name_is_valid(self, pod_id, expected_starts_with):