Skip to content

Commit caf0c3c

Browse files
Panaetiusolevski
andauthored
feat: add new service cache and migrations (#759)
* feat: initial implementation for postgres cache * squashme: Apply suggestions from code review * address comments * add new service and migrations * fix mypy * feat: use new cache in code * move server_options to app_config to fix wrong import direction (components -> bases should be the other way around) * fix tests * make api client work with notebooks fixtures * add watcher in test fixtures and fix k3d tests * address comments and fix deployment * make k8s_objects.name unique --------- Co-authored-by: Tasko Olevski <olevski90@gmail.com>
1 parent 0caedf7 commit caf0c3c

38 files changed

Lines changed: 10563 additions & 5659 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Kubernetes Cache."""
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""K8s cache config."""
2+
3+
from dataclasses import dataclass
4+
from typing import Self
5+
6+
from kubernetes.client.api_client import os
7+
8+
from renku_data_services.db_config.config import DBConfig
9+
from renku_data_services.k8s_watcher.db import K8sDbCache
10+
11+
12+
@dataclass
13+
class _K8sConfig:
14+
"""Defines the k8s client and namespace."""
15+
16+
renku_namespace: str = "default"
17+
18+
@classmethod
19+
def from_env(cls) -> Self:
20+
return cls(renku_namespace=os.environ.get("KUBERNETES_NAMESPACE", "default"))
21+
22+
23+
@dataclass
24+
class Config:
25+
"""K8s cache config."""
26+
27+
db: DBConfig
28+
k8s: _K8sConfig
29+
_k8s_cache: K8sDbCache | None = None
30+
31+
@property
32+
def k8s_cache(self) -> K8sDbCache:
33+
"""The DB adapter for the k8s cache."""
34+
if not self._k8s_cache:
35+
self._k8s_cache = K8sDbCache(
36+
session_maker=self.db.async_session_maker,
37+
)
38+
return self._k8s_cache
39+
40+
@classmethod
41+
def from_env(cls, prefix: str = "") -> "Config":
42+
"""Create a config from environment variables."""
43+
db = DBConfig.from_env(prefix)
44+
k8s_config = _K8sConfig.from_env()
45+
46+
return cls(db=db, k8s=k8s_config)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""The entrypoint for the k8s cache service."""
2+
3+
import asyncio
4+
import logging
5+
6+
import kr8s
7+
8+
from renku_data_services.k8s_cache.config import Config
9+
from renku_data_services.k8s_watcher.db import Cluster, K8sWatcher, k8s_object_handler
10+
from renku_data_services.k8s_watcher.models import ClusterId
11+
from renku_data_services.notebooks.constants import AMALTHEA_SESSION_KIND, JUPYTER_SESSION_KIND
12+
13+
14+
async def main() -> None:
15+
"""K8s cache entrypoint."""
16+
17+
logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO)
18+
config = Config.from_env()
19+
20+
kr8s_api = await kr8s.asyncio.api()
21+
clusters = [Cluster(id=ClusterId("renkulab"), namespace=config.k8s.renku_namespace, api=kr8s_api)]
22+
23+
watcher = K8sWatcher(
24+
handler=k8s_object_handler(config.k8s_cache),
25+
clusters={c.id: c for c in clusters},
26+
kinds=[AMALTHEA_SESSION_KIND, JUPYTER_SESSION_KIND],
27+
)
28+
await watcher.start()
29+
logging.info("started watching resources")
30+
# create file for liveness probe
31+
with open("/tmp/cache_ready", "w") as f:
32+
f.write("ready")
33+
await watcher.wait()
34+
35+
36+
if __name__ == "__main__":
37+
asyncio.run(main())

development/.keep renamed to bases/renku_data_services/k8s_cache/py.typed

File renamed without changes.

chartpress.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ charts:
2323
contextPath: .
2424
dockerfilePath: projects/secrets_storage/Dockerfile
2525
valuesPath: secretsStorage.image
26+
data-service-k8s-watcher:
27+
contextPath: .
28+
dockerfilePath: projects/k8s_watcher/Dockerfile
29+
valuesPath: dataService.k8sWatcher.image

components/renku_data_services/app_config/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
import renku_data_services.storage
3737
import renku_data_services.users
3838
from renku_data_services import errors
39+
from renku_data_services.app_config.server_options import (
40+
ServerOptions,
41+
ServerOptionsDefaults,
42+
generate_default_resource_pool,
43+
)
3944
from renku_data_services.authn.dummy import DummyAuthenticator, DummyUserStore
4045
from renku_data_services.authn.gitlab import GitlabAuthenticator
4146
from renku_data_services.authn.keycloak import KcUserStore, KeycloakAuthenticator
@@ -44,11 +49,6 @@
4449
from renku_data_services.connected_services.db import ConnectedServicesRepository
4550
from renku_data_services.crc import models
4651
from renku_data_services.crc.db import ResourcePoolRepository, UserRepository
47-
from renku_data_services.data_api.server_options import (
48-
ServerOptions,
49-
ServerOptionsDefaults,
50-
generate_default_resource_pool,
51-
)
5252
from renku_data_services.data_connectors.db import (
5353
DataConnectorRepository,
5454
DataConnectorSecretRepository,

bases/renku_data_services/data_api/server_options.py renamed to components/renku_data_services/app_config/server_options.py

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""K8s watcher and k8s clients."""

0 commit comments

Comments
 (0)