Skip to content

Commit

Permalink
autoconf - filter swarm events to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0ppy-d1sk committed Jan 11, 2024
1 parent e5ba468 commit 1cd587e
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/autoconf/SwarmController.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ def __init__(self, docker_host):
super().__init__("swarm")
self.__client = DockerClient(base_url=docker_host)
self.__internal_lock = Lock()
self.__swarm_instances = []
self.__swarm_services = []
self.__swarm_configs = []

def _get_controller_instances(self) -> List[Service]:
self.__swarm_instances = []
return self.__client.services.list(filters={"label": "bunkerweb.INSTANCE"})

def _get_controller_services(self) -> List[Service]:
self.__swarm_services = []
return self.__client.services.list(filters={"label": "bunkerweb.SERVER_NAME"})

def _to_instances(self, controller_instance) -> List[dict]:
self.__swarm_instances.append(controller_instance.id)
instances = []
instance_env = {}
for env in controller_instance.attrs["Spec"]["TaskTemplate"]["ContainerSpec"]["Env"]:
Expand All @@ -46,6 +52,7 @@ def _to_instances(self, controller_instance) -> List[dict]:
return instances

def _to_services(self, controller_service) -> List[dict]:
self.__swarm_services.append(controller_service.id)
service = {}
for variable, value in controller_service.attrs["Spec"]["Labels"].items():
if not variable.startswith("bunkerweb."):
Expand Down Expand Up @@ -80,6 +87,7 @@ def _get_static_services(self) -> List[dict]:
return services

def get_configs(self) -> Dict[str, Dict[str, Any]]:
self.__swarm_configs = []
configs = {}
for config_type in self._supported_config_types:
configs[config_type] = {}
Expand All @@ -103,6 +111,7 @@ def get_configs(self) -> Dict[str, Dict[str, Any]]:
continue
config_site = f"{config.attrs['Spec']['Labels']['bunkerweb.CONFIG_SITE']}/"
configs[config_type][f"{config_site}{config_name}"] = b64decode(config.attrs["Spec"]["Data"])
self.__swarm_configs.append(config.id)
return configs

def apply_config(self) -> bool:
Expand All @@ -114,9 +123,26 @@ def apply_config(self) -> bool:
)

def __process_event(self, event):
# TODO : dynamicaly retrieve labels from object ID
return True
#return "Actor" in event and "Attributes" in event["Actor"] and "Spec" in event["Actor"]["Attributes"] and "Labels" in event["Actor"]["Attributes"]["Spec"] and ("bunkerweb.INSTANCE" in event["Actor"]["Attributes"]["Spec"]["Labels"] or "bunkerweb.CONFIG_TYPE" in event["Actor"]["Attributes"]["Spec"]["Labels"])
if "Actor" not in event or "ID" not in event["Actor"] or "Type" not in event:
return False
if event["Type"] not in ["service", "config"]:
return False
if event["Type"] == "service":
if event["Actor"]["ID"] in self.__swarm_instances or event["Actor"]["ID"] in self.__swarm_services:
return True
try:
labels = self.__client.services.get(event["Actor"]["ID"]).attrs["Spec"]["Labels"]
return "bunkerweb.INSTANCE" in labels or "bunkerweb.SERVER_NAME" in labels
except:
return False
if event["Type"] == "config":
if event["Actor"]["ID"] in self.__swarm_configs:
return True
try:
return "bunkerweb.CONFIG_TYPE" in self.__client.configs.get(event["Actor"]["ID"]).attrs["Spec"]["Labels"]
except:
return False
return False

def __event(self, event_type):
while True:
Expand Down

0 comments on commit 1cd587e

Please sign in to comment.