From 02705382f11032f725b4c608bbc0261ade4118b7 Mon Sep 17 00:00:00 2001 From: opsx Date: Tue, 27 Oct 2026 10:30:21 +0000 Subject: [PATCH] =?UTF-8?q?fix(collector):=20Sanitized()=20=E6=BC=8F?= =?UTF-8?q?=E6=8E=89=E9=87=87=E9=9B=86=E8=8C=83=E5=9B=B4=E4=B8=89=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E5=AF=BC=E8=87=B4=E7=BC=96=E8=BE=91=E6=A1=86?= =?UTF-8?q?=E8=AF=BB=E4=B8=8D=E5=88=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景:基于 f3733f0(品牌统一 + i18n 重构)构建部署后发现,采集器 API 返回里完全没有了 targetNamespaces / appLabelSource / appInclude 三个键。 根因:重构引入了 models.SanitizedJSON() + Collector.Sanitized(), 后者是**白名单式** map 输出,写的时候这三个字段还不存在,重构时也没补进去。 controller 全部走 SanitizedJSON(collector),所以这三个字段被静默丢弃。 影响(比"少显示几个字段"严重): 编辑采集策略弹框读 collector.targetNamespaces 为 undefined → 命名空间面板显示"全集群采集"(实际配的是 astralx+log) → 用户只要点一次保存,就会把 targetNamespaces 写成 [] → 下次「部署」生成的 Promtail 白名单消失,开始全集群采集(日志量暴涨) appLabelSource / appInclude 同理。 修复:Collector.Sanitized() 补三个字段。 验证: GET /api/v1/collectors/1 targetNamespaces = ['astralx', 'log'] ← 修复前该键不存在 appLabelSource = '' appInclude = null 三个键均存在 ✅ Promtail DaemonSet 12/12 未受影响 ✅ Promtail ConfigMap 白名单(astralx, log)、app 四级回退、pod_phase drop 全部保留 ✅ 镜像:lghost/lokistack:latest + v0.1 = sha256:648316ae13c5 --- backend/models/collector.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/models/collector.go b/backend/models/collector.go index a5df2b8..e26d0f2 100644 --- a/backend/models/collector.go +++ b/backend/models/collector.go @@ -66,6 +66,11 @@ func (c *Collector) Sanitized() map[string]interface{} { "cluster": cluster, "targetDeployment": c.TargetDeployment, "namespace": c.Namespace, + // 采集范围配置:必须一起输出,否则前端编辑框读不到(显示为空), + // 用户一保存就会把现有配置写空 —— 这是重构加 Sanitized() 时漏字段导致的回归。 + "targetNamespaces": c.TargetNamespaces, + "appLabelSource": c.AppLabelSource, + "appInclude": c.AppInclude, "logPath": c.LogPath, "labels": c.Labels, "status": c.Status,