Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temp: Log all image references #84

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ select = ["ALL"]
"SLF001", # tests are allowed to access private members
"T201", # tests are allowed to use print
]
"src/noteburst/worker/main.py" = [
"PLR0915", # along a long function
]

[tool.ruff.lint.isort]
known-first-party = ["noteburst", "tests"]
Expand Down
3 changes: 2 additions & 1 deletion src/noteburst/jupyterclient/jupyterlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import websockets
from httpx import Cookies, Timeout
from pydantic import BaseModel, Field
from structlog import BoundLogger
from structlog.stdlib import BoundLogger
from websockets.client import WebSocketClientProtocol
from websockets.exceptions import WebSocketException
from websockets.typing import Data as WebsocketData
Expand Down Expand Up @@ -442,6 +442,7 @@ def lab_controller(self) -> LabControllerClient:
http_client=self.http_client,
token=noteburst_config.gafaelfawr_token.get_secret_value(),
url_prefix=noteburst_config.nublado_controller_path_prefix,
logger=self.logger,
)
return self._lab_controller_client

Expand Down
10 changes: 8 additions & 2 deletions src/noteburst/jupyterclient/labcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import httpx
from pydantic import BaseModel, ConfigDict, Field
from structlog.stdlib import BoundLogger

from noteburst.config import config

Expand Down Expand Up @@ -95,7 +96,9 @@ class LabControllerImages(BaseModel):
)
"""Pydantic model configuration."""

def get_by_reference(self, reference: str) -> JupyterImage | None:
def get_by_reference(
self, reference: str, logger: BoundLogger
) -> JupyterImage | None:
"""Get the JupyterImage with a corresponding reference.

Parameters
Expand All @@ -109,6 +112,7 @@ def get_by_reference(self, reference: str) -> JupyterImage | None:
Returns the JupyterImage if found, None otherwise.
"""
for image in self.all:
logger.info("Image reference", image=image.reference)
if reference == image.reference:
return image

Expand Down Expand Up @@ -142,10 +146,12 @@ def __init__(
http_client: httpx.AsyncClient,
token: str,
url_prefix: str,
logger: BoundLogger,
) -> None:
self._http_client = http_client
self._token = token
self._url_prefix = url_prefix
self._logger = logger

async def get_latest_weekly(self) -> JupyterImage:
"""Image for the latest weekly version.
Expand Down Expand Up @@ -200,7 +206,7 @@ async def get_by_reference(self, reference: str) -> JupyterImage:
An error occurred talking to JupyterLab Controller.
"""
images = await self._get_images()
image = images.get_by_reference(reference)
image = images.get_by_reference(reference, logger=self._logger)
if image is None:
raise LabControllerError(
f"No image with reference {reference} found."
Expand Down
1 change: 0 additions & 1 deletion src/noteburst/jupyterclient/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ async def create(
"username": username,
"name": "Noteburst",
"token_type": "service",
"token_name": f"noteburst {float(time.time())!s}",
"scopes": scopes,
"expires": int(time.time() + lifetime),
}
Expand Down
30 changes: 24 additions & 6 deletions src/noteburst/worker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,35 @@ async def startup(ctx: dict[Any, Any]) -> None:
user = User(
username=identity.username, uid=identity.uid, gid=identity.gid
)
authed_user = await user.login(
scopes=config.parsed_worker_token_scopes,
http_client=http_client,
token_lifetime=config.worker_token_lifetime,
)
try:
authed_user = await user.login(
scopes=config.parsed_worker_token_scopes,
http_client=http_client,
token_lifetime=config.worker_token_lifetime,
)
except httpx.HTTPStatusError as e:
logger.exception(
"Error authenticating the worker's user",
body=e.response.json(),
status_code=e.response.status_code,
)
raise
logger.info("Authenticated the worker's user.")

jupyter_client = JupyterClient(
user=authed_user, logger=logger, config=jupyter_config
)
await jupyter_client.log_into_hub()
try:
await jupyter_client.log_into_hub()
except httpx.HTTPStatusError as e:
logger.exception(
"Error logging into JupyterHub",
body=e.response.json(),
)
raise
except Exception:
logger.exception("Generic error logging into JupyterHub")
raise
try:
image_info = await jupyter_client.spawn_lab()
logger = logger.bind(image_ref=image_info.reference)
Expand Down
2 changes: 0 additions & 2 deletions tests/support/gafaelfawr.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def handler(request: httpx.Request) -> httpx.Response:
assert request_json == {
"username": ANY,
"token_type": "service",
"token_name": ANY,
"scopes": ["exec:notebook"],
"expires": ANY,
"name": "Noteburst",
Expand All @@ -69,7 +68,6 @@ def handler(request: httpx.Request) -> httpx.Response:
assert request_json["uid"] == uid
if gid:
assert request_json["gid"] == gid
assert request_json["token_name"].startswith("noteburst ")
assert request_json["expires"] > time.time()
response = {"token": make_gafaelfawr_token(request_json["username"])}
return httpx.Response(200, json=response, request=request)
Expand Down