Skip to content

Commit a45bec2

Browse files
sgaistolevski
andcommitted
feat: ensure notebook endpoints do their job (#388)
This work brings the notebook related endpoints to a working state to serve as replacement for the renku-notebooks external service. Short summary: - API tests have been added - Code has been fixed to answer / handle exception - Automated test cluster creation has been added using k3d --------- Co-authored-by: Tasko Olevski <[email protected]>
1 parent 1f3264c commit a45bec2

File tree

29 files changed

+665
-100
lines changed

29 files changed

+665
-100
lines changed

.devcontainer/.poetry_cache/.keep

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

.devcontainer/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"command": "poetry self add poetry-polylith-plugin"
1212
},
1313
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
14-
"ghcr.io/mpriscella/features/kind:1": {},
1514
"ghcr.io/devcontainers-contrib/features/gh-release:1": {
1615
"repo": "authzed/zed",
1716
"binaryNames": "zed"
@@ -28,7 +27,8 @@
2827
"ghcr.io/EliiseS/devcontainer-features/bash-profile:1": {
2928
"command": "alias k=kubectl"
3029
},
31-
"ghcr.io/devcontainers-contrib/features/rclone:1": {}
30+
"ghcr.io/devcontainers-contrib/features/rclone:1": {},
31+
"./k3d": {}
3232
},
3333
"overrideFeatureInstallOrder": [
3434
"ghcr.io/devcontainers-contrib/features/poetry",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "k3d",
3+
"version": "1.0.0",
4+
"name": "k3s based kubernetes cluster in docker",
5+
"postCreateCommand": "k3d --version",
6+
"installsAfter": [
7+
"ghcr.io/devcontainers-contrib/features/bash-command"
8+
],
9+
"options": {
10+
"k3d_version": {
11+
"type": "string",
12+
"description": "k3d version to install",
13+
"proposals": ["latest", "5.7.4"],
14+
"default": "latest"
15+
}
16+
}
17+
}

.devcontainer/k3d/install.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if [ "${K3D_VERSION}" != "none" ]; then
2+
echo "Downloading k3d..."
3+
if [ "${K3D_VERSION}" = "latest" ]; then
4+
# Install and check the hash
5+
curl -sSL https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
6+
else
7+
find_version_from_git_tags K3D_VERSION https://github.com/kubernetes/K3D
8+
if [ "${K3D_VERSION::1}" != "v" ]; then
9+
K3D_VERSION="v${K3D_VERSION}"
10+
fi
11+
# Install and check the hash
12+
curl -sSL https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | TAG="${K3D_VERSION}" bash
13+
fi
14+
fi

.github/workflows/test_publish.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ jobs:
9595
- uses: actions/checkout@v4
9696
with:
9797
fetch-depth: 0
98+
- uses: actions/cache/restore@v3
99+
name: Restore cache
100+
with:
101+
path: ${{ env.CACHE_PATH }}
102+
key: ${{ env.CACHE_KEY }}
98103
- name: Set Git config
99104
shell: bash
100105
run: |

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ repos:
1818
- id: check-toml
1919
- id: debug-statements
2020
- id: end-of-file-fixer
21+
exclude: 'components/renku_data_services/message_queue/(avro_models|schemas)'
2122
- id: mixed-line-ending
2223
- id: trailing-whitespace
24+
exclude: 'components/renku_data_services/message_queue/(avro_models|schemas)'
2325
- repo: https://github.com/asottile/yesqa
2426
rev: v1.5.0
2527
hooks:

Makefile

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.PHONY: schemas tests test_setup main_tests schemathesis_tests collect_coverage style_checks pre_commit_checks run download_avro check_avro avro_models update_avro kind_cluster install_amaltheas all
1+
.PHONY: schemas tests test_setup main_tests schemathesis_tests collect_coverage style_checks pre_commit_checks run download_avro check_avro avro_models update_avro k3d_cluster install_amaltheas all
22

33
AMALTHEA_JS_VERSION ?= 0.12.2
4-
AMALTHEA_SESSIONS_VERSION ?= 0.0.9-new-operator-chart
4+
AMALTHEA_SESSIONS_VERSION ?= 0.0.10-new-operator-chart
55
codegen_params = --input-file-type openapi --output-model-type pydantic_v2.BaseModel --use-double-quotes --target-python-version 3.12 --collapse-root-models --field-constraints --strict-nullable --set-default-enum-member --openapi-scopes schemas paths parameters --set-default-enum-member --use-one-literal-as-default --use-default
66

77
define test_apispec_up_to_date
@@ -143,21 +143,15 @@ help: ## Display this help.
143143

144144
##@ Helm/k8s
145145

