Skip to content

Commit

Permalink
feat: Add name and type fields to instance class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheophileDiot committed Aug 7, 2024
1 parent 6ed8c2b commit e1bd4a6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/autoconf/DockerController.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ def _get_controller_services(self) -> List[Container]:
]

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]

Expand Down
1 change: 1 addition & 0 deletions src/autoconf/IngressController.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _to_instances(self, controller_instance) -> List[dict]:
instance = {
"name": controller_instance.metadata.name,
"hostname": controller_instance.metadata.name,
"type": "pod",
}
health = False
if controller_instance.status.conditions:
Expand Down
4 changes: 2 additions & 2 deletions src/autoconf/SwarmController.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def _to_instances(self, controller_instance) -> List[dict]:
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():
Expand All @@ -59,6 +58,7 @@ def _to_instances(self, controller_instance) -> List[dict]:
{
"name": task["ID"],
"hostname": f"{controller_instance.name}.{task['NodeID']}.{task['ID']}",
"type": "container",
"health": task["Status"]["State"] == "running",
"env": instance_env,
}
Expand Down
8 changes: 6 additions & 2 deletions src/common/db/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ def get_jobs_cache_files(self, *, job_name: str = "", plugin_id: str = "") -> Li
)
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:
Expand All @@ -3080,7 +3080,7 @@ def add_instance(self, hostname: str, port: int, server_name: str, method: str,
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):
Expand Down Expand Up @@ -3112,8 +3112,10 @@ def update_instances(self, instances: List[Dict[str, Any]], method: str, changed
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,
)
Expand Down Expand Up @@ -3144,8 +3146,10 @@ def get_instances(self, *, method: Optional[str] = None) -> List[Dict[str, Any]]
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,
Expand Down
5 changes: 4 additions & 1 deletion src/common/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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()

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit e1bd4a6

Please sign in to comment.