Files
2026-01-05 12:44:28 +00:00

68 lines
2.6 KiB
Python

"""
Web Service Configuration
"""
import os
from typing import Optional
from pydantic import BaseModel, Field
class AKSConfig(BaseModel):
"""AKS配置"""
subscription_id: Optional[str] = Field(None, description="Azure订阅ID")
resource_group: Optional[str] = Field(None, description="资源组名称")
cluster_name: Optional[str] = Field(None, description="AKS集群名称")
default_namespace: str = Field("default", description="默认命名空间")
use_local_kubeconfig: bool = Field(False, description="使用本地kubeconfig")
class ServiceConfig(BaseModel):
"""服务配置"""
host: str = Field("0.0.0.0", description="服务主机")
port: int = Field(8000, description="服务端口")
workers: int = Field(1, description="工作进程数")
# AKS配置
aks_config: AKSConfig
# 功能开关
enable_quota_management: bool = Field(True, description="启用配额管理")
enable_lifecycle_management: bool = Field(True, description="启用生命周期管理")
enable_retry_mechanism: bool = Field(True, description="启用重试机制")
enable_metering: bool = Field(True, description="启用计量")
# 清理间隔
cleanup_interval: int = Field(60, description="清理间隔(秒)")
def validate(self):
"""验证配置"""
if not self.aks_config.use_local_kubeconfig:
if not all([
self.aks_config.subscription_id,
self.aks_config.resource_group,
self.aks_config.cluster_name
]):
raise ValueError(
"使用Azure API时,必须提供 subscription_id, resource_group, cluster_name"
)
# 从环境变量加载配置
config = ServiceConfig(
host=os.getenv("HOST", "0.0.0.0"),
port=int(os.getenv("PORT", "8000")),
workers=int(os.getenv("WORKERS", "1")),
aks_config=AKSConfig(
subscription_id=os.getenv("AZURE_SUBSCRIPTION_ID"),
resource_group=os.getenv("AZURE_RESOURCE_GROUP"),
cluster_name=os.getenv("AKS_CLUSTER_NAME"),
default_namespace=os.getenv("DEFAULT_NAMESPACE", "default"),
use_local_kubeconfig=os.getenv("USE_LOCAL_KUBECONFIG", "false").lower() == "true"
),
enable_quota_management=os.getenv("ENABLE_QUOTA_MANAGEMENT", "true").lower() == "true",
enable_lifecycle_management=os.getenv("ENABLE_LIFECYCLE_MANAGEMENT", "true").lower() == "true",
enable_retry_mechanism=os.getenv("ENABLE_RETRY_MECHANISM", "true").lower() == "true",
enable_metering=os.getenv("ENABLE_METERING", "true").lower() == "true",
cleanup_interval=int(os.getenv("CLEANUP_INTERVAL", "60"))
)