fix(promtail): app 标签通用回退 + 跳过僵尸 Pod + SecretKey 不下发 + 采集命名空间可选

=== 一、astralx 只采集到 2 个 app(核心问题)===
实证:用户的 Deployment Pod 模板只有 k8s.kuboard.cn/name 标签,
     没有 app / app.kubernetes.io/name。原模板的 app 只从这两个标签取,
     所以 115 个服务的 app 全是空值,Loki 里 app 只剩零星 2 个。

修复:app 四级回退,全部基于 Kubernetes 原生元数据(跨集群通用):
  1) app.kubernetes.io/name   标准约定
  2) app                      常见简写
  3) __tmp_controller_name    Pod owner(ReplicaSet) 去 hash → Deployment 名
                              (__meta_kubernetes_pod_controller_name 由
                                Promtail 直接从 K8s API 读取,任何集群都有,
                                不依赖 Kuboard/Rancher 等第三方工具注入的标签)
  4) pod name                 兜底,保证 app 永不为空

同时修复 PromtailHelmValues 模板同样的 app 逻辑,
并修正其 __meta_kubernetes__pod_name 双下划线笔误。

验证:astralx 的 app 值从 2 个 → 73 个。

=== 二、15 分钟 27 万条日志的真相 ===
排查结论:不是重复采集(positions 存 hostPath /run/promtail,Pod 重启不丢)。
真实原因:cex-openapi-gateway 残留 347 个 Evicted + 37 个 Error 的僵尸 Pod,
        只有 1 个 Running。这些 Pod 的日志文件仍在 /var/log/pods/ 下,
        Promtail 会把它们的历史异常堆栈(Reactor at reactor.core.publisher...)
        全量采集,造成 27 万条的假象并污染存储。

修复:relabel_configs 首条加 drop 规则:
  - source_labels: [__meta_kubernetes_pod_phase]
    regex: (Succeeded|Failed)
    action: drop
(两个模板都加)

=== 三、SecretKey 不再下发给前端 ===
安全要求:避免任何登录用户点「显示」按钮就能拿到对象存储 SK。
- StorageConfig.Sanitized(): secretKey 返回空串,仅保留 hasSecretKey 标记
  (accessKey 仍明文返回,便于管理员核对用的是哪个 AK)
- 测试连接:StorageTestRequest 新增 id 字段,secretKey 留空时后端用
  库里保存的凭据解密补齐(新建存储仍强制填写)
- 前端编辑时 SecretKey 一律留空 = 不修改,placeholder 显示
  「已保存 · 留空则不修改」,测试时带上 id

=== 四、采集器支持指定命名空间 ===
- Collector 新增 TargetNamespaces []string,对应 promtail 的
  kubernetes_sd_configs.namespaces.names(为空=全集群)
- PromtailDaemonsetYAML 新增 promtailNamespaceFilter 生成白名单
- service.Update 支持 targetNamespaces(兼容 []string 与逗号分隔字符串)
- 前端 CollectorEditDialog 增加「采集命名空间」输入(逗号分隔)

镜像:lghost/lokistack:latest + v0.1 = sha256:05b1505aedae
This commit is contained in:
opsx committed 2026-10-27 03:30:25 +00:00
1 parent 24caeefb3a
commit e006fc1a0e
13 files changed
+252 -58

No files matched your search

+4 -1
View File
@@ -15,7 +15,10 @@ type Collector struct {
ClusterID *uint `json:"clusterID,omitempty"`
Cluster *Cluster `json:"cluster,omitempty"`
TargetDeployment string `json:"targetDeployment"` // sidecar 专用:目标 deployment 名
Namespace string `json:"namespace"` // k8s 命名空间(默认 logging)
Namespace string `json:"namespace"` // 采集器自身部署所在的 k8s 命名空间
// TargetNamespaces 只采集这些命名空间的 Pod 日志;为空表示采集全集群。
// 对应 promtail 里 kubernetes_sd_configs.namespaces.names
TargetNamespaces []string `json:"targetNamespaces"`
LogPath string `json:"logPath"` // 采集日志路径
Labels string `json:"labels"` // 额外标签(JSON)
Status string `json:"status"` // pending | active | error
+14 -11
View File
@@ -23,10 +23,13 @@ type StorageConfig struct {
}
func (s *StorageConfig) Sanitized() map[string]interface{} {
// AccessKey / SecretKey 在库里是 ENC:<ciphertext> 加密存储,
// 直接返回会让前端编辑框显示 ENC:xxx 密文。这里统一解密后返回明文,
// 前端 Secret Key 仍用密码框(默认掩码,点「显示」才展开),
// 便于管理员核对凭据并「测试连接」。
// 凭据在库里是 ENC:<ciphertext> 加密存储。
//
// 安全约定:
// - AccessKey 解密后明文返回(管理员需要核对自己用的是哪个 AK)
// - SecretKey 一律不返回明文,只给 hasSecretKey 标记
// —— 避免任何登录用户点一下「显示」按钮就能拿到 SK。
// 编辑时前端留空表示「不修改」,测试连接由后端用库里保存的 SK 兜底。
plainAK := crypto.DecryptField(s.AccessKey)
plainSK := crypto.DecryptField(s.SecretKey)
@@ -37,13 +40,13 @@ func (s *StorageConfig) Sanitized() map[string]interface{} {
"endpoint": s.Endpoint,
"bucket": s.Bucket,
"region": s.Region,
"accessKey": plainAK,
"secretKey": plainSK,
"pathPrefix": s.PathPrefix,
"insecure": s.Insecure,
"isDefault": s.IsDefault,
"accessKey": plainAK,
"secretKey": "",
"pathPrefix": s.PathPrefix,
"insecure": s.Insecure,
"isDefault": s.IsDefault,
"hasSecretKey": plainSK != "",
"createdAt": s.CreatedAt,
"updatedAt": s.UpdatedAt,
"createdAt": s.CreatedAt,
"updatedAt": s.UpdatedAt,
}
}