-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
407 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# @Author: José Sánchez-Gallego (gallegoj@uw.edu) | ||
# @Date: 2024-07-25 | ||
# @Filename: actors.py | ||
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause) | ||
|
||
from __future__ import annotations | ||
|
||
from fastapi import APIRouter, Request | ||
from pydantic import BaseModel | ||
|
||
from lvmapi import config | ||
from lvmapi.tasks import restart_kubernetes_deployment_task | ||
from lvmapi.tools.rabbitmq import ping_actors | ||
|
||
|
||
class HealthResponse(BaseModel): | ||
"""Health response for an actor.""" | ||
|
||
actor: str | ||
deployment_name: str | ||
is_deployed: bool | ||
ping: bool | ||
|
||
|
||
router = APIRouter(prefix="/actors", tags=["actors"]) | ||
|
||
|
||
@router.get("/") | ||
async def get_actors_route() -> list[str]: | ||
"""Returns a list of actors.""" | ||
|
||
return config["actors"]["list"] | ||
|
||
|
||
@router.get("/health") | ||
async def get_actor_health(request: Request) -> list[HealthResponse]: | ||
"""Returns the health of all actors.""" | ||
|
||
actors = config["actors"]["list"] | ||
health: list[HealthResponse] = [] | ||
|
||
deployments = request.app.state.kubernetes.list_deployments() | ||
ping_response = await ping_actors(actors=actors) | ||
|
||
for actor in actors: | ||
deployment_name = config["actors.actor_to_deployment"][actor] | ||
ping = ping_response.get(actor, False) | ||
is_deployed = deployment_name in deployments | ||
health.append( | ||
HealthResponse( | ||
actor=actor, | ||
deployment_name=deployment_name, | ||
is_deployed=is_deployed, | ||
ping=ping, | ||
) | ||
) | ||
|
||
return health | ||
|
||
|
||
@router.get("/ping") | ||
async def ping_route(actors: list[str] | None = None) -> dict[str, bool]: | ||
"""Pings a list of actors.""" | ||
|
||
return await ping_actors(actors=actors) | ||
|
||
|
||
@router.get("/restart/{actor}") | ||
async def restart_actor_route(actor: str) -> str: | ||
"""Restarts an actor. Scheduled as a task and returns the task ID""" | ||
|
||
deployment = config["actors.actor_to_deployment"][actor] | ||
if deployment is None: | ||
raise ValueError(f"Actor {actor} does not have a deployment.") | ||
|
||
task = await restart_kubernetes_deployment_task.kiq(deployment) | ||
return task.task_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# @Author: José Sánchez-Gallego (gallegoj@uw.edu) | ||
# @Date: 2024-07-25 | ||
# @Filename: kubernetes.py | ||
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause) | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from fastapi import APIRouter, Request | ||
|
||
from lvmapi.tasks import restart_kubernetes_deployment_task | ||
|
||
|
||
if TYPE_CHECKING: | ||
from lvmapi.tools.kubernetes import Kubernetes | ||
|
||
|
||
router = APIRouter(prefix="/kubernetes", tags=["kubernetes"]) | ||
|
||
|
||
@router.get("/deployments") | ||
@router.get("/deployments/list") | ||
async def list_deployments(request: Request) -> list[str]: | ||
"""Lists the deployments in all namespaces.""" | ||
|
||
kube: Kubernetes = request.app.state.kubernetes | ||
|
||
return kube.list_deployments() | ||
|
||
|
||
@router.get("/deployments/{deployment}/restart") | ||
async def restart_deployment(deployment: str) -> str: | ||
"""Restarts a deployment. Scheduled as a task (returns task ID).""" | ||
|
||
task = await restart_kubernetes_deployment_task.kiq(deployment) | ||
return task.task_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.