feat(collectors): DaemonSet 不再填日志路径,改为「只采集哪些命名空间」多选
CI / Backend (go vet + test) (push) Canceled after 0s
CI / Frontend (lint + build + test) (push) Canceled after 0s

用户指出的三个问题,逐条核实后的结论:

1) 「采集日志路径」对 DaemonSet 确实是多余的 —— 确认后端模板里写死了
   `replacement: /var/log/pods/*$1/*.log`(按 pod UID 精确匹配容器标准输出),
   表单里填的 logPath 对 DaemonSet 完全不参与渲染。
   → 选择 DaemonSet 时隐藏该字段,提交时强制置空。

2) 「可以指定采集多个命名空间」—— 后端**早就支持**:
   models.Collector.TargetNamespaces + promtailNamespaceFilter(),
   空数组 = 全集群。只是**前端新建弹框没暴露**(编辑弹框里有)。
   → 新建弹框补上多选 chips(全集群 / 各命名空间可点选),并补齐前后端透传:
     dto.CreateCollectorRequest.TargetNamespaces
     controller → services.CreateCollectorRequest.TargetNamespaces
     models.Collector.TargetNamespaces
   之前前端即使传了也会被 Go 静默丢弃(无该字段)。

3) 「命名空间」字段语义已在上一提交补了分类型说明:
   - DaemonSet:采集器自身部署在哪个命名空间(不影响采什么)
   - Sidecar:目标应用 Deployment 所在命名空间

新增 TestDaemonsetTargetNamespacesFilter:
  nil → 不生成 namespaces 白名单(全集群)
  ["astralx"] → 生成且包含 astralx
  ["astralx","default"] → 两个都在

=== 实测(d45dfa18ecc5)===
  前端文案「只采集这些命名空间」「不选 = 采集整个集群…」均已进包
  渲染测试 3 种输入全部符合预期
  注:本次 docker push latest 遇到 Docker Hub 502,v0.1 成功而 latest 仍指向旧镜像,
     导致首次滚动后 Pod 仍是旧代码;重推 latest + 强制重建 Pod 后确认
     Pod digest = d45dfa18ecc5 —— 记录这个坑:push 失败必须核对三方 digest。
This commit is contained in:
opsx committed 2026-10-30 16:03:24 +00:00
1 parent 87909ed403
commit c8d026adc7
8 files changed
+104 -5

No files matched your search

+1 -1
View File
@@ -28,7 +28,7 @@ func (ctrl *CollectorController) Create(c *gin.Context) {
Name: req.Name, Type: req.Type, LokiDeploymentID: req.LokiDeploymentID,
HostID: req.HostID, ClusterID: req.ClusterID, TargetDeployment: req.TargetDeployment,
Namespace: req.Namespace, LogPath: req.LogPath, Labels: req.Labels,
ScrapeJobs: req.ScrapeJobs,
ScrapeJobs: req.ScrapeJobs, TargetNamespaces: req.TargetNamespaces,
})
if err != nil {
utils.Error(c, 400, err.Error())
+3
View File
@@ -14,4 +14,7 @@ type CreateCollectorRequest struct {
Labels string `json:"labels"`
// ScrapeJobs 主机模式的结构化采集项(可选;留空按 LogPath 生成 app + system 两个默认项)
ScrapeJobs []models.ScrapeJob `json:"scrapeJobs"`
// TargetNamespaces 仅 k8s-daemonset 使用:只采集这些命名空间的 Pod 日志。
// 空/不传 = 采集全集群(默认,保持旧行为)。
TargetNamespaces []string `json:"targetNamespaces"`
}
+2
View File
@@ -57,6 +57,7 @@ type CreateCollectorRequest struct {
LogPath string
Labels string
ScrapeJobs []models.ScrapeJob
TargetNamespaces []string
}
// fillAssociations 填充关联的 Loki / Host / Cluster(用于配置生成与展示)
@@ -286,6 +287,7 @@ func (s *CollectorService) Create(req *CreateCollectorRequest) (*models.Collecto
LogPath: req.LogPath,
Labels: req.Labels,
ScrapeJobs: req.ScrapeJobs,
TargetNamespaces: req.TargetNamespaces,
Status: "pending",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
@@ -0,0 +1,37 @@
package services
import (
"strings"
"testing"
)
// 回归:DaemonSet 模式的"只采集这些命名空间"必须真正渲染进 promtail 配置,
// 否则前端多选形同虚设(用户选了 astralx,实际还是全集群在采)。
func TestDaemonsetTargetNamespacesFilter(t *testing.T) {
base := func(ns []string) string {
return PromtailDaemonsetYAML("log", "http://loki.log.svc:3100", "c1", "", ns, "", nil)
}
// 1) 空 = 全集群:不应出现 namespaces 白名单
all := base(nil)
if strings.Contains(all, "namespaces:") {
t.Error("targetNamespaces 为空时不应生成命名空间白名单(应为全集群采集)")
}
// 2) 指定一个命名空间:必须出现白名单
one := base([]string{"astralx"})
if !strings.Contains(one, "namespaces:") {
t.Fatal("targetNamespaces=[astralx] 时没有生成 namespaces 白名单")
}
if !strings.Contains(one, "astralx") {
t.Error("白名单里没有 astralx")
}
// 3) 多个命名空间都要在
multi := base([]string{"astralx", "default"})
for _, want := range []string{"astralx", "default"} {
if !strings.Contains(multi, want) {
t.Errorf("多选时白名单缺少 %q", want)
}
}
}