146-
kind_cluster: ## Creates a kind cluster for testing
147-
kind delete cluster
148-
docker network rm -f kind
149-
docker network create -d=bridge -o com.docker.network.bridge.enable_ip_masquerade=true -o com.docker.network.driver.mtu=1500 --ipv6=false kind
150-
kind create cluster --config kind_config.yaml
151-
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
152-
echo "Waiting for ingress controller to initialize"
153-
sleep 15
154-
kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=90s
146+
k3d_cluster: ## Creates a k3d cluster for testing
147+
k3d cluster delete
148+
k3d cluster create --agents 1 --k3s-arg --disable=metrics-server@server:0
155149

156150
install_amaltheas: ## Installs both version of amalthea in the. NOTE: It uses the currently active k8s context.
157151
helm repo add renku https://swissdatasciencecenter.github.io/helm-charts
158152
helm repo update
159153
helm upgrade --install amalthea-js renku/amalthea --version $(AMALTHEA_JS_VERSION)
160-
helm upgrade --install amalthea-sessions amalthea-sessions-0.0.9-new-operator-chart.tgz --version $(AMALTHEA_SESSIONS_VERSION)
154+
helm upgrade --install amalthea-se renku/amalthea-sessions --version ${AMALTHEA_SESSIONS_VERSION}
161155

162156
# TODO: Add the version variables from the top of the file here when the charts are fully published
163157
amalthea_schema: ## Updates generates pydantic classes from CRDs

components/renku_data_services/base_api/auth.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,30 @@ async def decorated_function(*args: _P.args, **kwargs: _P.kwargs) -> _T:
180180
return response
181181

182182
return decorated_function
183+
184+
185+
def internal_gitlab_authenticate(
186+
authenticator: Authenticator,
187+
) -> Callable[
188+
[Callable[Concatenate[Request, APIUser, APIUser, _P], Coroutine[Any, Any, _T]]],
189+
Callable[Concatenate[Request, APIUser, _P], Coroutine[Any, Any, _T]],
190+
]:
191+
"""Decorator for a Sanic handler that that adds a user for the internal gitlab user."""
192+
193+
def decorator(
194+
f: Callable[Concatenate[Request, APIUser, APIUser, _P], Coroutine[Any, Any, _T]],
195+
) -> Callable[Concatenate[Request, APIUser, _P], Coroutine[Any, Any, _T]]:
196+
@wraps(f)
197+
async def decorated_function(
198+
request: Request,
199+
user: APIUser,
200+
*args: _P.args,
201+
**kwargs: _P.kwargs,
202+
) -> _T:
203+
access_token = str(request.headers.get("Gitlab-Access-Token"))
204+
internal_gitlab_user = await authenticator.authenticate(access_token, request)
205+
return await f(request, user, internal_gitlab_user, *args, **kwargs)
206+
207+
return decorated_function
208+
209+
return decorator

components/renku_data_services/notebooks/api.spec.yaml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,8 @@ components:
455455
message:
456456
type: string
457457
example: "Something went wrong - please try again later"
458-
required:
459-
- "code"
460-
- "message"
461-
required:
462-
- "error"
458+
required: ["code", "message"]
459+
required: ["error"]
463460
Generated:
464461
properties:
465462
enabled:
@@ -478,7 +475,7 @@ components:
478475
repositories:
479476
type: array
480477
default: []
481-
items:
478+
items:
482479
"$ref": "#/components/schemas/LaunchNotebookRequestRepository"
483480
cloudstorage:
484481
default: []
@@ -489,7 +486,7 @@ components:
489486
default: 1
490487
type: integer
491488
resource_class_id:
492-
default:
489+
default:
493490
nullable: true
494491
type: integer
495492
environment_variables:
@@ -539,7 +536,7 @@ components:
539536
default: {}
540537
type: object
541538
image:
542-
default:
539+
default:
543540
nullable: true
544541
type: string
545542
lfs_auto_fetch:
@@ -548,13 +545,13 @@ components:
548545
namespace:
549546
type: string
550547
notebook:
551-
default:
548+
default:
552549
nullable: true
553550
type: string
554551
project:
555552
type: string
556553
resource_class_id:
557-
default:
554+
default:
558555
nullable: true
559556
type: integer
560557
serverOptions:
@@ -565,7 +562,7 @@ components:
565562
user_secrets:
566563
allOf:
567564
- "$ref": "#/components/schemas/UserSecrets"
568-
default:
565+
default:
569566
nullable: true
570567
required:
571568
- commit_sha
@@ -660,7 +657,7 @@ components:
660657
properties:
661658
configuration:
662659
additionalProperties: {}
663-
default:
660+
default:
664661
nullable: true
665662
type: object
666663
readonly:
@@ -669,7 +666,7 @@ components:
669666
source_path:
670667
type: string
671668
storage_id:
672-
default:
669+
default:
673670
nullable: true
674671
type: string
675672
target_path:
@@ -882,7 +879,7 @@ components:
882879
type: integer
883880
description: The size of disk storage for the session, in gigabytes
884881
resource_class_id:
885-
default:
882+
default:
886883
nullable: true
887884
type: integer
888885
cloudstorage:

