feat(monitor): K8s Monitor 从 API 动态加载项目映射,移除硬编码
Some checks failed
Build and Deploy Log Center / build-and-deploy (push) Failing after 1m7s
Some checks failed
Build and Deploy Log Center / build-and-deploy (push) Failing after 1m7s
monitor 启动时调用 GET /api/v1/projects 拉取项目列表, 自动生成 app label -> project_id 映射(下划线转短横线 + -dev 变体), 新项目只需在 Log Center 注册即可自动纳入 K8s 监控。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ac73c6cd31
commit
874c873de9
@ -636,23 +636,21 @@ jobs:
|
|||||||
| `CreateContainerConfigError` | 容器配置错误 |
|
| `CreateContainerConfigError` | 容器配置错误 |
|
||||||
| `RunContainerError` | 容器启动失败 |
|
| `RunContainerError` | 容器启动失败 |
|
||||||
|
|
||||||
### 接入方式:添加 Pod label 映射
|
### 接入方式:自动映射
|
||||||
|
|
||||||
K8s Monitor CronJob 已在集群中运行,每 5 分钟扫描一次。新项目接入只需在 `k8s-monitor/monitor.py` 的 `APP_TO_PROJECT` 字典中添加映射:
|
K8s Monitor CronJob 已在集群中运行,每 5 分钟扫描一次。Monitor 启动时会从 Log Center API(`GET /api/v1/projects`)动态拉取项目列表,自动生成 app label -> project_id 的映射。
|
||||||
|
|
||||||
```python
|
**映射规则**:`project_id` 中的下划线替换为短横线作为 app label,同时生成 `-dev` 后缀变体。
|
||||||
# k8s-monitor/monitor.py
|
|
||||||
APP_TO_PROJECT = {
|
| project_id | 自动生成的 app label |
|
||||||
"rtc-backend": "rtc_backend", # Pod 的 app label -> project_id
|
|---|---|
|
||||||
"rtc-backend-dev": "rtc_backend",
|
| `rtc_backend` | `rtc-backend`, `rtc-backend-dev` |
|
||||||
"rtc-web": "rtc_web",
|
| `rtc_web` | `rtc-web`, `rtc-web-dev` |
|
||||||
"rtc-web-dev": "rtc_web",
|
| `log_center_api` | `log-center-api`, `log-center-api-dev` |
|
||||||
"log-center-api": "log_center_api",
|
|
||||||
"log-center-web": "log_center_web",
|
**新项目接入 K8s 监控只需两步**:
|
||||||
# 新项目在此添加映射
|
1. 在步骤 1 中完成项目注册(确保项目出现在 Log Center 项目列表中)
|
||||||
"your-app": "your_project_id",
|
2. K8s Deployment 的 `app` label 使用 `project_id` 的短横线形式
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
确保你的 K8s Deployment 有 `app` label:
|
确保你的 K8s Deployment 有 `app` label:
|
||||||
|
|
||||||
|
|||||||
@ -22,19 +22,38 @@ ABNORMAL_STATES = {
|
|||||||
"RunContainerError",
|
"RunContainerError",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Pod label app -> project_id 映射
|
|
||||||
APP_TO_PROJECT = {
|
|
||||||
"rtc-backend": "rtc_backend",
|
|
||||||
"rtc-backend-dev": "rtc_backend",
|
|
||||||
"rtc-web": "rtc_web",
|
|
||||||
"rtc-web-dev": "rtc_web",
|
|
||||||
"log-center-api": "log_center",
|
|
||||||
"log-center-web": "log_center",
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# 动态映射表,启动时从 API 加载
|
||||||
|
APP_TO_PROJECT: dict[str, str] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def load_project_mapping():
|
||||||
|
"""从 Log Center API 拉取项目列表,自动生成 app label -> project_id 映射。
|
||||||
|
|
||||||
|
映射规则:project_id 中的下划线替换为短横线作为 app label,
|
||||||
|
同时生成 -dev 后缀的变体用于开发环境。
|
||||||
|
例如 project_id="rtc_backend" -> app labels: "rtc-backend", "rtc-backend-dev"
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
resp = requests.get(f"{LOG_CENTER_URL}/api/v1/projects", timeout=10)
|
||||||
|
resp.raise_for_status()
|
||||||
|
projects = resp.json().get("projects", [])
|
||||||
|
|
||||||
|
mapping = {}
|
||||||
|
for project in projects:
|
||||||
|
pid = project["project_id"]
|
||||||
|
# project_id 下划线转短横线作为 app label
|
||||||
|
app_label = pid.replace("_", "-")
|
||||||
|
mapping[app_label] = pid
|
||||||
|
mapping[f"{app_label}-dev"] = pid
|
||||||
|
|
||||||
|
APP_TO_PROJECT.update(mapping)
|
||||||
|
logger.info(f"从 API 加载了 {len(projects)} 个项目,生成 {len(mapping)} 条映射")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"从 API 加载项目映射失败: {e},将使用 app label 原值作为 project_id")
|
||||||
|
|
||||||
|
|
||||||
def get_project_id(pod_labels: dict) -> str:
|
def get_project_id(pod_labels: dict) -> str:
|
||||||
app_name = pod_labels.get("app", "unknown")
|
app_name = pod_labels.get("app", "unknown")
|
||||||
@ -132,6 +151,9 @@ def report_error(error: dict, logs: str):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# 从 Log Center API 动态加载项目映射
|
||||||
|
load_project_mapping()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config.load_incluster_config()
|
config.load_incluster_config()
|
||||||
except config.ConfigException:
|
except config.ConfigException:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user