Skip to content

Commit 7cad782

Browse files
authored
🐛 trim bad characters from the start of k8s labels (#104)
1 parent 5c0296e commit 7cad782

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

dagster_ray/kuberay/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ def normalize_k8s_label_values(labels: dict[str, str]) -> dict[str, str]:
88

99
cleanup_regex = re.compile(r"[^a-zA-Z0-9-_.]+")
1010

11+
banned_starting_characters = ["-", "_", "."]
12+
1113
for key, value in labels.items():
12-
# daniel~!@my.domain -> daniel-my-domain
13-
labels[key] = cleanup_regex.sub("", value.replace("@", "-").replace(".", "-"))[:63]
14+
# -daniel~!@my.domain -> daniel-my-domain
15+
with_maybe_bad_start = cleanup_regex.sub("", value.replace("@", "-").replace(".", "-"))
16+
while with_maybe_bad_start and with_maybe_bad_start[0] in banned_starting_characters:
17+
with_maybe_bad_start = with_maybe_bad_start[1:]
18+
labels[key] = with_maybe_bad_start[:63]
1419

1520
return labels

tests/kuberay/test_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def test_normalize_k8s_label_values():
1010
"user-dirty": "daniel!`~@my.org",
1111
"alphanumeric": "abc123",
1212
"long": 64 * "a",
13+
"badstart": "-foo",
14+
"badstart_after_initial_replace": "@foo",
1315
}
1416
) == {
1517
"foo": "bar",
@@ -18,4 +20,6 @@ def test_normalize_k8s_label_values():
1820
"user-dirty": "daniel-my-org",
1921
"alphanumeric": "abc123",
2022
"long": 63 * "a",
23+
"badstart": "foo",
24+
"badstart_after_initial_replace": "foo",
2125
}

0 commit comments

Comments
 (0)