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

@@ -59,7 +59,7 @@ export const CollectorCard = memo(function CollectorCard({
message={collector.statusMessage || undefined}
actions={
<>
<Button variant="outline" size="sm" aria-label={`编辑采集策略 ${collector.name}`} title="编辑采集策略" onClick={() => onEdit(collector)}>
<Button variant="outline" size="sm" aria-label={`编辑采集策略 ${collector.name}`} title="编辑采集策略 / 采集命名空间" onClick={() => onEdit(collector)}>
<Pencil className="size-3.5" />
</Button>
<Button variant="outline" size="sm" className="flex-1 text-xs" onClick={() => onInstall(collector.id)} disabled={isBusy}>
@@ -13,6 +13,8 @@ import type { Collector } from '@/types'
const schema = z.object({
name: z.string().trim().min(1, '请输入采集策略名称'),
logPath: z.string().trim(),
/** 逗号分隔的命名空间白名单;留空 = 采集全集群 */
targetNamespaces: z.string().trim(),
})
type Values = z.infer<typeof schema>
@@ -24,17 +26,34 @@ interface Props {
export const CollectorEditDialog = memo(function CollectorEditDialog({ collector, onOpenChange, onSaved }: Props) {
const [saving, setSaving] = useState(false)
const form = useForm<Values>({ resolver: zodResolver(schema), defaultValues: { name: '', logPath: '' } })
const form = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { name: '', logPath: '', targetNamespaces: '' },
})
useEffect(() => {
if (collector) form.reset({ name: collector.name, logPath: collector.logPath || '' })
if (collector) {
form.reset({
name: collector.name,
logPath: collector.logPath || '',
targetNamespaces: (collector.targetNamespaces ?? []).join(', '),
})
}
}, [collector, form])
const submit = form.handleSubmit(async (values) => {
if (!collector) return
setSaving(true)
try {
await collectorApi.update(collector.id, values)
await collectorApi.update(collector.id, {
name: values.name,
logPath: values.logPath,
// 逗号/空格分隔 → 字符串数组;留空数组表示采集全集群
targetNamespaces: values.targetNamespaces
.split(/[,\s]+/)
.map((s) => s.trim())
.filter(Boolean),
})
onOpenChange(false)
onSaved()
} catch (err) {
@@ -54,6 +73,17 @@ export const CollectorEditDialog = memo(function CollectorEditDialog({ collector
<FormField control={form.control} name="name" render={({ field }) => (
<FormItem><FormLabel>名称</FormLabel><FormControl><Input {...field} /></FormControl><FormMessage /></FormItem>
)} />
{collector?.type === 'k8s-daemonset' && <FormField control={form.control} name="targetNamespaces" render={({ field }) => (
<FormItem>
<FormLabel>采集命名空间</FormLabel>
<FormControl><Input placeholder="astralx, log(留空 = 全集群)" {...field} /></FormControl>
<FormMessage />
<p className="text-xs text-muted-foreground">
多个命名空间用逗号分隔;留空则采集全集群所有命名空间。保存后点击「部署」应用新配置,
Promtail 会按此生成 kubernetes_sd_configs.namespaces 白名单。
</p>
</FormItem>
)} />}
{supportsPath && <FormField control={form.control} name="logPath" render={({ field }) => (
<FormItem>
<FormLabel>采集日志路径</FormLabel>
@@ -92,9 +92,11 @@ export const StorageFormDialog = memo(function StorageFormDialog({
name: editing.name, storageType: editing.storageType,
endpoint: editing.endpoint || '', bucket: editing.bucket,
region: editing.region || '',
// 后端已返回明文凭据,编辑时回填,避免每次都提示"请先填写 Access Key / Secret Key"
// AccessKey 后端明文返回,方便核对
accessKey: editing.accessKey || '',
secretKey: editing.secretKey || '',
// SecretKey 后端不下发明文(安全要求:避免登录用户点「显示」就能看到),
// 因此编辑时一律留空 = 不修改,测试连接由后端用库里保存的 SK 兜底
secretKey: '',
pathPrefix: editing.pathPrefix || '', insecure: editing.insecure, isDefault: editing.isDefault,
} as never : DEFAULT_VALUES)
setTestResult(null)
@@ -104,14 +106,18 @@ export const StorageFormDialog = memo(function StorageFormDialog({
// 测试连接:用当前表单值(未保存)直连对象存储验证
const handleTest = async () => {
const values = form.getValues()
if (!values.bucket.trim() || !values.accessKey?.trim() || !values.secretKey?.trim()) {
// 编辑已有存储时 SecretKey 留空是「不修改」,测试由后端用库里保存的 SK 兜底
const hasStoredSecret = Boolean(editing?.hasSecretKey)
if (!values.bucket.trim() || !values.accessKey?.trim() || (!values.secretKey?.trim() && !hasStoredSecret)) {
// 提示显示在弹框内(原来用 toast 弹到右下角,用户看不到是哪个字段的问题)
setTestResult({
reachable: false,
authenticated: false,
bucketExists: false,
bucketCreated: false,
message: '请先填写 Bucket 与 Access Key / Secret Key',
message: hasStoredSecret
? '请先填写 Bucket 与 Access Key(Secret Key 已保存,留空即可)'
: '请先填写 Bucket 与 Access Key / Secret Key',
})
return
}
@@ -119,6 +125,8 @@ export const StorageFormDialog = memo(function StorageFormDialog({
setTestResult(null)
try {
const result = await storageApi.test({
// 编辑态把 id 带上:SecretKey 留空时后端用库里保存的凭据补全
id: editing?.id,
storageType: values.storageType,
endpoint: values.endpoint,
bucket: values.bucket,
@@ -313,7 +321,11 @@ export const StorageFormDialog = memo(function StorageFormDialog({
<FormItem>
<FormLabel>Secret Key</FormLabel>
<FormControl>
<PasswordInput placeholder="minioadmin" {...field} />
<PasswordInput
placeholder={editing?.hasSecretKey ? '已保存 · 留空则不修改' : '请输入 Secret Key'}
autoComplete="new-password"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>