Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions airflow/kubernetes/kubernetes_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ def _strip_unsafe_kubernetes_special_chars(string: str) -> str:
"""
Kubernetes only supports lowercase alphanumeric characters, "-" and "." in
the pod name.
However, there are special rules about how "-" and "." can be used so let's
only keep
alphanumeric chars see here for detail:
See here for detail:
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/

:param string: The requested Pod name
:return: Pod name stripped of any unsafe characters
"""
return ''.join(ch.lower() for ch in list(string) if ch.isalnum())
return ''.join(ch.lower() for ch in list(string) if ch.isalnum() or ch in ['-'])
Copy link
Member

Choose a reason for hiding this comment

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

It’s probably easier to do a re.sub instead.

Copy link
Author

Choose a reason for hiding this comment

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

Can you elaborate more on why using an additional module would be more effective? I am fairly new to Python, so I am eager for knowledge.

Copy link
Member

Choose a reason for hiding this comment

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

Not many reasons TBH, just it’s probably easier to understand IMO.

return re.sub(r"[^-a-z0-9]+", string.lower())

It may also be easier to implement additional logic, such as avoiding - in the beginning of the string (which the logic you implement right now does not do).

Copy link
Member

Choose a reason for hiding this comment

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

if we are to use regex, we should use a precompiled regex object. If we are not going to use regex here, then i think we should just do the comparison with ch == '-' without creating a new list in memory.



def create_pod_id(dag_id: str, task_id: str) -> str:
Expand All @@ -52,7 +50,7 @@ def create_pod_id(dag_id: str, task_id: str) -> str:
"""
safe_dag_id = _strip_unsafe_kubernetes_special_chars(dag_id)
safe_task_id = _strip_unsafe_kubernetes_special_chars(task_id)
return safe_dag_id + safe_task_id
return f"{safe_dag_id}-{safe_task_id}"


def annotations_to_key(annotations: Dict[str, str]) -> Optional[TaskInstanceKey]:
Expand Down