fix(loki): COS 存储全链路修复 —— Secret 生成/env 注入/凭据解密/endpoint 规范化

API 端到端跑通中暴露 4 个串联问题(腾讯云 COS + K8s 场景):

1. loki_manifest.go: switch 缺少 cos / obs
   → 不生成 loki-storage-credentials Secret、不注入 LOKI_STORAGE_* 环境变量、
     也不给 Loki 加 -config.expand-env=true
   → 配置里 ${LOKI_STORAGE_ACCESS_KEY} 无法展开 → AK 为空
   → COS 返回 403 InvalidAccessKeyId(ruler 每分钟报错)
   修复:case 补上 cos / obs

2. loki_manifest.go: 生成 Secret 时用了 ENC: 密文原始值
   → Secret data 里是 ENC:xxx 而非真实 AK/SK
   修复:crypto.DecryptField 解密后再 base64

3. loki_config.go: host-docker 模式同样把密文写进配置
   修复:解密后返回

4. loki_config.go: cos/oss/obs 的 endpoint 未剥离桶名前缀
   → 用户填虚拟主机式 endpoint(mybucket-1250000000.cos.<region>...)时,
     Loki 的 S3 客户端又拼一次 bucketnames,变成 <bucket>.<bucket>.cos...
   → TLS 证书校验失败(x509: certificate is valid for ... not <bucket>.<bucket>.cos...)
   修复:新增 stripBucketPrefix,s3/cos/oss/obs/minio 分支统一剥离桶名前缀

验证(log namespace,API 端到端):
  - Loki statefulset 1/1 Running,ruler 无 InvalidAccessKeyId/x509
  - 写入 + 查询日志成功(持久化到 COS)
  - Promtail DaemonSet 12/12 全节点就绪
  - collector status=active

运维侧附加动作(非代码,见部署文档):
  - 存储配置改用有效 AK/SK + 区域端点 cos.ap-hongkong.myqcloud.com
  - 平台 ServiceAccount log:lokistack 绑定 cluster-admin
    (否则无法创建 Promtail 所需的 ClusterRole/ClusterRoleBinding)
This commit is contained in:
opsx committed 2026-10-26 19:32:38 +00:00
1 parent 004375e0ce
commit 46e73d3d17
3 files changed
+47 -6

No files matched your search

+23
View File
@@ -152,6 +152,29 @@ func endpointHasBucket(endpoint, bucket string) bool {
return bucket != "" && strings.HasPrefix(strings.ToLower(host), strings.ToLower(bucket)+".")
}
// stripBucketPrefix 从 endpoint 里剥掉桶名前缀,得到区域端点。
//
// mybucket-1250000000.cos.ap-hongkong.myqcloud.com → cos.ap-hongkong.myqcloud.com
// cos.ap-hongkong.myqcloud.com → 原样返回
//
// Loki 的 S3 客户端(s3forcepathstyle=false)会自己把 bucketnames 拼到 endpoint 前,
// 所以配置里必须是区域端点,否则会拼出 <bucket>.<bucket>.cos... 触发 TLS 证书错误。
func stripBucketPrefix(endpoint, bucket string) string {
if bucket == "" || endpoint == "" {
return endpoint
}
scheme := ""
host := strings.TrimSpace(endpoint)
if i := strings.Index(host, "://"); i >= 0 {
scheme, host = host[:i+3], host[i+3:]
}
prefix := strings.ToLower(bucket) + "."
if strings.HasPrefix(strings.ToLower(host), prefix) {
return scheme + host[len(prefix):]
}
return endpoint
}
// bucketPathFor 根据 endpoint 风格决定桶路径(用于建桶 PUT)。
//
// 1) 区域端点(推荐): cos.ap-hongkong.myqcloud.com → 路径式 /<bucket>