uranusjr commented on code in PR #27524:
URL: https://github.com/apache/airflow/pull/27524#discussion_r1015177027
##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -62,6 +62,23 @@
from airflow.utils.context import Context
+def _task_id_to_pod_name(val):
+ val = val.lower()
+ first = val[0]
+ if not re.match(r"[a-z0-9]", first):
+ val = "0" + val
+ last = val[-1]
+ if not re.match(r"[a-z0-9]", last):
+ val = val + "0"
+ val = re.sub(r"[^a-z0-9\-.]", "-", val)
+ if len(val) > 253:
+ raise ValueError(
+ f"Pod name {val} is longer than 253 characters. "
+ "See
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/."
+ )
+ return val
Review Comment:
```suggestion
def _task_id_to_pod_name(val: str) -> str:
val = val.lower()
if not re.match(r"[a-z0-9]", val[0]):
val = f"0{val}"
if not re.match(r"[a-z0-9]", val[-1]):
val = f"{val}0"
val = re.sub(r"[^a-z0-9\-.]", "-", val)
if len(val) > 253:
raise ValueError(
f"Pod name {val} is longer than 253 characters. "
"See
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/."
)
return val
```
I wonder if it’d be faster to use `^` and `$` instead of `[0]` and `[-1]`,
but that’s probably too marginal anyway.
It’s probably worthwhile to add a docstring to this function explaining what
it does and expects to receive (must be a non-empty string).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]