components/renku_data_services/notebooks/api/amalthea_patches/general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def session_tolerations(server: "UserServer") -> list[dict[str, Any]]:
3030
"op": "add",
3131
"path": "/statefulset/spec/template/spec/tolerations",
3232
"value": default_tolerations
33-
+ [i.json_match_expression() for i in server.server_options.tolerations],
33+
+ [toleration.json_match_expression() for toleration in server.server_options.tolerations],
3434
}
3535
],
3636
}

components/renku_data_services/notebooks/api/amalthea_patches/init_containers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ def certificates_container(config: _NotebooksConfig) -> tuple[client.V1Container
312312
projected=client.V1ProjectedVolumeSource(
313313
default_mode=440,
314314
sources=[
315-
{"secret": {"name": i.get("secret")}}
316-
for i in config.sessions.ca_certs.secrets
317-
if isinstance(i, dict) and i.get("secret") is not None
315+
{"secret": {"name": secret.get("secret")}}
316+
for secret in config.sessions.ca_certs.secrets
317+
if isinstance(secret, dict) and secret.get("secret") is not None
318318
],
319319
),
320320
)

components/renku_data_services/notebooks/api/classes/k8s_client.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ async def get_pod_logs(self, name: str, max_log_lines: Optional[int] = None) ->
7979
"""Get the logs of all containers in the session."""
8080
pod = cast(Pod, await Pod.get(name=name, namespace=self.namespace))
8181
logs: dict[str, str] = {}
82-
containers = [i.name for i in pod.spec.containers] + [i.name for i in pod.spec.initContainers]
82+
containers = [container.name for container in pod.spec.containers + pod.spec.get("initContainers", [])]
8383
for container in containers:
8484
try:
8585
# NOTE: calling pod.logs without a container name set crashes the library
86-
clogs: list[str] = [i async for i in pod.logs(container=container, tail_lines=max_log_lines)]
86+
clogs: list[str] = [clog async for clog in pod.logs(container=container, tail_lines=max_log_lines)]
8787
except NotFoundError:
8888
raise errors.MissingResourceError(message=f"The session pod {name} does not exist.")
8989
except ServerError as err:
@@ -243,8 +243,10 @@ async def patch_statefulset_tokens(self, name: str, renku_tokens: RenkuTokens) -
243243
except NotFoundError:
244244
return None
245245

246-
containers: list[V1Container] = [V1Container(**i) for i in sts.spec.template.spec.containers]
247-
init_containers: list[V1Container] = [V1Container(**i) for i in sts.spec.template.spec.init_containers]
246+
containers: list[V1Container] = [V1Container(**container) for container in sts.spec.template.spec.containers]
247+
init_containers: list[V1Container] = [
248+
V1Container(**container) for container in sts.spec.template.spec.init_containers
249+
]
248250

249251
git_proxy_container_index, git_proxy_container = next(
250252
((i, c) for i, c in enumerate(containers) if c.name == "git-proxy"),
@@ -368,7 +370,7 @@ async def list_servers(self, safe_username: str) -> list[_SessionType]:
368370
)
369371
raise JSCacheError(f"The JSCache produced an unexpected status code: {res.status_code}")
370372

371-
return [self.server_type.model_validate(i) for i in res.json()]
373+
return [self.server_type.model_validate(server) for server in res.json()]
372374

373375
async def get_server(self, name: str) -> _SessionType | None:
374376
"""Get a specific jupyter server."""
@@ -441,7 +443,11 @@ async def get_server_logs(
441443
) -> dict[str, str]:
442444
"""Get the logs from the server."""
443445
# NOTE: this get_server ensures the user has access to the server without it you could read someone elses logs
444-
_ = await self.get_server(server_name, safe_username)
446+
server = await self.get_server(server_name, safe_username)
447+
if not server:
448+
raise MissingResourceError(
449+
f"Cannot find server {server_name} for user " f"{safe_username} to retrieve logs."
450+
)
445451
pod_name = f"{server_name}-0"
446452
return await self.renku_ns_client.get_pod_logs(pod_name, max_log_lines)
447453

@@ -481,9 +487,10 @@ async def delete_server(self, server_name: str, safe_username: str) -> None:
481487
"""Delete the server."""
482488
server = await self.get_server(server_name, safe_username)
483489
if not server:
484-
return None
485-
await self.renku_ns_client.delete_server(server_name)
486-
return None
490+
raise MissingResourceError(
491+
f"Cannot find server {server_name} for user " f"{safe_username} in order to delete it."
492+
)
493+
return await self.renku_ns_client.delete_server(server_name)
487494

488495
async def patch_tokens(self, server_name: str, renku_tokens: RenkuTokens, gitlab_token: GitlabToken) -> None:
489496
"""Patch the Renku and Gitlab access tokens used in a session."""

0 commit comments

Comments
 (0)