feat: Add name and type fields to instance class

The `DockerController`, `SwarmController` and `IngressController` classes have been updated to include a `type` field in the instance objects. This field indicates whether the instance is a static one, a container or a pod. This change provides additional information about the type of instances being managed by the controllers.
This commit is contained in:
Théophile Diot 2024-08-07 13:44:25 +01:00
parent 6ed8c2bdf8
commit e1bd4a6313
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
5 changed files with 21 additions and 12 deletions

View file

@ -45,14 +45,15 @@ class DockerController(Controller):
]
def _to_instances(self, controller_instance) -> List[dict]:
instance = {}
instance["name"] = controller_instance.name
instance["hostname"] = controller_instance.name
instance["health"] = controller_instance.status == "running" and controller_instance.attrs["State"]["Health"]["Status"] == "healthy"
instance["env"] = {}
instance = {
"name": controller_instance.name,
"hostname": controller_instance.name,
"type": "container",
"health": controller_instance.status == "running" and controller_instance.attrs["State"]["Health"]["Status"] == "healthy",
"env": {},
}
for env in controller_instance.attrs["Config"]["Env"]:
variable = env.split("=")[0]
value = env.replace(f"{variable}=", "", 1)
variable, value = env.split("=", 1)
instance["env"][variable] = value
return [instance]

View file

@ -43,6 +43,7 @@ class IngressController(Controller):
instance = {
"name": controller_instance.metadata.name,
"hostname": controller_instance.metadata.name,
"type": "pod",
}
health = False
if controller_instance.status.conditions:

View file

@ -48,8 +48,7 @@ class SwarmController(Controller):
instances = []
instance_env = {}
for env in controller_instance.attrs["Spec"]["TaskTemplate"]["ContainerSpec"]["Env"]:
variable = env.split("=")[0]
value = env.replace(f"{variable}=", "", 1)
variable, value = env.split("=", 1)
instance_env[variable] = value
for task in controller_instance.tasks():
@ -59,6 +58,7 @@ class SwarmController(Controller):
{
"name": task["ID"],
"hostname": f"{controller_instance.name}.{task['NodeID']}.{task['ID']}",
"type": "container",
"health": task["Status"]["State"] == "running",
"env": instance_env,
}

View file

@ -3069,7 +3069,7 @@ class Database:
)
return cache_files
def add_instance(self, hostname: str, port: int, server_name: str, method: str, changed: Optional[bool] = True) -> str:
def add_instance(self, hostname: str, port: int, server_name: str, method: str, changed: Optional[bool] = True, *, name: Optional[str] = None) -> str:
"""Add instance."""
with self._db_session() as session:
if self.readonly:
@ -3080,7 +3080,7 @@ class Database:
if db_instance is not None:
return f"Instance {hostname} already exists, will not be added."
session.add(Instances(hostname=hostname, port=port, server_name=server_name, method=method))
session.add(Instances(hostname=hostname, name=name or "static instance", port=port, server_name=server_name, method=method))
if changed:
with suppress(ProgrammingError, OperationalError):
@ -3112,8 +3112,10 @@ class Database:
to_put.append(
Instances(
hostname=instance["hostname"],
name=instance.get("name", "static instance"),
port=instance["env"].get("API_HTTP_PORT", 5000),
server_name=instance["env"].get("API_SERVER_NAME", "bwapi"),
type=instance.get("type", "static"),
status="up" if instance.get("health", True) else "down",
method=method,
)
@ -3144,8 +3146,10 @@ class Database:
return [
{
"hostname": instance.hostname,
"name": instance.name,
"port": instance.port,
"server_name": instance.server_name,
"type": instance.type,
"status": instance.status,
"method": instance.method,
"creation_date": instance.creation_date,

View file

@ -36,6 +36,7 @@ INTEGRATIONS_ENUM = Enum(
STREAM_TYPES_ENUM = Enum("no", "yes", "partial", name="stream_types_enum")
PLUGIN_TYPES_ENUM = Enum("core", "external", "pro", name="plugin_types_enum")
PRO_STATUS_ENUM = Enum("active", "invalid", "expired", "suspended", name="pro_status_enum")
INSTANCE_TYPE_ENUM = Enum("static", "container", "pod", name="instance_type_enum")
INSTANCE_STATUS_ENUM = Enum("loading", "up", "down", name="instance_status_enum")
Base = declarative_base()
@ -200,9 +201,11 @@ class Instances(Base):
__tablename__ = "bw_instances"
hostname = Column(String(256), primary_key=True)
name = Column(String(256), nullable=False, default="static instance")
port = Column(Integer, nullable=False)
server_name = Column(String(256), nullable=False)
status = Column(INSTANCE_STATUS_ENUM, nullable=True, default="loading")
type = Column(INSTANCE_TYPE_ENUM, nullable=False, default="static")
status = Column(INSTANCE_STATUS_ENUM, nullable=False, default="loading")
method = Column(METHODS_ENUM, nullable=False, default="manual")
creation_date = Column(DateTime, nullable=False, server_default=func.now())
last_seen = Column(DateTime, nullable=True, server_default=func.now(), onupdate=partial(datetime.now, timezone.utc))