|
7 | 7 | import os
|
8 | 8 | import threading
|
9 | 9 | import weakref
|
10 |
| -from typing import Dict, List, Optional, Tuple |
| 10 | +from typing import Optional, Tuple |
11 | 11 |
|
12 | 12 | import urllib3
|
13 | 13 | import kubernetes
|
@@ -48,7 +48,7 @@ def get_return_type(self, func):
|
48 | 48 | return None
|
49 | 49 |
|
50 | 50 |
|
51 |
| -def median(values: List[float]) -> float: |
| 51 | +def median(values: list[float]) -> float: |
52 | 52 | if len(values) == 0:
|
53 | 53 | return 0
|
54 | 54 | return values[len(values)//2]
|
@@ -149,15 +149,15 @@ def __init__(self, logger, namespace, prefix, priority, cpu_reservation, labels=
|
149 | 149 | self.cpu_reservation: float = max(0.0, min(cpu_reservation, 1.0))
|
150 | 150 | self.logger = logger
|
151 | 151 | self.log_level: str = log_level
|
152 |
| - self._labels: Dict[str, str] = labels or {} |
| 152 | + self._labels: dict[str, str] = labels or {} |
153 | 153 | self.apps_api = client.AppsV1Api()
|
154 | 154 | self.api = client.CoreV1Api()
|
155 | 155 | self.net_api = client.NetworkingV1Api()
|
156 | 156 | self.namespace: str = namespace
|
157 |
| - self.config_volumes: Dict[str, V1Volume] = {} |
158 |
| - self.config_mounts: Dict[str, V1VolumeMount] = {} |
159 |
| - self.core_config_volumes: Dict[str, V1Volume] = {} |
160 |
| - self.core_config_mounts: Dict[str, V1VolumeMount] = {} |
| 157 | + self.config_volumes: dict[str, V1Volume] = {} |
| 158 | + self.config_mounts: dict[str, V1VolumeMount] = {} |
| 159 | + self.core_config_volumes: dict[str, V1Volume] = {} |
| 160 | + self.core_config_mounts: dict[str, V1VolumeMount] = {} |
161 | 161 | self._external_profiles = weakref.WeakValueDictionary()
|
162 | 162 | self._service_limited_env: dict[str, dict[str, str]] = defaultdict(dict)
|
163 | 163 |
|
@@ -191,7 +191,7 @@ def __init__(self, logger, namespace, prefix, priority, cpu_reservation, labels=
|
191 | 191 | pod_background = threading.Thread(target=self._loop_forever(self._monitor_pods), daemon=True)
|
192 | 192 | pod_background.start()
|
193 | 193 |
|
194 |
| - self._deployment_targets: Dict[str, int] = {} |
| 194 | + self._deployment_targets: dict[str, int] = {} |
195 | 195 | deployment_background = threading.Thread(target=self._loop_forever(self._monitor_deployments), daemon=True)
|
196 | 196 | deployment_background.start()
|
197 | 197 |
|
@@ -434,7 +434,7 @@ def memory_info(self):
|
434 | 434 | return self._node_pool_max_ram - self._pod_used_ram, self._node_pool_max_ram
|
435 | 435 |
|
436 | 436 | @staticmethod
|
437 |
| - def _create_metadata(deployment_name: str, labels: Dict[str, str]): |
| 437 | + def _create_metadata(deployment_name: str, labels: dict[str, str]): |
438 | 438 | return V1ObjectMeta(name=deployment_name, labels=labels)
|
439 | 439 |
|
440 | 440 | def _create_volumes(self, core_mounts=False):
|
@@ -585,7 +585,7 @@ def get_target(self, service_name: str) -> int:
|
585 | 585 | """Get the target for running instances of a service."""
|
586 | 586 | return self._deployment_targets.get(service_name, 0)
|
587 | 587 |
|
588 |
| - def get_targets(self) -> Dict[str, int]: |
| 588 | + def get_targets(self) -> dict[str, int]: |
589 | 589 | """Get the target for running instances of all services."""
|
590 | 590 | return self._deployment_targets
|
591 | 591 |
|
@@ -674,8 +674,20 @@ def start_stateful_container(self, service_name: str, container_name: str,
|
674 | 674 | ))
|
675 | 675 | mounts.append(V1VolumeMount(mount_path=volume_spec.mount_path, name=mount_name))
|
676 | 676 |
|
| 677 | + # Read the key being used for the deployment instance or generate a new one |
| 678 | + try: |
| 679 | + instance_key = uuid.uuid4().hex |
| 680 | + old_deployment = self.apps_api.read_namespaced_deployment(deployment_name, self.namespace) |
| 681 | + for container in old_deployment.spec.template.spec.containers: |
| 682 | + for env in container.env: |
| 683 | + if env.name == 'AL_INSTANCE_KEY': |
| 684 | + instance_key = env.value |
| 685 | + break |
| 686 | + except ApiException as error: |
| 687 | + if error.status != 404: |
| 688 | + raise |
| 689 | + |
677 | 690 | # Setup the deployment itself
|
678 |
| - instance_key = uuid.uuid4().hex |
679 | 691 | labels['container'] = container_name
|
680 | 692 | spec.container.environment.append({'name': 'AL_INSTANCE_KEY', 'value': instance_key})
|
681 | 693 | self._create_deployment(service_name, deployment_name, spec.container,
|
|
0 commit comments