Skip to content

Commit

Permalink
chore(webserver): add a page to redirect to when there is a K8S error (
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala authored Mar 24, 2024
1 parent 4782790 commit 7fcd977
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spark_on_k8s/api/webserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from functools import wraps
from pathlib import Path

import httpx
from fastapi import APIRouter, WebSocket
from kubernetes_asyncio.client import ApiException
from starlette.background import BackgroundTask
from starlette.requests import Request # noqa: TCH002
from starlette.responses import HTMLResponse, StreamingResponse
Expand All @@ -21,7 +23,28 @@
)


def handle_k8s_errors(func):
@wraps(func)
async def wrapper(*args, request: Request, **kwargs):
try:
return await func(*args, request=request, **kwargs)
except ApiException as e:
title = f"{e.status} {e.reason}"
message = e.body
return templates.TemplateResponse(
"error.html",
{
"request": request,
"title": title,
"message": message,
},
)

return wrapper


@router.get("/ui/{path:path}")
@handle_k8s_errors
async def ui_reverse_proxy(request: Request):
path = request.url.path
path = path.replace(router.prefix + "/ui", "").lstrip("/")
Expand Down Expand Up @@ -51,6 +74,7 @@ async def ui_reverse_proxy(request: Request):


@router.get("/apps", response_class=HTMLResponse)
@handle_k8s_errors
async def apps(request: Request):
"""List spark apps in a namespace, and display them in a web page."""
namespace = request.query_params.get("namespace", APIConfiguration.SPARK_ON_K8S_API_DEFAULT_NAMESPACE)
Expand Down Expand Up @@ -78,6 +102,7 @@ async def app_logs_websocket(websocket: WebSocket, namespace: str, app_id: str,


@router.get("/logs/{namespace}/{app_id}", response_class=HTMLResponse)
@handle_k8s_errors
async def app_logs(request: Request, namespace: str, app_id: str):
"""Display logs of a spark app in a web page."""
tail = request.query_params.get("tail", -1)
Expand Down
15 changes: 15 additions & 0 deletions spark_on_k8s/api/webserver/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
<link href="{{ url_for('static', path='/apps.css') }}" rel="stylesheet">
</head>
<body>
<div class="error-container">
<h1>{{ title }}</h1>
<p>{{ message }}</p>
</div>
</body>
</html>

0 comments on commit 7fcd977

Please sign in to comment.