Skip to content

Commit

Permalink
Fix: Parse image name and version from env
Browse files Browse the repository at this point in the history
Fixes #1756
  • Loading branch information
Ogaday authored and seldondev committed Apr 28, 2020
1 parent df61cac commit 151d27d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
4 changes: 3 additions & 1 deletion python/seldon_core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing import Union, List, Dict

from seldon_core.metrics import split_image_tag


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -156,7 +158,7 @@ def validate_model_metadata(data: Dict) -> Dict:
a correct type for specific components.
"""
if MODEL_IMAGE is not None:
image_name, image_version = MODEL_IMAGE.split(":")
image_name, image_version = split_image_tag(MODEL_IMAGE)
else:
image_name, image_version = "", ""
name = data.get("name", image_name)
Expand Down
22 changes: 20 additions & 2 deletions python/seldon_core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import numpy as np

from typing import List, Dict
from typing import List, Dict, Tuple
import logging
import json
import os
Expand All @@ -34,9 +34,27 @@
# This sets the bins spread logarithmically between 0.001 and 30
BINS = [0] + list(np.logspace(-3, np.log10(30), 50)) + [np.inf]


def split_image_tag(tag: str) -> Tuple[str]:
"""
Extract image name and version from an image tag.
Parameters
----------
tag
Fully qualified docker image tag. Eg. seldonio/sklearn-iris:0.1
Returns
-------
Image name, image version tuple
"""
*name_parts, version = tag.split(':')
return ':'.join(name_parts), version


# Development placeholder
image = os.environ.get(ENV_MODEL_IMAGE, f"{NONIMPLEMENTED_MSG}:{NONIMPLEMENTED_MSG}")
image_name, image_version = image.split(":")
image_name, image_version = split_image_tag(image)
predictor_version = json.loads(os.environ.get(ENV_PREDICTOR_LABELS, "{}")).get(
"version", f"{NONIMPLEMENTED_MSG}"
)
Expand Down
35 changes: 34 additions & 1 deletion python/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import pytest

from unittest.mock import patch

from seldon_core.metrics import SeldonMetrics
from seldon_core.wrapper import get_rest_microservice
from seldon_core.metadata import (
Expand Down Expand Up @@ -96,7 +98,38 @@ def test_validate_model_metadata():
"inputs": [{"name": "input", "datatype": "BYTES", "shape": [1]}],
"outputs": [{"name": "output", "datatype": "BYTES", "shape": [1]}],
}
assert meta == validate_model_metadata(meta)
with patch(
'seldon_core.metadata.MODEL_IMAGE', None
):
assert meta == validate_model_metadata(meta)


def test_validate_model_metadata_with_env():
meta = {
"name": "my-model-name",
"versions": ["model-version"],
"platform": "model-platform",
"inputs": [{"name": "input", "datatype": "BYTES", "shape": [1]}],
"outputs": [{"name": "output", "datatype": "BYTES", "shape": [1]}],
}
with patch(
'seldon_core.metadata.MODEL_IMAGE', 'seldonio/sklearn-iris:0.1'
):
assert meta == validate_model_metadata(meta)


def test_validate_model_metadata_with_colon_in_env():
meta = {
"name": "my-model-name",
"versions": ["model-version"],
"platform": "model-platform",
"inputs": [{"name": "input", "datatype": "BYTES", "shape": [1]}],
"outputs": [{"name": "output", "datatype": "BYTES", "shape": [1]}],
}
with patch(
'seldon_core.metadata.MODEL_IMAGE', 'localhost:32000/sklearn-iris:0.1'
):
assert meta == validate_model_metadata(meta)


@pytest.mark.parametrize("invalid_versions", ["v1", [1], "[v]", "[1]", 1, 1.1])
Expand Down
19 changes: 19 additions & 0 deletions python/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,32 @@
create_counter,
create_gauge,
create_timer,
split_image_tag,
validate_metrics,
COUNTER,
BINS,
)
from seldon_core.user_model import client_custom_metrics


def test_split_image_tag():
image = 'seldonio/sklearn-iris:0.1'
expected_name = 'seldonio/sklearn-iris'
expected_version = '0.1'
name, version = split_image_tag(image)
assert name == expected_name
assert version == expected_version


def test_split_image_tag_with_colon():
image = 'localhost:32000/sklearn-iris:0.1'
expected_name = 'localhost:32000/sklearn-iris'
expected_version = '0.1'
name, version = split_image_tag(image)
assert name == expected_name
assert version == expected_version


def test_create_counter():
v = create_counter("k", 1)
assert v["type"] == "COUNTER"
Expand Down

0 comments on commit 151d27d

Please sign in to comment.