fix(aiops): block k8s playbooks for host repair
All checks were successful
CD Pipeline / tests (push) Successful in 1m27s
Code Review / ai-code-review (push) Successful in 26s
CD Pipeline / build-and-deploy (push) Successful in 8m6s
CD Pipeline / post-deploy-checks (push) Successful in 3m31s

This commit is contained in:
Your Name
2026-05-01 10:33:28 +08:00
parent 7472eb2fcd
commit e4aef6ac4e
3 changed files with 105 additions and 2 deletions

View File

@@ -337,6 +337,23 @@ class AutoRepairService:
blocked_by="NOT_APPROVED",
)
if self._is_host_or_backup_incident(incident) and self._playbook_has_k8s_steps(best_match.playbook):
logger.warning(
"auto_repair_blocked_host_backup_k8s_playbook",
incident_id=incident.incident_id,
playbook_id=best_match.playbook.playbook_id,
alert_category=getattr(incident, "alert_category", None),
)
return AutoRepairDecision(
can_auto_repair=False,
playbook=best_match.playbook,
reason=(
"主機/備份類告警禁止執行 K8s Playbook"
"需改走 SSH 診斷或緊急介入"
),
blocked_by="HOST_BACKUP_K8S_PLAYBOOK",
)
# 5. 可以自動修復
logger.info(
"auto_repair_approved",
@@ -676,6 +693,29 @@ class AutoRepairService:
return max_risk
def _is_host_or_backup_incident(self, incident: Incident) -> bool:
"""主機/備份類事件只能走 SSH/只讀診斷,不允許 K8s rollout 類修復。"""
category = (getattr(incident, "alert_category", None) or "").lower()
if category in {"host_resource", "backup_failure"}:
return True
for signal in incident.signals or []:
labels = signal.labels or {}
alertname = str(labels.get("alertname") or signal.alert_name or "")
if alertname.startswith("HostBackup") or alertname.startswith("Host"):
return True
return False
def _playbook_has_k8s_steps(self, playbook: Playbook) -> bool:
"""檢查 Playbook 是否包含 K8s 指令,避免主機告警誤執行 deployment 操作。"""
for step in playbook.repair_steps:
command = (step.command or "").strip().lower()
if step.action_type == ActionType.KUBECTL or command.startswith("kubectl "):
return True
return False
def _risk_exceeds_threshold(self, risk: RiskLevel) -> bool:
"""檢查風險是否超過自動修復門檻"""
high_risks = {RiskLevel.HIGH, RiskLevel.CRITICAL}