Skip to content

Commit cc5ffbe

Browse files
committed
Address a corner case with name truncation
1 parent f925f68 commit cc5ffbe

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/cloudai/_core/json_gen_strategy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ def sanitize_k8s_job_name(self, job_name: str) -> str:
5555
sanitized_name = job_name.lower()
5656
sanitized_name = re.sub(r"[^a-z0-9-]", "-", sanitized_name)
5757
sanitized_name = re.sub(r"^[^a-z0-9]+", "", sanitized_name)
58+
sanitized_name = sanitized_name[:253]
5859
sanitized_name = re.sub(r"[^a-z0-9]+$", "", sanitized_name)
59-
final_name = sanitized_name[:253]
6060

61-
if not final_name:
61+
if not sanitized_name:
6262
raise ValueError(f"'{job_name}' cannot be sanitized to a valid Kubernetes job name.")
6363

64-
return final_name
64+
return sanitized_name
6565

6666
def store_test_run(self) -> None:
6767
from cloudai.models.scenario import TestRunDetails

tests/json_gen_strategy/test_common_kubernetes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def gen_json(self) -> dict:
3535
("name@with#special$chars", "name-with-special-chars"),
3636
("NameWithUpperCase", "namewithuppercase"),
3737
("a" * 260, "a" * 253),
38+
("---leading-and-trailing---", "leading-and-trailing"),
39+
("a" * 250 + "-" * 3 + "b" * 10, "a" * 250), # ensure no trailing hyphens on truncation
3840
],
3941
)
4042
def test_job_name_sanitization(k8s_system: KubernetesSystem, base_tr: TestRun, tname: str, expected: str) -> None:

0 commit comments

Comments
 (0)