From 7a10d27d617cd1c57988e6266cb9a20c2e231d75 Mon Sep 17 00:00:00 2001 From: OoO Date: Tue, 5 May 2026 01:09:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(p53):=20K8s=20Nginx=20Proxy=20=E6=94=AF?= =?UTF-8?q?=E6=8F=B4=20=E2=80=94=20host=5Flabel/provider=5Ftag=20=E8=A3=9C?= =?UTF-8?q?=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 問題: K8s 內網無法直連 GCP 公網 11434,所以 110 跳板架了 Nginx Proxy 轉發 11435/11436 到 GCP-A/GCP-B。但 services/ollama_service.py 的 get_host_label() 只看 IP substring(34.143.170.20 / 34.21.145.224), K8s 環境會 fallback 到「未知」造成觀測台主機標籤錯亂。 修補: - services/ollama_service.py::get_host_label 新增: 192.168.0.110:11435 → "GCP-SSD(via Nginx 110)" 192.168.0.110:11436 → "GCP-SSD-2(via Nginx 110)" 保留:直連 GCP / 111 / 188 / localhost 既有判斷 - services/ollama_service.py::get_provider_tag(新函式) 統一 provider 標籤判斷(之前散落 code_review_pipeline 等多處重寫): GCP 直連 + Nginx 11435 → 'gcp_ollama' GCP-B 直連 + Nginx 11436 → 'ollama_secondary' 111 → 'ollama_111' 其他 → 'ollama_other' 跨環境統一:ai_calls.provider 在 docker-compose / K8s 都記同一 tag, 跨環境統計不分裂。 - services/code_review_pipeline_service.py:233 改用統一 get_provider_tag(),移除原本 hardcode 的 if/else IP 判斷。 - k8s/02-configmap.yaml(user 已改) OLLAMA_HOST_PRIMARY = http://192.168.0.110:11435 (Nginx → GCP-A) OLLAMA_HOST_SECONDARY = http://192.168.0.110:11436 (Nginx → GCP-B) OLLAMA_HOST_FALLBACK = http://192.168.0.111:11434 (內網) driver test: http://34.143.170.20:11434 → GCP-SSD / gcp_ollama http://192.168.0.110:11435 → GCP-SSD(via Nginx 110)/ gcp_ollama http://192.168.0.111:11434 → 111 備援 / ollama_111 Co-Authored-By: Claude Opus 4.7 (1M context) --- k8s/02-configmap.yaml | 7 +++--- services/code_review_pipeline_service.py | 10 +++----- services/ollama_service.py | 31 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/k8s/02-configmap.yaml b/k8s/02-configmap.yaml index db95438..c052d7d 100644 --- a/k8s/02-configmap.yaml +++ b/k8s/02-configmap.yaml @@ -44,10 +44,9 @@ data: PASSWORD_REQUIRE_SPECIAL: "false" # Ollama AI 服務(ADR-027 三主機級聯:GCP-A → GCP-B → 111) - # 不再寫死 OLLAMA_HOST,改由 services/ollama_service.py:resolve_ollama_host() - # 動態解析。三主機 env 由 OLLAMA_HOST_PRIMARY/SECONDARY/FALLBACK 控制。 - OLLAMA_HOST_PRIMARY: "http://34.143.170.20:11434" - OLLAMA_HOST_SECONDARY: "http://34.21.145.224:11434" + # 已架設 Nginx Proxy (110:11435/11436) 轉發至 GCP,解決 K8s 內網無法直連 GCP 11434 的問題。 + OLLAMA_HOST_PRIMARY: "http://192.168.0.110:11435" + OLLAMA_HOST_SECONDARY: "http://192.168.0.110:11436" OLLAMA_HOST_FALLBACK: "http://192.168.0.111:11434" OLLAMA_MODEL: "qwen3:8b" diff --git a/services/code_review_pipeline_service.py b/services/code_review_pipeline_service.py index 5374c77..dbcfaa7 100644 --- a/services/code_review_pipeline_service.py +++ b/services/code_review_pipeline_service.py @@ -228,13 +228,11 @@ class CodeReviewPipeline: 只輸出 JSON 陣列,不含其他文字。無問題時輸出 []""" # ADR-027 Phase 2 N3:lazy resolve Hermes 主機(GCP 優先 / 111 備援), - # 避開 import-time freeze。provider 標籤跟著解析結果動態決定。 + # 避開 import-time freeze。Phase 53:改用 services.ollama_service.get_provider_tag + # 統一函式,支援 K8s Nginx Proxy(110:11435/11436)。 hermes_url = get_hermes_url() - provider_tag = ( - 'gcp_ollama' if ('34.21.145.224' in hermes_url or '34.143.170.20' in hermes_url) - else 'ollama_111' if '192.168.0.111' in hermes_url - else 'ollama_other' - ) + from services.ollama_service import get_provider_tag + provider_tag = get_provider_tag(hermes_url) # Phase 1 v5.0: 包 ai_call_logger 追蹤 Code Review Hermes scan with log_ai_call( caller='code_review_hermes', diff --git a/services/ollama_service.py b/services/ollama_service.py index c3f02c8..46a57a3 100644 --- a/services/ollama_service.py +++ b/services/ollama_service.py @@ -134,13 +134,24 @@ def resolve_ollama_host(primary: str = OLLAMA_HOST_PRIMARY, def get_host_label(host: str) -> str: - """將 IP/URL 轉換為易讀的主機標籤""" + """將 IP/URL 轉換為易讀的主機標籤 + + Phase 53:支援 K8s 環境的 Nginx Proxy(110:11435/11436 → GCP)。 + 判斷順序:直連 GCP IP > Nginx 轉發 port > 內網 IP > 本地。 + """ if not host: return "未知" + # 直連 GCP(docker-compose 環境) if "34.143.170.20" in host: return "GCP-SSD" if "34.21.145.224" in host: return "GCP-SSD-2" + # Nginx Proxy 轉發(K8s 環境,110 跳板代理 GCP) + if "192.168.0.110:11435" in host: + return "GCP-SSD(via Nginx 110)" + if "192.168.0.110:11436" in host: + return "GCP-SSD-2(via Nginx 110)" + # 內網 / 本地 if "192.168.0.111" in host: return "111 備援" if "192.168.0.188" in host or "localhost" in host: @@ -148,6 +159,24 @@ def get_host_label(host: str) -> str: return host.split('//')[-1].split(':')[0] +def get_provider_tag(host: str) -> str: + """將 host URL 轉換為 ai_calls.provider 標籤 + + Phase 53 新加:統一 provider 判斷邏輯,避免散落各 service 重寫。 + 對應 ai_calls.provider 白名單:gcp_ollama / ollama_secondary / ollama_111 / ollama_other + """ + if not host: + return 'ollama_other' + # GCP 直連或 Nginx 轉發都歸 gcp_ollama / ollama_secondary + if "34.143.170.20" in host or "192.168.0.110:11435" in host: + return 'gcp_ollama' + if "34.21.145.224" in host or "192.168.0.110:11436" in host: + return 'ollama_secondary' + if "192.168.0.111" in host: + return 'ollama_111' + return 'ollama_other' + + @dataclass class OllamaResponse: """Ollama 回應結構