[docs]defget_image_name(uses:str)->str:"""The image can be provided in different formats by the user. This function converts it to an image name which can be understood by k8s. It uses the Hub api to get the image name and the latest tag on Docker Hub. If you don't want to rebuild image on Jina Hub, you can set `JINA_HUB_NO_IMAGE_REBUILD` environment variable. :param uses: image name :return: normalized image name """try:rebuild_image='JINA_HUB_NO_IMAGE_REBUILD'notinos.environscheme,name,tag,secret=parse_hub_uri(uses)meta_data,_=HubIO.fetch_meta(name,tag,secret=secret,rebuild_image=rebuild_image,force=True)image_name=meta_data.image_namereturnimage_nameexceptException:ifuses.startswith('docker'):# docker:// is a valid requirement and user may want to put its own imagereturnuses.replace('docker://','')raise
[docs]defto_compatible_name(name:str)->str:"""Converts the deployment name to a valid name for K8s and docker compose. :param name: name of the deployment :return: compatible name """returnname.replace('/','-').replace('_','-').lower()
[docs]defget_base_executor_version():""" Get the version of jina to be used :return: the version tag """importrequeststry:url='https://registry.hub.docker.com/v1/repositories/jinaai/jina/tags'tags=requests.get(url).json()name_set={tag['name']fortagintags}if__version__inname_set:return__version__else:return'master'except:return'master'
[docs]defconstruct_runtime_container_args(cargs,uses_metas,uses_with,pod_type):""" Construct a set of Namespace arguments into a list of arguments to pass to a container entrypoint :param cargs: The namespace arguments :param uses_metas: The uses_metas to override :param uses_with: The uses_with to override :param pod_type: The pod_type :return: Arguments to pass to container """importjsonfromjina.helperimportArgNamespacefromjina.parsersimportset_pod_parsertaboo={'uses_with','uses_metas','volumes','uses_before','uses_after','workspace_id','upload_files','noblock_on_start','env',}ifpod_type==PodRoleType.HEAD:taboo.add('uses')taboo.add('workspace')ifpod_typein{PodRoleType.WORKER,PodRoleType.GATEWAY}:taboo.add('polling')non_defaults=ArgNamespace.get_non_defaults_args(cargs,set_pod_parser(),taboo=taboo,)_args=ArgNamespace.kwargs2list(non_defaults)container_args=['executor']+_argsifuses_metasisnotNone:container_args.extend(['--uses-metas',json.dumps(uses_metas)])ifuses_withisnotNone:container_args.extend(['--uses-with',json.dumps(uses_with)])container_args.append('--native')returncontainer_args
[docs]defvalidate_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 """ifuses==__default_executor__oruses.startswith('docker://'):returnTruetry:scheme,_,_,_=parse_hub_uri(uses)ifschemein{'jinahub+docker','jinahub+sandbox'}:returnTrueexceptValueError:returnFalse