-
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.
Merge pull request #29 from jsickcodes/s3-healthcheck
Add a S3 check to healthcheck endpoint
- Loading branch information
Showing
2 changed files
with
39 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
"""Handler for the Kubernetes health check.""" | ||
|
||
from fastapi import APIRouter | ||
import httpx | ||
from fastapi import APIRouter, Depends | ||
from safir.dependencies.http_client import http_client_dependency | ||
from safir.dependencies.logger import logger_dependency | ||
from starlette.responses import PlainTextResponse | ||
from structlog.stdlib import BoundLogger | ||
|
||
from ltdproxy.config import config | ||
from ltdproxy.s3 import Bucket, bucket_dependency | ||
|
||
health_router = APIRouter() | ||
|
||
|
||
@health_router.get("/__healthz", name="healthz") | ||
def healthy() -> PlainTextResponse: | ||
async def healthy( | ||
bucket: Bucket = Depends(bucket_dependency), | ||
logger: BoundLogger = Depends(logger_dependency), | ||
http_client: httpx.AsyncClient = Depends(http_client_dependency), | ||
) -> PlainTextResponse: | ||
if config.healthcheck_bucket_key: | ||
# enter mode for testing S3 streaming | ||
stream = await bucket.stream_object( | ||
http_client, config.healthcheck_bucket_key | ||
) | ||
if stream.status_code != httpx.codes.OK: | ||
logger.error( | ||
"Health check got bad S3 response code", | ||
status_code=stream.status_code, | ||
) | ||
return PlainTextResponse("ERROR", status_code=500) | ||
async for _ in stream.aiter_bytes(): | ||
pass | ||
await stream.aclose() | ||
|
||
return PlainTextResponse("OK", status_code=200) |