@@ -2256,7 +2256,14 @@ def _k8s_build_service_manifest(
22562256
22572257
22582258async def _k8s_resolve_exapp_upstream (app_name : str ) -> tuple [str , int ] | None :
2259- """Look up K8s Service for an ExApp, return (host, port) or None."""
2259+ """Look up K8s Service for an ExApp, return (host, port) or None.
2260+
2261+ Returns None when K8s is disabled or no Service exists (Docker ExApp).
2262+ Raises web.HTTPServiceUnavailable when the K8s API returned an error
2263+ (e.g. 403 token expired, 500 server error) so the caller knows not to
2264+ cache the result with stale NC-provided host/port.
2265+ Connection-level errors already raise via _k8s_request.
2266+ """
22602267 if not K8S_ENABLED or not K8S_API_SERVER or not _get_k8s_token ():
22612268 return None
22622269
@@ -2266,6 +2273,7 @@ async def _k8s_resolve_exapp_upstream(app_name: str) -> tuple[str, int] | None:
22662273 return None
22672274
22682275 svc : dict [str , Any ] | None = None
2276+ got_k8s_error = False
22692277
22702278 # Try the base Service name first (single-role / legacy ExApps).
22712279 service_name = exapp .exapp_k8s_name
@@ -2275,6 +2283,8 @@ async def _k8s_resolve_exapp_upstream(app_name: str) -> tuple[str, int] | None:
22752283 )
22762284 if status == 200 and isinstance (data , dict ):
22772285 svc = data
2286+ elif status != 404 :
2287+ got_k8s_error = True
22782288
22792289 # Fallback: search for any Service labelled for this ExApp (multi-role).
22802290 if svc is None :
@@ -2290,8 +2300,19 @@ async def _k8s_resolve_exapp_upstream(app_name: str) -> tuple[str, int] | None:
22902300 if items :
22912301 svc = items [0 ]
22922302 service_name = (svc .get ("metadata" ) or {}).get ("name" , service_name )
2303+ elif status != 200 :
2304+ got_k8s_error = True
22932305
22942306 if svc is None :
2307+ if got_k8s_error :
2308+ LOGGER .warning (
2309+ "K8s API returned error during Service lookup for '%s'; "
2310+ "will not cache stale record" ,
2311+ app_name ,
2312+ )
2313+ raise web .HTTPServiceUnavailable (
2314+ text = f"K8s API error during Service lookup for '{ app_name } '"
2315+ )
22952316 return None
22962317
22972318 svc_type = (svc .get ("spec" ) or {}).get ("type" , "ClusterIP" )
0 commit comments