Skip to content

Commit

Permalink
Bugfix: Invalid name when trimmed pod_id ends with hyphen in ``Kube…
Browse files Browse the repository at this point in the history
…rnetesPodOperator`` (apache#15443)

When a pod name is more than `MAX_LABEL_LEN` (63 characters), it is trimmed to 63 chars

https://github.com/apache/airflow/blob/8711f90ab820ed420ef317b931e933a2062c891f/airflow/kubernetes/pod_generator.py#L470-L472

and we add a safe UUID to the `pod_id` joined by a dot `.`. However the regex
for Kubernetes name does not allow `-` followed by a `.`.

Valid Regex:

```
^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
```

This commit strips any hypens at the end of the trimmed `pod_id`.

(cherry picked from commit 130f9e3)
  • Loading branch information
kaxil committed Apr 26, 2021
1 parent 9a913fe commit cdfe3b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion airflow/kubernetes/pod_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ def make_unique_pod_id(pod_id: str) -> str:

safe_uuid = uuid.uuid4().hex # safe uuid will always be less than 63 chars
trimmed_pod_id = pod_id[:MAX_LABEL_LEN]
safe_pod_id = f"{trimmed_pod_id}.{safe_uuid}"

# 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}"

return safe_pod_id

Expand Down
19 changes: 19 additions & 0 deletions tests/kubernetes/test_pod_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import os
import re
import sys
import unittest
import uuid
Expand Down Expand Up @@ -656,6 +657,24 @@ def test_pod_name_confirm_to_max_length(self, _, pod_id):
assert len(parts[0]) <= 63
assert len(parts[1]) <= 63

@parameterized.expand(
(
("pod-name-with-hyphen-", "pod-name-with-hyphen"),
("pod-name-with-double-hyphen--", "pod-name-with-double-hyphen"),
("pod0-name", "pod0-name"),
("simple", "simple"),
)
)
def test_pod_name_is_valid(self, pod_id, expected_starts_with):
name = PodGenerator.make_unique_pod_id(pod_id)

regex = r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$"
assert (
len(name) <= 253 and all(ch.lower() == ch for ch in name) and re.match(regex, name)
), "pod_id is invalid - fails allowed regex check"

assert name.rsplit(".")[0] == expected_starts_with

def test_deserialize_model_string(self):
fixture = """
apiVersion: v1
Expand Down

0 comments on commit cdfe3b0

Please sign in to comment.