From 085bfc3f93c5e9cca58ab17d7469dfe7ee58069a Mon Sep 17 00:00:00 2001 From: Kaxil Naik Date: Tue, 20 Apr 2021 11:23:45 +0100 Subject: [PATCH] Further fix trimmed `pod_id` for `KubernetesPodOperator` (#15445) Missed a case in (#15443) where `.` can be followed by another `.`. (cherry picked from commit 1e66ce8c5861ac25274a113a509919e11d46038b) --- airflow/kubernetes/pod_generator.py | 8 +++----- tests/kubernetes/test_pod_generator.py | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) 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):