Skip to content

Commit 0d01b05

Browse files
Merge pull request #295 from CybercentreCanada/persistent-service-update
Persistent service update (dev)
2 parents fd87423 + e7ae18e commit 0d01b05

File tree

5 files changed

+158
-119
lines changed

5 files changed

+158
-119
lines changed

assemblyline_core/scaler/controllers/docker_ctl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def memory_info(self):
243243
self.log.debug(f'Total Memory available {mem}/{self._info["MemTotal"]/mega}')
244244
return mem, total_mem
245245

246-
def get_target(self, service_name):
246+
def get_target(self, service_name: str) -> int:
247247
"""Get how many instances of a service we expect to be running.
248248
249249
Since we start our containers with 'restart always' we just need to count how many

assemblyline_core/scaler/controllers/interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def cpu_info(self):
2424
"""Return free and total memory in the system."""
2525
raise NotImplementedError()
2626

27-
def free_cpu(self):
27+
def free_cpu(self) -> float:
2828
"""Number of cores available for reservation."""
2929
return self.cpu_info()[0]
3030

31-
def free_memory(self):
31+
def free_memory(self) -> float:
3232
"""Megabytes of RAM that has not been reserved."""
3333
return self.memory_info()[0]
3434

assemblyline_core/scaler/controllers/kubernetes_ctl.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import threading
99
import weakref
10-
from typing import Dict, List, Optional, Tuple
10+
from typing import Optional, Tuple
1111

1212
import urllib3
1313
import kubernetes
@@ -48,7 +48,7 @@ def get_return_type(self, func):
4848
return None
4949

5050

51-
def median(values: List[float]) -> float:
51+
def median(values: list[float]) -> float:
5252
if len(values) == 0:
5353
return 0
5454
return values[len(values)//2]
@@ -149,15 +149,15 @@ def __init__(self, logger, namespace, prefix, priority, cpu_reservation, labels=
149149
self.cpu_reservation: float = max(0.0, min(cpu_reservation, 1.0))
150150
self.logger = logger
151151
self.log_level: str = log_level
152-
self._labels: Dict[str, str] = labels or {}
152+
self._labels: dict[str, str] = labels or {}
153153
self.apps_api = client.AppsV1Api()
154154
self.api = client.CoreV1Api()
155155
self.net_api = client.NetworkingV1Api()
156156
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] = {}
161161
self._external_profiles = weakref.WeakValueDictionary()
162162
self._service_limited_env: dict[str, dict[str, str]] = defaultdict(dict)
163163

@@ -191,7 +191,7 @@ def __init__(self, logger, namespace, prefix, priority, cpu_reservation, labels=
191191
pod_background = threading.Thread(target=self._loop_forever(self._monitor_pods), daemon=True)
192192
pod_background.start()
193193

194-
self._deployment_targets: Dict[str, int] = {}
194+
self._deployment_targets: dict[str, int] = {}
195195
deployment_background = threading.Thread(target=self._loop_forever(self._monitor_deployments), daemon=True)
196196
deployment_background.start()
197197

@@ -434,7 +434,7 @@ def memory_info(self):
434434
return self._node_pool_max_ram - self._pod_used_ram, self._node_pool_max_ram
435435

436436
@staticmethod
437-
def _create_metadata(deployment_name: str, labels: Dict[str, str]):
437+
def _create_metadata(deployment_name: str, labels: dict[str, str]):
438438
return V1ObjectMeta(name=deployment_name, labels=labels)
439439

440440
def _create_volumes(self, core_mounts=False):
@@ -585,7 +585,7 @@ def get_target(self, service_name: str) -> int:
585585
"""Get the target for running instances of a service."""
586586
return self._deployment_targets.get(service_name, 0)
587587

588-
def get_targets(self) -> Dict[str, int]:
588+
def get_targets(self) -> dict[str, int]:
589589
"""Get the target for running instances of all services."""
590590
return self._deployment_targets
591591

@@ -674,8 +674,20 @@ def start_stateful_container(self, service_name: str, container_name: str,
674674
))
675675
mounts.append(V1VolumeMount(mount_path=volume_spec.mount_path, name=mount_name))
676676

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+
677690
# Setup the deployment itself
678-
instance_key = uuid.uuid4().hex
679691
labels['container'] = container_name
680692
spec.container.environment.append({'name': 'AL_INSTANCE_KEY', 'value': instance_key})
681693
self._create_deployment(service_name, deployment_name, spec.container,

0 commit comments

Comments
 (0)