Skip to content

Commit

Permalink
Changes based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dhpollack committed Mar 28, 2023
1 parent 7bba536 commit 6f1feef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
5 changes: 2 additions & 3 deletions metaflow/plugins/kubernetes/kubernetes_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ def echo(msg, stream="stderr", job_id=None, **kwargs):

# `labels` is a tuple of strings or a tuple with a single comma separated string
# convert it to a dict
labels = KubernetesDecorator.validate_kube_labels(
KubernetesDecorator.parse_kube_keyvalue_list(labels, False)
)
labels = KubernetesDecorator.parse_kube_keyvalue_list(labels, False)
KubernetesDecorator.validate_kube_labels(labels)

def _sync_metadata():
if ctx.obj.metadata.TYPE == "local":
Expand Down
19 changes: 9 additions & 10 deletions metaflow/plugins/kubernetes/kubernetes_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(self, attributes=None, statically_defined=False):
self.attributes["labels"] = self.parse_kube_keyvalue_list(
self.attributes["labels"].split(","), False
)
self.attributes["labels"] = self.validate_kube_labels(self.attributes["labels"])
self.validate_kube_labels(self.attributes["labels"])

if isinstance(self.attributes["node_selector"], str):
self.attributes["node_selector"] = self.parse_kube_keyvalue_list(
Expand Down Expand Up @@ -441,21 +441,20 @@ def parse_kube_keyvalue_list(items: List[str], requires_both: bool = True):
@staticmethod
def validate_kube_labels(
labels: Optional[Dict[str, Optional[str]]],
regex_match: str = r"^(([A-Za-z0-9][-A-Za-z0-9_.]{0,61})?[A-Za-z0-9])?$",
):
) -> bool:
def validate_label(s: Optional[str]):
regex_match: str = r"^(([A-Za-z0-9][-A-Za-z0-9_.]{0,61})?[A-Za-z0-9])?$"
if not s:
# allow empty label
return s
return True
if not re.search(regex_match, s):
# this is the same message kubernetes itself returns
raise Exception(
raise KubernetesException(
f'Invalid value: "{s}": a valid label must be an empty string or '
"consist of alphanumeric characters, '-', '_' or '.', and must "
"consist of alphanumeric characters, '-', '_' or '.', must "
"start and end with an alphanumeric character (e.g. 'MyValue', "
"or 'my_value', or '12345', regex used for validation is "
"'^(([A-Za-z0-9][-A-Za-z0-9_.]{0,61})?[A-Za-z0-9])?$'"
"or 'my_value', or '12345'), and be 63 characters or less"
)
return s
return True

return {k: validate_label(v) for k, v in labels.items()} if labels else labels
return all([validate_label(v) for v in labels.values()]) if labels else True
6 changes: 3 additions & 3 deletions test/unit/test_kubernetes_decorator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from metaflow.plugins.kubernetes.kubernetes import KubernetesException
from metaflow.plugins.kubernetes.kubernetes_decorator import KubernetesDecorator


Expand Down Expand Up @@ -37,8 +38,7 @@
],
)
def test_kubernetes_decorator_validate_kube_labels(labels):
cleaned_labels = KubernetesDecorator.validate_kube_labels(labels)
assert cleaned_labels == labels
assert KubernetesDecorator.validate_kube_labels(labels)


@pytest.mark.parametrize(
Expand All @@ -64,5 +64,5 @@ def test_kubernetes_decorator_validate_kube_labels(labels):
)
def test_kubernetes_decorator_validate_kube_labels_fail(labels):
"""Fail if label contains invalid characters or is too long"""
with pytest.raises(Exception):
with pytest.raises(KubernetesException):
KubernetesDecorator.validate_kube_labels(labels)

0 comments on commit 6f1feef

Please sign in to comment.