diff --git a/gateway/core.py b/gateway/core.py index 5f16ba3..7e79d7f 100644 --- a/gateway/core.py +++ b/gateway/core.py @@ -85,6 +85,7 @@ def route( form_params: list[str] | None = None, body_params: list[str] | None = None, response_model: any = None, # TODO: Make specific for pydantic models + response_class: any = None, # TODO: Make specific for pydantic response classes tags: list[str] = None, dependencies: Sequence[params.Depends] | None = None, summary: str | None = None, @@ -113,6 +114,8 @@ def route( Keys passed referencing body data parameters to be sent to downstream microservice response_model Response model of the forwarded request. Can be imported from other packages. + response_class + Response class of the forwarded request. Can be imported from other packages. tags : list[str] List of tags used to classify methods dependencies: Sequence[params.Depends] | None @@ -135,6 +138,7 @@ def route( path, status_code=status_code, response_model=response_model, + response_class=response_class, tags=tags, dependencies=dependencies, summary=summary, diff --git a/gateway/routers/k8s.py b/gateway/routers/k8s.py deleted file mode 100644 index 126b6cb..0000000 --- a/gateway/routers/k8s.py +++ /dev/null @@ -1,55 +0,0 @@ -"""EPs for the pod orchestrator.""" -import logging -from typing import Annotated - -import kubernetes.client -from fastapi import APIRouter, Path, Security - -from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass -from gateway.conf import gateway_settings - -k8s_router = APIRouter( - dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass)], - tags=["PodOrc"], - responses={404: {"description": "Not found"}}, -) -logger = logging.getLogger(__name__) - - -def initialize_k8s_api_conn(): # Convert to decorator for each EP? - """Create an API client for the K8s instance.""" - # K8s init - k8s_conf = kubernetes.client.Configuration() - k8s_conf.api_key["authorization"] = gateway_settings.K8S_API_KEY - k8s_conf.host = gateway_settings.PODORC_SERVICE_URL - kubernetes.client.Configuration.set_default(k8s_conf) - - api_client = kubernetes.client.CoreV1Api() - return api_client - - -@k8s_router.get("/namespaces", response_model=list[str]) -async def get_namespaces(): - """List available namespaces.""" - k8s_api = initialize_k8s_api_conn() - resp = k8s_api.list_namespace().to_dict() - ns = {namespace["metadata"]["name"] for namespace in resp["items"]} - return ns - - -@k8s_router.get("/pods/{namespace}") -async def get_k8s_pods_by_namespace(namespace: Annotated[str, Path(title="Namespace to query")], ): - """Get a list of k8s pods for a given namespace.""" - k8s_api = initialize_k8s_api_conn() - # TODO improve output and limit requested information - # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md - return k8s_api.list_namespaced_pod(namespace=namespace).to_dict() - - -@k8s_router.get("/svc/{namespace}") -async def get_k8s_svc_by_namespace(namespace: Annotated[str, Path(title="Namespace to query")], ): - """Get a list of k8s services for a given namespace.""" - k8s_api = initialize_k8s_api_conn() - # TODO improve output and limit requested information - # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md - return k8s_api.list_namespaced_service(namespace=namespace).to_dict() diff --git a/gateway/routers/podorc.py b/gateway/routers/podorc.py new file mode 100644 index 0000000..c4c383b --- /dev/null +++ b/gateway/routers/podorc.py @@ -0,0 +1,116 @@ +"""EPs for the pod orchestrator.""" +import logging +from typing import Annotated + +from fastapi import APIRouter, Path, Security +from fastapi.responses import JSONResponse +from starlette import status +from starlette.requests import Request +from starlette.responses import Response + +from gateway.auth import verify_idp_token, idp_oauth2_scheme_pass +from gateway.conf import gateway_settings +from gateway.core import route + +po_router = APIRouter( + dependencies=[Security(verify_idp_token), Security(idp_oauth2_scheme_pass)], + tags=["PodOrc"], + responses={404: {"description": "Not found"}}, +) +logger = logging.getLogger(__name__) + + +@route( + request_method=po_router.post, + path="/{analysis_id}", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def create_analysis( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Get the logs for a specific analysis run.""" + pass + + +@route( + request_method=po_router.get, + path="/{analysis_id}/logs", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def get_analysis_logs( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Get the logs for a specific analysis run.""" + pass + + +@route( + request_method=po_router.get, + path="/{analysis_id}/status", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def get_analysis_status( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Get the status for a specific analysis run.""" + pass + + +@route( + request_method=po_router.get, + path="/{analysis_id}/pods", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def get_analysis_pods( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Get the pods for a specific analysis run.""" + pass + + +@route( + request_method=po_router.put, + path="/{analysis_id}/stop", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def stop_analysis( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Stop a specific analysis run.""" + pass + + +@route( + request_method=po_router.delete, + path="/{analysis_id}/delete", + status_code=status.HTTP_200_OK, + service_url=gateway_settings.PODORC_SERVICE_URL, + response_class=JSONResponse, +) +async def delete_analysis( + request: Request, + response: Response, + analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")], +): + """Delete a specific analysis run.""" + pass diff --git a/gateway/server.py b/gateway/server.py index 1ec835f..7c53da7 100644 --- a/gateway/server.py +++ b/gateway/server.py @@ -16,9 +16,9 @@ from gateway.models import HealthCheck from gateway.models.conf import Token from gateway.routers.hub import hub_router -from gateway.routers.k8s import k8s_router from gateway.routers.kong import kong_router from gateway.routers.metadata import metadata_router +from gateway.routers.podorc import po_router from gateway.routers.results import results_router @@ -118,7 +118,7 @@ def get_token(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]) -> Tok app.include_router( - k8s_router, + po_router, ) app.include_router(