Skip to content

Commit

Permalink
serveutils: Get InferenceService CR fields safely
Browse files Browse the repository at this point in the history
Add some KeyError exception handling when getting fields of an
InferenceService CR.

Support both v1alpha3 and v1beta1 when quering for the predictor's
host/url.

Improve the docstrings of some functions.

Signed-off-by: Stefano Fioravanzo <[email protected]>
Reviewed-by: Ilias Katsakioris <[email protected]>
  • Loading branch information
StefanoFioravanzo authored and elikatsis committed May 19, 2021
1 parent 0c70ef2 commit fab255c
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions backend/kale/common/serveutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,17 @@ def _add_owner_references(infs_name: str, pvc_name: str):


def monitor_inference_service(name: str):
"""Wait for an InferenceService to become ready."""
"""Waits for an InferenceService to become ready.
An InferenceService is considered ready when two conditions are met:
1. the ``status.conditions`` field of the CR contains a condition of
type ``Ready`` with a ``True`` status.
2. The CR defines a valid host/url for the (default) predictor.
Args:
name (str): Name of the KFServing InferenceService
"""
host = None

def _is_ready(inference_service):
Expand All @@ -468,7 +478,13 @@ def _is_ready(inference_service):
return

if _is_ready(inf):
host = inf["status"]["default"]["predictor"]["host"]
try:
if inf["status"].get("default"): # v1alpha3
host = inf["status"]["default"]["predictor"]["host"]
elif inf["status"].get("components"): # v1beta1
host = inf["status"]["components"]["predictor"]["url"]
except KeyError:
pass
time.sleep(3)

log.info("InferenceService '%s' is ready.", name)
Expand All @@ -483,15 +499,49 @@ def get_inference_service(name: str):


def get_inference_service_host(name: str) -> str:
"""Get the hostname of the InferenceService."""
"""Get the hostname of the InferenceService.
Args:
name (str): Name of the KFServing InferenceService
Returns:
str: The status.url field of the InferenceService CR. Empty string
if ``url`` is not defined.
"""
inference_service = get_inference_service(name)
url = inference_service["status"]["url"]
try:
url = inference_service["status"]["url"]
except KeyError:
log.error("Could not find url for InferenceService '%s'", name)
return ""
if url.startswith("http://"):
url = url[len("http://"):]
return url.replace("example.com", "svc.cluster.local")


def get_inference_service_default_predictor_host(name: str) -> str:
"""Get the hostname of the default predictor."""
inference_service = get_inference_service(name)
return inference_service["status"]["default"]["predictor"]["host"]
"""Get the hostname of the default predictor.
This function supports both v1alpha3 and v1beta1 InferenceService CRDs.
The predictor's url/host is defined in two different fields:
- ``v1alpha3``: ``status.default.predictor.host``
- ``v1beta1``: ``status.components.predictor.url``
Args:
name (str): Name of the KFServing InferenceService
Returns:
str: The host/url field of the (default) predictor. Empty string if
it cannot be determined.
"""
inf = get_inference_service(name)
try:
if inf["status"].get("default"): # v1alpha3
return inf["status"]["default"]["predictor"]["host"]
elif inf["status"].get("components"): # v1beta1
return inf["status"]["components"]["predictor"]["url"]
except KeyError:
log.error("Could not find the predictor's url for InferenceService"
" '%s'", name)
return ""

0 comments on commit fab255c

Please sign in to comment.