Skip to content

Commit

Permalink
Fix label sanitization for strings that end in a period (#8151)
Browse files Browse the repository at this point in the history
Summary:
(or have a period at the end  when they are truncated)

Test Plan: BK, verify new test was failing before
  • Loading branch information
gibsondan committed Jun 1, 2022
1 parent 64bd480 commit 8fc3838
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python_modules/libraries/dagster-k8s/dagster_k8s/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def sanitize_k8s_label(label_name: str):
# Truncate too long label values to fit into 63-characters limit and avoid invalid characters.
# https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
label_name = label_name[:63]
return re.sub(r"[^a-zA-Z0-9\-_\.]", "-", label_name).strip("-").strip("_")
return re.sub(r"[^a-zA-Z0-9\-_\.]", "-", label_name).strip("-").strip("_").strip(".")


def retrieve_pod_logs(pod_name, namespace):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest
from dagster_k8s import DagsterK8sJobConfig, construct_dagster_k8s_job
from dagster_k8s.job import (
Expand Down Expand Up @@ -681,3 +683,30 @@ def test_sanitize_labels():

assert job["metadata"]["labels"]["dagster/op"] == "get_f-o.o-bar-0"
assert job["metadata"]["labels"]["my_label"] == "WhatsUP"


# Taken from the k8s error message when a label is invalid
K8s_LABEL_REGEX = r"(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?"


def test_sanitize_labels_regex():

assert re.fullmatch(K8s_LABEL_REGEX, sanitize_k8s_label("normal-string"))

assert not re.fullmatch(
K8s_LABEL_REGEX,
"string-with-period.",
)

assert re.fullmatch(
K8s_LABEL_REGEX,
sanitize_k8s_label("string-with-period."),
)

# string that happens to end with a period after being truncated to 63 characters
assert re.fullmatch(
K8s_LABEL_REGEX,
sanitize_k8s_label(
"data_pipe_graph_abcdefghi_jklmn.raw_data_graph_abcdefghi_jklmn.opqrstuvwxyz"
),
)

0 comments on commit 8fc3838

Please sign in to comment.