autoconf - force updating first configuration

This commit is contained in:
florian 2023-08-22 17:07:04 +02:00
parent 3d13cf345e
commit 9feb66710b
No known key found for this signature in database
GPG key ID: 3D80806F12602A7C
5 changed files with 24 additions and 19 deletions

View file

@ -16,7 +16,18 @@ class Config(ConfigCaller):
self.__logger = setup_logger("Config", getenv("LOG_LEVEL", "INFO"))
self.__instances = []
self.__services = []
self.__configs = {}
self._supported_config_types = [
"http",
"stream",
"server-http",
"server-stream",
"default-server-http",
"modsec",
"modsec-crs",
]
self.__configs = {
config_type: {} for config_type in self._supported_config_types
}
self.__config = {}
self._db = Database(self.__logger)
@ -44,7 +55,7 @@ class Config(ConfigCaller):
return True
return False
def apply(self, instances, services, configs={}) -> bool:
def apply(self, instances, services, configs={}, first=False) -> bool:
success = True
# update types
@ -56,21 +67,21 @@ class Config(ConfigCaller):
}
changes = []
if instances != self.__instances:
if instances != self.__instances or first:
self.__instances = instances
updates["instances"] = True
changes.append("instances")
if services != self.__services:
if services != self.__services or first:
self.__services = services
updates["services"] = True
if configs != self.__configs:
if configs != self.__configs or first:
self.__configs = configs
updates["configs"] = True
changes.append("custom_configs")
if updates["instances"] or updates["services"]:
old_env = self.__get_full_env()
self.__config = self.__get_full_env()
if self.__config != old_env:
if self.__config != old_env or first:
updates["config"] = True
changes.append("config")

View file

@ -16,18 +16,10 @@ class Controller(Config):
ctrl_type: Union[Literal["docker"], Literal["swarm"], Literal["kubernetes"]],
):
super().__init__()
self._loaded = False
self._type = ctrl_type
self._instances = []
self._services = []
self._supported_config_types = [
"http",
"stream",
"server-http",
"server-stream",
"default-server-http",
"modsec",
"modsec-crs",
]
self._configs = {
config_type: {} for config_type in self._supported_config_types
}
@ -83,12 +75,14 @@ class Controller(Config):
pass
def _set_autoconf_load_db(self):
if not self._db.is_autoconf_loaded():
if not self._loaded:
ret = self._db.set_autoconf_load(True)
if ret:
self._logger.warning(
f"Can't set autoconf loaded metadata to true in database: {ret}",
)
else:
self._loaded = True
def get_services(self):
services = []

View file

@ -106,7 +106,7 @@ class DockerController(Controller):
return configs
def apply_config(self) -> bool:
return self.apply(self._instances, self._services, configs=self._configs)
return self.apply(self._instances, self._services, configs=self._configs, first=not self._loaded)
def process_events(self):
self._set_autoconf_load_db()

View file

@ -298,7 +298,7 @@ class IngressController(Controller):
sleep(10)
def apply_config(self) -> bool:
return self.apply(self._instances, self._services, configs=self._configs)
return self.apply(self._instances, self._services, configs=self._configs, first=not self._loaded)
def process_events(self):
self._set_autoconf_load_db()

View file

@ -127,7 +127,7 @@ class SwarmController(Controller):
return configs
def apply_config(self) -> bool:
return self.apply(self._instances, self._services, configs=self._configs)
return self.apply(self._instances, self._services, configs=self._configs, first=not self._loaded)
def __event(self, event_type):
while True: