qdrant-operator 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qdrant_operator/__init__.py +1 -0
- qdrant_operator/container.py +49 -0
- qdrant_operator/domain.py +1245 -0
- qdrant_operator/handlers.py +189 -0
- qdrant_operator/helm_adapter.py +85 -0
- qdrant_operator/kubernetes_adapter.py +144 -0
- qdrant_operator/main.py +48 -0
- qdrant_operator/ports.py +116 -0
- qdrant_operator/py.typed +0 -0
- qdrant_operator/qdrant_adapter.py +124 -0
- qdrant_operator/s3_adapter.py +113 -0
- qdrant_operator/usecases.py +597 -0
- qdrant_operator-0.2.0.dist-info/METADATA +284 -0
- qdrant_operator-0.2.0.dist-info/RECORD +17 -0
- qdrant_operator-0.2.0.dist-info/WHEEL +4 -0
- qdrant_operator-0.2.0.dist-info/entry_points.txt +3 -0
- qdrant_operator-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Kopf handlers: translate Kubernetes events into use case calls and status patches."""
|
|
2
|
+
|
|
3
|
+
# pyright: reportArgumentType=false
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from datetime import UTC
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import kopf
|
|
12
|
+
|
|
13
|
+
from qdrant_operator.container import Container
|
|
14
|
+
from qdrant_operator.domain import BACKUPS
|
|
15
|
+
from qdrant_operator.domain import CLUSTERS
|
|
16
|
+
from qdrant_operator.domain import GROUP
|
|
17
|
+
from qdrant_operator.domain import RESTORES
|
|
18
|
+
from qdrant_operator.domain import SCHEDULES
|
|
19
|
+
from qdrant_operator.domain import VERSION
|
|
20
|
+
from qdrant_operator.domain import BackupPhase
|
|
21
|
+
from qdrant_operator.domain import BackupScheduleSpec
|
|
22
|
+
from qdrant_operator.domain import BackupScheduleStatus
|
|
23
|
+
from qdrant_operator.domain import BackupSpec
|
|
24
|
+
from qdrant_operator.domain import BackupStatus
|
|
25
|
+
from qdrant_operator.domain import ClusterSpec
|
|
26
|
+
from qdrant_operator.domain import ClusterStatus
|
|
27
|
+
from qdrant_operator.domain import ResourceKind
|
|
28
|
+
from qdrant_operator.domain import ResourceRef
|
|
29
|
+
from qdrant_operator.domain import RestorePhase
|
|
30
|
+
from qdrant_operator.domain import RestoreSpec
|
|
31
|
+
from qdrant_operator.domain import RestoreStatus
|
|
32
|
+
from qdrant_operator.kubernetes_adapter import load_kubernetes_config
|
|
33
|
+
from qdrant_operator.usecases import ClusterNotFoundError
|
|
34
|
+
from qdrant_operator.usecases import SourceNotReadyError
|
|
35
|
+
|
|
36
|
+
FINALIZER = f"{GROUP}/finalizer"
|
|
37
|
+
CLUSTER_OBSERVE_INTERVAL = 30.0
|
|
38
|
+
SCHEDULE_TICK_INTERVAL = 60.0
|
|
39
|
+
BACKUP_EXPIRY_INTERVAL = 300.0
|
|
40
|
+
RETRY_DELAY_SECONDS = 30.0
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def resource_ref(kind: ResourceKind, meta: Mapping[str, Any]) -> ResourceRef:
|
|
44
|
+
return ResourceRef(kind, meta["name"], meta["namespace"])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@kopf.on.startup()
|
|
48
|
+
async def configure(settings: kopf.OperatorSettings, **_: Any) -> None:
|
|
49
|
+
settings.persistence.finalizer = FINALIZER
|
|
50
|
+
settings.persistence.progress_storage = kopf.AnnotationsProgressStorage(prefix=GROUP)
|
|
51
|
+
settings.persistence.diffbase_storage = kopf.AnnotationsDiffBaseStorage(
|
|
52
|
+
prefix=GROUP, key="last-applied-spec"
|
|
53
|
+
)
|
|
54
|
+
settings.posting.level = logging.INFO
|
|
55
|
+
await load_kubernetes_config()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@kopf.on.probe(id="ready")
|
|
59
|
+
def ready(**_: Any) -> str:
|
|
60
|
+
return "ok"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@kopf.on.create(GROUP, VERSION, CLUSTERS.plural)
|
|
64
|
+
@kopf.on.update(GROUP, VERSION, CLUSTERS.plural, field="spec")
|
|
65
|
+
@kopf.on.resume(GROUP, VERSION, CLUSTERS.plural)
|
|
66
|
+
async def reconcile_cluster(
|
|
67
|
+
spec: kopf.Spec,
|
|
68
|
+
meta: kopf.Meta,
|
|
69
|
+
status: kopf.Status,
|
|
70
|
+
patch: kopf.Patch,
|
|
71
|
+
logger: kopf.Logger,
|
|
72
|
+
**_: Any,
|
|
73
|
+
) -> None:
|
|
74
|
+
cluster = ClusterSpec.from_dict(spec, meta)
|
|
75
|
+
current = ClusterStatus.from_dict(status)
|
|
76
|
+
result = await Container().reconcile_cluster().execute(cluster, meta.get("generation"), current)
|
|
77
|
+
patch.status.update(result.to_dict())
|
|
78
|
+
logger.info(f"Helm release {cluster.release_name} applied; phase {result.phase.value}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@kopf.timer(GROUP, VERSION, CLUSTERS.plural, interval=CLUSTER_OBSERVE_INTERVAL, initial_delay=10.0)
|
|
82
|
+
async def observe_cluster(
|
|
83
|
+
spec: kopf.Spec, meta: kopf.Meta, status: kopf.Status, patch: kopf.Patch, **_: Any
|
|
84
|
+
) -> None:
|
|
85
|
+
cluster = ClusterSpec.from_dict(spec, meta)
|
|
86
|
+
current = ClusterStatus.from_dict(status)
|
|
87
|
+
result = await Container().observe_cluster().execute(cluster, current)
|
|
88
|
+
if result == current:
|
|
89
|
+
return
|
|
90
|
+
patch.status.update(result.to_dict())
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@kopf.on.delete(GROUP, VERSION, CLUSTERS.plural)
|
|
94
|
+
async def delete_cluster(spec: kopf.Spec, meta: kopf.Meta, logger: kopf.Logger, **_: Any) -> None:
|
|
95
|
+
cluster = ClusterSpec.from_dict(spec, meta)
|
|
96
|
+
await Container().delete_cluster().execute(cluster)
|
|
97
|
+
logger.info(f"Helm release {cluster.release_name} removed")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@kopf.on.create(GROUP, VERSION, BACKUPS.plural)
|
|
101
|
+
async def execute_backup(
|
|
102
|
+
spec: kopf.Spec, meta: kopf.Meta, patch: kopf.Patch, logger: kopf.Logger, **_: Any
|
|
103
|
+
) -> None:
|
|
104
|
+
backup = BackupSpec.from_dict(spec, meta)
|
|
105
|
+
ref = resource_ref(BACKUPS, meta)
|
|
106
|
+
try:
|
|
107
|
+
result = await Container().execute_backup().execute(backup, ref)
|
|
108
|
+
except (ClusterNotFoundError, KeyError) as error:
|
|
109
|
+
raise kopf.TemporaryError(str(error), delay=RETRY_DELAY_SECONDS) from error
|
|
110
|
+
except Exception as error:
|
|
111
|
+
patch.status.update(BackupStatus(phase=BackupPhase.FAILED, error=str(error)).to_dict())
|
|
112
|
+
raise kopf.PermanentError(f"Backup failed: {error}") from error
|
|
113
|
+
patch.status.update(result.to_dict())
|
|
114
|
+
logger.info(f"Backup {result.phase.value}: {result.s3_path} ({result.total_size})")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@kopf.on.delete(GROUP, VERSION, BACKUPS.plural)
|
|
118
|
+
async def delete_backup(spec: kopf.Spec, meta: kopf.Meta, logger: kopf.Logger, **_: Any) -> None:
|
|
119
|
+
backup = BackupSpec.from_dict(spec, meta)
|
|
120
|
+
try:
|
|
121
|
+
deleted = await Container().delete_backup_data().execute(backup)
|
|
122
|
+
except KeyError as error:
|
|
123
|
+
logger.warning(f"Leaving backup data in place, credentials unavailable: {error}")
|
|
124
|
+
return
|
|
125
|
+
logger.info(f"Deleted {deleted} objects under {backup.storage.uri(backup.name)}")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def has_expiry(status: kopf.Status, **_: Any) -> bool:
|
|
129
|
+
return bool(status.get("expiresAt"))
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@kopf.timer(
|
|
133
|
+
GROUP,
|
|
134
|
+
VERSION,
|
|
135
|
+
BACKUPS.plural,
|
|
136
|
+
interval=BACKUP_EXPIRY_INTERVAL,
|
|
137
|
+
when=has_expiry,
|
|
138
|
+
)
|
|
139
|
+
async def expire_backup(
|
|
140
|
+
meta: kopf.Meta, status: kopf.Status, logger: kopf.Logger, **_: Any
|
|
141
|
+
) -> None:
|
|
142
|
+
ref = resource_ref(BACKUPS, meta)
|
|
143
|
+
expired = (
|
|
144
|
+
await Container()
|
|
145
|
+
.expire_backup()
|
|
146
|
+
.execute(ref, BackupStatus.from_dict(status), datetime.now(UTC))
|
|
147
|
+
)
|
|
148
|
+
if expired:
|
|
149
|
+
logger.info(f"Backup {meta['name']} passed retentionDays and was deleted")
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@kopf.on.create(GROUP, VERSION, RESTORES.plural)
|
|
153
|
+
async def execute_restore(
|
|
154
|
+
spec: kopf.Spec, meta: kopf.Meta, patch: kopf.Patch, logger: kopf.Logger, **_: Any
|
|
155
|
+
) -> None:
|
|
156
|
+
ref = resource_ref(RESTORES, meta)
|
|
157
|
+
try:
|
|
158
|
+
restore = RestoreSpec.from_dict(spec, meta)
|
|
159
|
+
result = await Container().execute_restore().execute(restore, ref)
|
|
160
|
+
except (ClusterNotFoundError, SourceNotReadyError, KeyError) as error:
|
|
161
|
+
raise kopf.TemporaryError(str(error), delay=RETRY_DELAY_SECONDS) from error
|
|
162
|
+
except Exception as error:
|
|
163
|
+
patch.status.update(RestoreStatus(phase=RestorePhase.FAILED, error=str(error)).to_dict())
|
|
164
|
+
raise kopf.PermanentError(f"Restore failed: {error}") from error
|
|
165
|
+
patch.status.update(result.to_dict())
|
|
166
|
+
logger.info(f"Restore {result.phase.value} from {result.source_backup}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@kopf.on.create(GROUP, VERSION, SCHEDULES.plural)
|
|
170
|
+
@kopf.on.update(GROUP, VERSION, SCHEDULES.plural, field="spec")
|
|
171
|
+
@kopf.on.resume(GROUP, VERSION, SCHEDULES.plural)
|
|
172
|
+
@kopf.timer(GROUP, VERSION, SCHEDULES.plural, interval=SCHEDULE_TICK_INTERVAL)
|
|
173
|
+
async def process_schedule(
|
|
174
|
+
spec: kopf.Spec,
|
|
175
|
+
meta: kopf.Meta,
|
|
176
|
+
status: kopf.Status,
|
|
177
|
+
body: kopf.Body,
|
|
178
|
+
patch: kopf.Patch,
|
|
179
|
+
logger: kopf.Logger,
|
|
180
|
+
**_: Any,
|
|
181
|
+
) -> None:
|
|
182
|
+
schedule = BackupScheduleSpec.from_dict(spec, meta)
|
|
183
|
+
current = BackupScheduleStatus.from_dict(status)
|
|
184
|
+
result = (
|
|
185
|
+
await Container().process_schedule().execute(schedule, current, body, datetime.now(UTC))
|
|
186
|
+
)
|
|
187
|
+
if result.active_backup and result.active_backup != current.active_backup:
|
|
188
|
+
logger.info(f"Scheduled backup {result.active_backup} created")
|
|
189
|
+
patch.status.update(result.to_dict())
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Helm CLI adapter: stateless, one `helm upgrade --install` per reconcile, values over stdin."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import json
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from loguru import logger
|
|
10
|
+
|
|
11
|
+
QDRANT_CHART = "qdrant"
|
|
12
|
+
QDRANT_CHART_REPO = "https://qdrant.github.io/qdrant-helm"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class HelmError(RuntimeError):
|
|
16
|
+
"""A helm invocation exited non-zero."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class HelmAdapter:
|
|
21
|
+
chart: str = QDRANT_CHART
|
|
22
|
+
repo: str = QDRANT_CHART_REPO
|
|
23
|
+
kubeconfig: str | None = None
|
|
24
|
+
timeout_seconds: float = 600.0
|
|
25
|
+
|
|
26
|
+
async def apply(
|
|
27
|
+
self,
|
|
28
|
+
release_name: str,
|
|
29
|
+
namespace: str,
|
|
30
|
+
chart_version: str,
|
|
31
|
+
values: Mapping[str, Any],
|
|
32
|
+
) -> None:
|
|
33
|
+
await self.run(
|
|
34
|
+
"upgrade",
|
|
35
|
+
"--install",
|
|
36
|
+
release_name,
|
|
37
|
+
self.chart,
|
|
38
|
+
"--repo",
|
|
39
|
+
self.repo,
|
|
40
|
+
"--version",
|
|
41
|
+
chart_version,
|
|
42
|
+
"--namespace",
|
|
43
|
+
namespace,
|
|
44
|
+
"--create-namespace",
|
|
45
|
+
"--values",
|
|
46
|
+
"-",
|
|
47
|
+
stdin=json.dumps(values).encode(),
|
|
48
|
+
)
|
|
49
|
+
logger.info("helm release applied", release=release_name, namespace=namespace)
|
|
50
|
+
|
|
51
|
+
async def uninstall(self, release_name: str, namespace: str) -> None:
|
|
52
|
+
await self.run("uninstall", release_name, "--namespace", namespace, "--wait")
|
|
53
|
+
logger.info("helm release uninstalled", release=release_name, namespace=namespace)
|
|
54
|
+
|
|
55
|
+
async def release_exists(self, release_name: str, namespace: str) -> bool:
|
|
56
|
+
try:
|
|
57
|
+
await self.run("status", release_name, "--namespace", namespace, "--output", "json")
|
|
58
|
+
except HelmError:
|
|
59
|
+
return False
|
|
60
|
+
return True
|
|
61
|
+
|
|
62
|
+
async def run(self, *args: str, stdin: bytes | None = None) -> str:
|
|
63
|
+
command = ["helm", *args, *self.kubeconfig_args()]
|
|
64
|
+
logger.debug("helm command", command=" ".join(command))
|
|
65
|
+
process = await asyncio.create_subprocess_exec(
|
|
66
|
+
*command,
|
|
67
|
+
stdin=asyncio.subprocess.PIPE if stdin is not None else None,
|
|
68
|
+
stdout=asyncio.subprocess.PIPE,
|
|
69
|
+
stderr=asyncio.subprocess.PIPE,
|
|
70
|
+
)
|
|
71
|
+
try:
|
|
72
|
+
stdout, stderr = await asyncio.wait_for(
|
|
73
|
+
process.communicate(stdin), timeout=self.timeout_seconds
|
|
74
|
+
)
|
|
75
|
+
except TimeoutError:
|
|
76
|
+
process.kill()
|
|
77
|
+
raise HelmError(f"helm {args[0]} timed out after {self.timeout_seconds}s") from None
|
|
78
|
+
if process.returncode != 0:
|
|
79
|
+
raise HelmError(f"helm {args[0]} failed: {stderr.decode().strip()}")
|
|
80
|
+
return stdout.decode()
|
|
81
|
+
|
|
82
|
+
def kubeconfig_args(self) -> list[str]:
|
|
83
|
+
if not self.kubeconfig:
|
|
84
|
+
return []
|
|
85
|
+
return ["--kubeconfig", self.kubeconfig]
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Kubernetes API adapter. Client configuration is loaded once at operator startup."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
from collections.abc import AsyncIterator
|
|
5
|
+
from contextlib import asynccontextmanager
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
from typing import cast
|
|
9
|
+
|
|
10
|
+
from kubernetes_asyncio import client
|
|
11
|
+
from kubernetes_asyncio import config as kubernetes_config
|
|
12
|
+
from kubernetes_asyncio.client.exceptions import ApiException
|
|
13
|
+
from loguru import logger
|
|
14
|
+
|
|
15
|
+
from qdrant_operator.domain import JsonDict
|
|
16
|
+
from qdrant_operator.domain import ResourceKind
|
|
17
|
+
from qdrant_operator.domain import ResourceRef
|
|
18
|
+
from qdrant_operator.domain import SecretRef
|
|
19
|
+
from qdrant_operator.domain import StatefulSetStatus
|
|
20
|
+
|
|
21
|
+
config: Any = kubernetes_config
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
async def load_kubernetes_config() -> None:
|
|
25
|
+
"""In-cluster service account when present, otherwise the local kubeconfig."""
|
|
26
|
+
try:
|
|
27
|
+
config.load_incluster_config()
|
|
28
|
+
logger.info("kubernetes config loaded", source="in-cluster")
|
|
29
|
+
except config.ConfigException:
|
|
30
|
+
await config.load_kube_config()
|
|
31
|
+
logger.info("kubernetes config loaded", source="kubeconfig")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class KubernetesAdapter:
|
|
36
|
+
@asynccontextmanager
|
|
37
|
+
async def api(self) -> AsyncIterator[Any]:
|
|
38
|
+
api_client: Any = client.ApiClient()
|
|
39
|
+
async with api_client as api:
|
|
40
|
+
yield api
|
|
41
|
+
|
|
42
|
+
async def get_secret_value(self, secret_ref: SecretRef) -> str:
|
|
43
|
+
async with self.api() as api:
|
|
44
|
+
try:
|
|
45
|
+
secret = await client.CoreV1Api(api).read_namespaced_secret(
|
|
46
|
+
name=secret_ref.name, namespace=secret_ref.namespace
|
|
47
|
+
)
|
|
48
|
+
except ApiException as error:
|
|
49
|
+
if error.status != 404:
|
|
50
|
+
raise
|
|
51
|
+
raise KeyError(
|
|
52
|
+
f"Secret {secret_ref.namespace}/{secret_ref.name} not found"
|
|
53
|
+
) from error
|
|
54
|
+
data: dict[str, str] = secret.data or {}
|
|
55
|
+
encoded = data.get(secret_ref.key)
|
|
56
|
+
if not encoded:
|
|
57
|
+
raise KeyError(
|
|
58
|
+
f"Key {secret_ref.key} not found in secret {secret_ref.namespace}/{secret_ref.name}"
|
|
59
|
+
)
|
|
60
|
+
return base64.b64decode(encoded).decode()
|
|
61
|
+
|
|
62
|
+
async def get_custom_resource(self, ref: ResourceRef) -> JsonDict | None:
|
|
63
|
+
async with self.api() as api:
|
|
64
|
+
try:
|
|
65
|
+
body = await client.CustomObjectsApi(api).get_namespaced_custom_object(
|
|
66
|
+
group=ref.kind.group,
|
|
67
|
+
version=ref.kind.version,
|
|
68
|
+
namespace=ref.namespace,
|
|
69
|
+
plural=ref.kind.plural,
|
|
70
|
+
name=ref.name,
|
|
71
|
+
)
|
|
72
|
+
except ApiException as error:
|
|
73
|
+
if error.status == 404:
|
|
74
|
+
return None
|
|
75
|
+
raise
|
|
76
|
+
return cast(JsonDict, body)
|
|
77
|
+
|
|
78
|
+
async def list_custom_resources(
|
|
79
|
+
self, kind: ResourceKind, namespace: str, label_selector: str | None = None
|
|
80
|
+
) -> list[JsonDict]:
|
|
81
|
+
async with self.api() as api:
|
|
82
|
+
result = await client.CustomObjectsApi(api).list_namespaced_custom_object(
|
|
83
|
+
group=kind.group,
|
|
84
|
+
version=kind.version,
|
|
85
|
+
namespace=namespace,
|
|
86
|
+
plural=kind.plural,
|
|
87
|
+
label_selector=label_selector or "",
|
|
88
|
+
)
|
|
89
|
+
return cast(list[JsonDict], cast(JsonDict, result).get("items", []))
|
|
90
|
+
|
|
91
|
+
async def create_custom_resource(self, body: JsonDict) -> JsonDict:
|
|
92
|
+
kind = ResourceKind(body["kind"], f"{body['kind'].lower()}s")
|
|
93
|
+
metadata = body["metadata"]
|
|
94
|
+
async with self.api() as api:
|
|
95
|
+
created = await client.CustomObjectsApi(api).create_namespaced_custom_object(
|
|
96
|
+
group=kind.group,
|
|
97
|
+
version=kind.version,
|
|
98
|
+
namespace=metadata["namespace"],
|
|
99
|
+
plural=kind.plural,
|
|
100
|
+
body=body,
|
|
101
|
+
)
|
|
102
|
+
logger.info("resource created", kind=body["kind"], name=metadata["name"])
|
|
103
|
+
return cast(JsonDict, created)
|
|
104
|
+
|
|
105
|
+
async def delete_custom_resource(self, ref: ResourceRef) -> None:
|
|
106
|
+
async with self.api() as api:
|
|
107
|
+
try:
|
|
108
|
+
await client.CustomObjectsApi(api).delete_namespaced_custom_object(
|
|
109
|
+
group=ref.kind.group,
|
|
110
|
+
version=ref.kind.version,
|
|
111
|
+
namespace=ref.namespace,
|
|
112
|
+
plural=ref.kind.plural,
|
|
113
|
+
name=ref.name,
|
|
114
|
+
)
|
|
115
|
+
except ApiException as error:
|
|
116
|
+
if error.status != 404:
|
|
117
|
+
raise
|
|
118
|
+
logger.info("resource deleted", kind=ref.kind.kind, name=ref.name, namespace=ref.namespace)
|
|
119
|
+
|
|
120
|
+
async def patch_status(self, ref: ResourceRef, status: JsonDict) -> None:
|
|
121
|
+
async with self.api() as api:
|
|
122
|
+
await client.CustomObjectsApi(api).patch_namespaced_custom_object_status(
|
|
123
|
+
group=ref.kind.group,
|
|
124
|
+
version=ref.kind.version,
|
|
125
|
+
namespace=ref.namespace,
|
|
126
|
+
plural=ref.kind.plural,
|
|
127
|
+
name=ref.name,
|
|
128
|
+
body={"status": status},
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
async def get_statefulset_status(self, name: str, namespace: str) -> StatefulSetStatus | None:
|
|
132
|
+
async with self.api() as api:
|
|
133
|
+
try:
|
|
134
|
+
statefulset: Any = await client.AppsV1Api(api).read_namespaced_stateful_set_status(
|
|
135
|
+
name=name, namespace=namespace
|
|
136
|
+
)
|
|
137
|
+
except ApiException as error:
|
|
138
|
+
if error.status == 404:
|
|
139
|
+
return None
|
|
140
|
+
raise
|
|
141
|
+
status = statefulset.status
|
|
142
|
+
return StatefulSetStatus(
|
|
143
|
+
replicas=status.replicas or 0, ready_replicas=status.ready_replicas or 0
|
|
144
|
+
)
|
qdrant_operator/main.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Operator entry point: routes stdlib logging (kopf) into loguru and registers the handlers."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from kopf import cli
|
|
8
|
+
from loguru import logger
|
|
9
|
+
|
|
10
|
+
import qdrant_operator.handlers as handlers
|
|
11
|
+
|
|
12
|
+
LIVENESS_URL = "http://0.0.0.0:8080/healthz"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class InterceptHandler(logging.Handler):
|
|
16
|
+
def emit(self, record: logging.LogRecord) -> None:
|
|
17
|
+
level = (
|
|
18
|
+
logger.level(record.levelname).name if record.levelname in LEVELS else record.levelno
|
|
19
|
+
)
|
|
20
|
+
logger.opt(depth=6, exception=record.exc_info).log(level, record.getMessage())
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
LEVELS = frozenset({"TRACE", "DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL"})
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def configure_logging(level: str) -> None:
|
|
27
|
+
logger.remove()
|
|
28
|
+
logger.add(sys.stderr, level=level, serialize=os.environ.get("LOG_FORMAT", "json") == "json")
|
|
29
|
+
logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
DEFAULT_ARGS = ("--all-namespaces", f"--liveness={LIVENESS_URL}")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def run(argv: list[str] | None = None) -> None:
|
|
36
|
+
"""Start the operator; extra arguments are passed to `kopf run` and override the defaults."""
|
|
37
|
+
configure_logging(os.environ.get("LOG_LEVEL", "INFO"))
|
|
38
|
+
extra = sys.argv[1:] if argv is None else argv
|
|
39
|
+
cli.main(
|
|
40
|
+
args=["run", "-m", handlers.__name__, *DEFAULT_ARGS, *extra],
|
|
41
|
+
prog_name="qdrant-operator",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
configure_logging(os.environ.get("LOG_LEVEL", "INFO"))
|
|
46
|
+
|
|
47
|
+
if __name__ == "__main__":
|
|
48
|
+
run()
|
qdrant_operator/ports.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Port interfaces (typing.Protocol) between use cases and external systems."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import AsyncIterator
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from typing import Any
|
|
6
|
+
from typing import Protocol
|
|
7
|
+
|
|
8
|
+
from qdrant_operator.domain import JsonDict
|
|
9
|
+
from qdrant_operator.domain import QdrantNode
|
|
10
|
+
from qdrant_operator.domain import ResourceKind
|
|
11
|
+
from qdrant_operator.domain import ResourceRef
|
|
12
|
+
from qdrant_operator.domain import RestorePriority
|
|
13
|
+
from qdrant_operator.domain import S3Credentials
|
|
14
|
+
from qdrant_operator.domain import S3StorageSpec
|
|
15
|
+
from qdrant_operator.domain import SecretRef
|
|
16
|
+
from qdrant_operator.domain import Snapshot
|
|
17
|
+
from qdrant_operator.domain import StatefulSetStatus
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HelmPort(Protocol):
|
|
21
|
+
async def apply(
|
|
22
|
+
self,
|
|
23
|
+
release_name: str,
|
|
24
|
+
namespace: str,
|
|
25
|
+
chart_version: str,
|
|
26
|
+
values: Mapping[str, Any],
|
|
27
|
+
) -> None:
|
|
28
|
+
"""Install or upgrade a release to the given chart version and values."""
|
|
29
|
+
...
|
|
30
|
+
|
|
31
|
+
async def uninstall(self, release_name: str, namespace: str) -> None: ...
|
|
32
|
+
|
|
33
|
+
async def release_exists(self, release_name: str, namespace: str) -> bool: ...
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class QdrantPort(Protocol):
|
|
37
|
+
async def list_collections(self, node: QdrantNode) -> list[str]: ...
|
|
38
|
+
|
|
39
|
+
async def create_snapshot(self, node: QdrantNode, collection: str) -> Snapshot: ...
|
|
40
|
+
|
|
41
|
+
def stream_snapshot(
|
|
42
|
+
self, node: QdrantNode, collection: str, snapshot_name: str
|
|
43
|
+
) -> AsyncIterator[bytes]: ...
|
|
44
|
+
|
|
45
|
+
async def delete_snapshot(
|
|
46
|
+
self, node: QdrantNode, collection: str, snapshot_name: str
|
|
47
|
+
) -> None: ...
|
|
48
|
+
|
|
49
|
+
async def recover_snapshot(
|
|
50
|
+
self,
|
|
51
|
+
node: QdrantNode,
|
|
52
|
+
collection: str,
|
|
53
|
+
location: str,
|
|
54
|
+
priority: RestorePriority,
|
|
55
|
+
checksum: str | None,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""Ask the node to fetch a snapshot from a URL and recover the collection from it."""
|
|
58
|
+
...
|
|
59
|
+
|
|
60
|
+
async def collection_info(self, node: QdrantNode, collection: str) -> JsonDict: ...
|
|
61
|
+
|
|
62
|
+
async def ready(self, node: QdrantNode) -> bool: ...
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class StoragePort(Protocol):
|
|
66
|
+
async def upload_stream(
|
|
67
|
+
self,
|
|
68
|
+
storage: S3StorageSpec,
|
|
69
|
+
credentials: S3Credentials,
|
|
70
|
+
key: str,
|
|
71
|
+
chunks: AsyncIterator[bytes],
|
|
72
|
+
) -> int:
|
|
73
|
+
"""Stream chunks into an object. Returns the number of bytes written."""
|
|
74
|
+
...
|
|
75
|
+
|
|
76
|
+
async def put_object(
|
|
77
|
+
self, storage: S3StorageSpec, credentials: S3Credentials, key: str, data: bytes
|
|
78
|
+
) -> None: ...
|
|
79
|
+
|
|
80
|
+
async def get_object(
|
|
81
|
+
self, storage: S3StorageSpec, credentials: S3Credentials, key: str
|
|
82
|
+
) -> bytes: ...
|
|
83
|
+
|
|
84
|
+
async def delete_prefix(
|
|
85
|
+
self, storage: S3StorageSpec, credentials: S3Credentials, prefix: str
|
|
86
|
+
) -> int:
|
|
87
|
+
"""Delete every object under a prefix. Returns how many were deleted."""
|
|
88
|
+
...
|
|
89
|
+
|
|
90
|
+
async def presigned_get_url(
|
|
91
|
+
self,
|
|
92
|
+
storage: S3StorageSpec,
|
|
93
|
+
credentials: S3Credentials,
|
|
94
|
+
key: str,
|
|
95
|
+
expires_seconds: int,
|
|
96
|
+
) -> str: ...
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class KubernetesPort(Protocol):
|
|
100
|
+
async def get_secret_value(self, secret_ref: SecretRef) -> str: ...
|
|
101
|
+
|
|
102
|
+
async def get_custom_resource(self, ref: ResourceRef) -> JsonDict | None: ...
|
|
103
|
+
|
|
104
|
+
async def list_custom_resources(
|
|
105
|
+
self, kind: ResourceKind, namespace: str, label_selector: str | None = None
|
|
106
|
+
) -> list[JsonDict]: ...
|
|
107
|
+
|
|
108
|
+
async def create_custom_resource(self, body: JsonDict) -> JsonDict: ...
|
|
109
|
+
|
|
110
|
+
async def delete_custom_resource(self, ref: ResourceRef) -> None: ...
|
|
111
|
+
|
|
112
|
+
async def patch_status(self, ref: ResourceRef, status: JsonDict) -> None: ...
|
|
113
|
+
|
|
114
|
+
async def get_statefulset_status(
|
|
115
|
+
self, name: str, namespace: str
|
|
116
|
+
) -> StatefulSetStatus | None: ...
|
qdrant_operator/py.typed
ADDED
|
File without changes
|