Skip to content

Commit

Permalink
Add kubernetes health check endpoint to server
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirving committed Nov 1, 2023
1 parent 796f95e commit cf236e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/lsst/daf/butler/remote_butler/server/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from fastapi import Depends, FastAPI
from fastapi.middleware.gzip import GZipMiddleware
from lsst.daf.butler import Butler
from safir.metadata import Metadata, get_metadata

from ._config import get_config_from_env
from ._factory import Factory
Expand All @@ -56,6 +57,26 @@ def factory_dependency() -> Factory:
return Factory(butler=_make_global_butler())


@app.get(
"/",
description=(
"Return metadata about the running application. Can also be used as"
" a health check. This route is not exposed outside the cluster and"
" therefore cannot be used by external clients."
),
include_in_schema=False,
response_model=Metadata,
response_model_exclude_none=True,
summary="Application metadata",
)
async def get_index() -> Metadata:
"""GET ``/`` (the app's internal root).
By convention, this endpoint returns only the application's metadata.
"""
return get_metadata(package_name="lsst.daf.butler", application_name="butler")


@app.get("/butler/v1/universe", response_model=dict[str, Any])
def get_dimension_universe(factory: Factory = Depends(factory_dependency)) -> dict[str, Any]:
"""Allow remote client to get dimensions definition."""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ pyarrow >= 0.16
responses >= 0.12.0
urllib3 >= 1.25.10
fastapi
safir >= 3.4.0
5 changes: 5 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def tearDownClass(cls):
del app.dependency_overrides[factory_dependency]
removeTestTempDir(cls.root)

def test_health_check(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["name"], "butler")

def test_simple(self):
response = self.client.get("/butler/v1/universe")
self.assertEqual(response.status_code, 200)
Expand Down

0 comments on commit cf236e2

Please sign in to comment.