Skip to content

Commit

Permalink
fix: improve error message (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanFM committed Feb 8, 2022
1 parent d2dd11a commit f1a3bb9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions jina/excepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ class DockerVersionError(SystemError, BaseJinaExeception):

class DaemonInvalidDockerfile(FileNotFoundError, BaseJinaExeception):
"""Raised when invalid dockerfile is passed to JinaD"""


class NoContainerizedError(Exception, BaseJinaExeception):
""" Raised when trying to use non-containerized Executor in K8s or Docker Compose"""
8 changes: 7 additions & 1 deletion jina/orchestrate/deployments/config/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from typing import Dict, Union, List, Optional, Tuple

from jina import __default_executor__
from jina.excepts import NoContainerizedError
from jina.enums import PodRoleType
from jina.orchestrate.deployments.config.helper import (
get_image_name,
to_compatible_name,
get_base_executor_version,
construct_runtime_container_args,
validate_uses,
)
from jina.orchestrate.deployments import BaseDeployment

Expand Down Expand Up @@ -131,7 +133,6 @@ def get_runtime_config(
)

env = cargs.env

image_name = self._get_image_name(cargs.uses)
container_args = self._get_container_args(cargs)
config = {
Expand All @@ -149,6 +150,11 @@ def __init__(
args: Union['Namespace', Dict],
deployments_addresses: Optional[Dict[str, List[str]]] = None,
):
if not validate_uses(args.uses):
raise NoContainerizedError(
f'Executor "{args.uses}" is not valid to be used in docker-compose. '
'You need to use a containerized Executor. You may check `jina hub --help` to see how Jina Hub can help you building containerized Executors.'
)
self.deployments_addresses = deployments_addresses
self.head_service = None
self.uses_before_service = None
Expand Down
18 changes: 18 additions & 0 deletions jina/orchestrate/deployments/config/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from jina.hubble.helper import parse_hub_uri
from jina.hubble.hubio import HubIO
from jina.enums import PodRoleType
from jina import __default_executor__


def get_image_name(uses: str) -> str:
Expand Down Expand Up @@ -98,3 +99,20 @@ def construct_runtime_container_args(cargs, uses_metas, uses_with, pod_type):
container_args.extend(['--uses-with', json.dumps(uses_with)])
container_args.append('--native')
return container_args


def validate_uses(uses: str):
"""Validate uses argument
:param uses: uses argument
:return: boolean indicating whether is a valid uses to be used in K8s or docker compose
"""
if uses == __default_executor__ or uses.startswith('docker://'):
return True

try:
scheme, _, _, _ = parse_hub_uri(uses)
if scheme in {'jinahub+docker', 'jinahub+sandbox'}:
return True
except ValueError:
return False
7 changes: 7 additions & 0 deletions jina/orchestrate/deployments/config/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from jina import __default_executor__
from jina.enums import PodRoleType
from jina.excepts import NoContainerizedError
from jina.orchestrate.deployments.config.k8slib import kubernetes_deployment
from jina.orchestrate.deployments.config.helper import (
get_image_name,
to_compatible_name,
get_base_executor_version,
construct_runtime_container_args,
validate_uses,
)
from jina.serve.networking import K8sGrpcConnectionPool
from jina.orchestrate.deployments import BaseDeployment
Expand Down Expand Up @@ -212,6 +214,11 @@ def __init__(
):
# External Deployments should be ignored in a K8s based Flow
assert not (hasattr(args, 'external') and args.external)
if not validate_uses(args.uses):
raise NoContainerizedError(
f'Executor "{args.uses}" is not valid to be used in K8s. '
'You need to use a containerized Executor. You may check `jina hub --help` to see how Jina Hub can help you building containerized Executors.'
)
self.k8s_namespace = k8s_namespace
self.k8s_connection_pool = k8s_connection_pool
self.k8s_deployments_addresses = k8s_deployments_addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,11 @@ def test_flow_to_docker_compose_yaml(tmpdir, protocol):
assert '--connection-list' not in executor2_uses_after_args
assert '--uses-before' not in executor2_uses_after_args
assert '--uses-after' not in executor2_uses_after_args


def test_raise_exception_invalid_executor():
from jina.excepts import NoContainerizedError

with pytest.raises(NoContainerizedError):
f = Flow().add(uses='A')
f.to_docker_compose_yaml()
Original file line number Diff line number Diff line change
Expand Up @@ -619,5 +619,14 @@ def test_flow_to_k8s_yaml_external_pod(tmpdir, k8s_connection_pool, has_external
assert '--deployments-addresses' in gateway_args
assert (
gateway_args[gateway_args.index('--deployments-addresses') + 1]
== '{"executor0": ["executor0-head-0.test-flow-ns.svc:8081"], "external_executor": ["external-executor-head-0.test-flow-ns.svc:8081"]}'
== '{"executor0": ["executor0-head-0.test-flow-ns.svc:8081"], "external_executor": ['
'"external-executor-head-0.test-flow-ns.svc:8081"]}'
)


def test_raise_exception_invalid_executor(tmpdir):
from jina.excepts import NoContainerizedError

with pytest.raises(NoContainerizedError):
f = Flow().add(uses='A')
f.to_k8s_yaml(str(tmpdir))

0 comments on commit f1a3bb9

Please sign in to comment.