Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Invalid name when trimmed pod_id ends with hyphen in ``Kube… #15443

Merged
merged 1 commit into from Apr 19, 2021

Conversation

kaxil
Copy link
Member

@kaxil kaxil commented Apr 19, 2021

…rnetesPodOperator``

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

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

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.


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

…rnetesPodOperator``

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`.
@boring-cyborg boring-cyborg bot added the provider:cncf-kubernetes Kubernetes provider related issues label Apr 19, 2021
@kaxil kaxil added this to the Airflow 2.0.3 milestone Apr 19, 2021
Copy link
Member

@houqp houqp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

On top of this, I wonder if we should keep the name sanitizing logic consistent between executor and operator by passing the task name through _strip_unsafe_kubernetes_special_chars instead of using re.sub(r'[^a-z0-9.-]+', '-', name.lower()) in KubernetesPodOperator._set_name.

@github-actions
Copy link

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest master at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label Apr 19, 2021
@kaxil kaxil merged commit 130f9e3 into apache:master Apr 19, 2021
@kaxil kaxil deleted the fix-pod-name branch April 19, 2021 23:02
("pod-name-with-hyphen-", "pod-name-with-hyphen"),
("pod-name-with-double-hyphen--", "pod-name-with-double-hyphen"),
("pod0-name", "pod0-name"),
("simple", "simple"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
("simple", "simple"),
("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"),

Comment on lines +472 to +475

# 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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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}"
trimmed_pod_id = pod_id[:MAX_LABEL_LEN].rstrip('-') # Strip trailing '-' as it cant be followed by '.'
safe_pod_id = f"{trimmed_pod_id}.{safe_uuid}"

Maybe? Either way, your comment is wrong as it only removes trailing dashes, and occurrences has a typo.

What about the '..' case (and more, maybe trimming until the last char is a-z0-9)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #15445 to address it

@jedcunningham
Copy link
Member

Oops, too slow, guess we can address these in a follow up PR.

kaxil added a commit to astronomer/airflow that referenced this pull request Apr 20, 2021
Missed a case in (apache#15443) where `.` can be followed by another `.`.
kaxil added a commit that referenced this pull request Apr 20, 2021
Missed a case in (#15443) where `.` can be followed by another `.`.
kaxil added a commit to astronomer/airflow that referenced this pull request Apr 20, 2021
…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)
kaxil added a commit to astronomer/airflow that referenced this pull request Apr 20, 2021
Missed a case in (apache#15443) where `.` can be followed by another `.`.

(cherry picked from commit 1e66ce8)
kaxil added a commit to astronomer/airflow that referenced this pull request Apr 26, 2021
…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)
kaxil added a commit to astronomer/airflow that referenced this pull request Apr 26, 2021
Missed a case in (apache#15443) where `.` can be followed by another `.`.

(cherry picked from commit 1e66ce8)
potiuk pushed a commit that referenced this pull request May 9, 2021
…rnetesPodOperator`` (#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)
potiuk pushed a commit that referenced this pull request May 9, 2021
Missed a case in (#15443) where `.` can be followed by another `.`.

(cherry picked from commit 1e66ce8)
@ashb ashb removed this from the Airflow 2.0.3 milestone May 18, 2021
@ashb ashb added this to the Airflow 2.1 milestone May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full tests needed We need to run full set of tests for this PR to merge provider:cncf-kubernetes Kubernetes provider related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants