- Implemented Pydantic models for Kubernetes cluster state representation in `cluster.py`. - Created a `Resource` class for converting JSON/dict to Python objects in `resource.py`. - Established user models and services for user management, including password hashing and JWT token generation. - Developed application orchestration services for managing Kubernetes applications, including installation and uninstallation. - Added cluster service for retrieving cluster status and health reports. - Introduced node service for fetching node resource details and health status. - Implemented user service for handling user authentication and management.
71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
# service.py
|
||
"""
|
||
Service (服务) 层 - 应用编排。
|
||
负责处理核心业务逻辑(如权限、命名),并调用 DAO 层来执行数据操作。
|
||
"""
|
||
import ulid
|
||
|
||
from ocdp.orchestration.cluster import Cluster
|
||
from ocdp.daos.orchestration import application_dao as dao
|
||
from ocdp.models.orchestration.application import (ApplicationTemplate, InstallReceipt, ApplicationStatus,
|
||
InstalledApplicationInstance, UninstallReceipt, NamespaceDeleteReceipt, ApplicationMetadata)
|
||
|
||
# ... (list_available_applications, list_user_applications 保持不变) ...
|
||
def list_available_applications(cluster: Cluster) -> list[ApplicationTemplate]:
|
||
"""(Service) 获取所有可供安装的应用模板列表。"""
|
||
return dao.list_application_templates(cluster)
|
||
def list_user_applications(cluster: Cluster, user_id: str) -> list[InstalledApplicationInstance]:
|
||
"""(Service) 获取指定用户已经安装的应用实例列表。"""
|
||
return dao.list_application_instances(cluster, user_id)
|
||
|
||
def install_new_application(
|
||
cluster: Cluster,
|
||
user_id: str,
|
||
app_template_name: str,
|
||
mode: str,
|
||
user_overrides: dict | None = None
|
||
) -> InstallReceipt:
|
||
"""
|
||
(Service) 触发一个新应用的安装。
|
||
核心职责:根据业务规则生成唯一的命名空间。
|
||
"""
|
||
# 1. (Service 职责) 获取应用的业务名称,用于构造命名空间
|
||
# 这里通过调用一次 get_application_metadata 来获取,但只为了 application_name
|
||
# DAO 层为了执行任务,也会自己获取一次
|
||
metadata = cluster.get_application_metadata(app_template_name)
|
||
application_name = metadata.get("application_name", app_template_name)
|
||
|
||
# 2. (Service 职责) 生成唯一的命名空间
|
||
instance_id = str(ulid.new()).lower()
|
||
namespace = f"{user_id}-{application_name}-{instance_id}"
|
||
|
||
# 3. (Service 职责) 将所有参数(包括生成的namespace)传递给 DAO 层执行
|
||
return dao.install_application(
|
||
cluster=cluster,
|
||
namespace=namespace,
|
||
app_template_name=app_template_name,
|
||
mode=mode,
|
||
user_overrides=user_overrides
|
||
)
|
||
|
||
def get_instance_status(
|
||
cluster: Cluster,
|
||
namespace: str,
|
||
app_template_name: str,
|
||
mode: str
|
||
) -> ApplicationStatus:
|
||
"""(Service) 获取指定应用实例的详细状态。"""
|
||
return dao.get_application_status(cluster, namespace, app_template_name, mode)
|
||
|
||
def uninstall_application_release(
|
||
cluster: Cluster,
|
||
namespace: str,
|
||
app_template_name: str,
|
||
mode: str
|
||
) -> UninstallReceipt:
|
||
"""(Service) 卸载应用实例 (Helm Release)。"""
|
||
return dao.uninstall_application_release(cluster, namespace, app_template_name, mode)
|
||
|
||
def delete_application_namespace(cluster: Cluster, namespace: str) -> NamespaceDeleteReceipt:
|
||
"""(Service) 删除应用实例的命名空间。"""
|
||
return dao.delete_namespace(cluster, namespace) |