Skip to content

feat: support cr for k8s entity generator#2542

Open
StartE wants to merge 21 commits into
alibaba:mainfrom
StartE:yili/cr_entity
Open

feat: support cr for k8s entity generator#2542
StartE wants to merge 21 commits into
alibaba:mainfrom
StartE:yili/cr_entity

Conversation

@StartE
Copy link
Copy Markdown
Collaborator

@StartE StartE commented Mar 27, 2026

K8s Meta v2:支持 CR 动态采集与实体/关联导出,并增强 Informer 错误与缓存同步处理。

背景 / 要解决什么

LoongCollector 的 Kubernetes Meta v2 原先主要面向 内置资源(如 Pod、Service)。在实际场景里,很多工作负载信息在 自定义资源(CR) 上(例如 Argo Workflow),需要和 Pod、Namespace 等一起做 实体(entity)关联(link) 采集,便于检索与关联分析。

本 PR 在 不破坏现有内置资源行为 的前提下,增加 按配置采集指定 GVR 的 CR,并支持可选的 Pod↔CRNamespace↔CR 关联导出。

行为变化

  1. 配置驱动:在 pipeline / 插件配置里声明要采的 CR(API 组、版本、资源名 plural、Kind 等)及是否导出实体、标签、注解、关联类型等。
  2. 实现方式:对 CR 使用 client-go 的 Dynamic Client + SharedInformerunstructured),与现有 Clientset + Typed Informer 并存;Dynamic 侧使用 JSON 内容协商(与 Clientset 可能使用的 protobuf 区分)。
  3. 可靠性:Informer 在 连续不可恢复的 watch 错误(如鉴权失败、资源不存在等,见代码中的判定)下会 give-up 并停止,避免无限重试刷屏;缓存首次同步有 退避重试与日志,便于运维排查。

架构

MetaManager 在 Init 时注入 rest.Config;内置资源走原有 cache;CR 走统一 dynamic informer cache,事件进入同一套 link / entity 管线,由 kubernetesmetav2 插件按配置转成 pipeline 事件。

主要改动

区域 说明
pkg/helper/k8smeta/ CR 配置类型与注册、cr_unified_cache、与 k8s_meta_link / k8s_meta_manager 集成;k8s_meta_informer_* 处理 watch 错误与 cache sync
plugins/input/kubernetesmetav2/ meta_collector 增加 CR 实体与 Namespace→CR 等导出逻辑;service_meta 接配置
docs/cn/plugins/input/extended/service-kubernetesmeta-v2.md 使用说明与字段说明(建议 reviewer 先看文档再对代码)

功能使用示例

CR资源使用示例(1) 采集argoWorkflow:

enable: true
inputs:
  - Type: service_kubernetes_meta
    Interval: 600
    Node: true
    Pod: true
    # Third-party CRs: configure each GVR (and optional PodLink / Entity2PodRelation) under CustomResources.
    CustomResources:
      - APIGroup: argoproj.io     # API-Group
        APIVersion: v1alpha1       # API version
        Resource: workflows       # 资源类型
        Kind: Workflow                 # kind信息
        EntityType: argo.workflow    #实体类型
        CollectEntity: true   
        Namespace2EntityRelation: "contains"     # 和Namespace的关联关系提取
        Entity2PodRelation: "contains"    
        PodLink:  # 和Pod的关联关系提取
          OwnerKind: Workflow          # Pod OwnerReference  对应的资源Kind
          OwnerAPIGroupContains: argoproj.io     # Pod OwnerReference 对应的 API-Group
          PodLabelKey: workflows.argoproj.io/workflow  #  兜底逻辑,从label中提取关联关系
        EnableLabels: true        # CR粒度配置是否上报标签
        EnableAnnotations: true # CR粒度配置是否上报注释

CR资源使用示例(2)采集Sandbox:

  - Type: service_kubernetes_meta
    Interval: 600
    Node: true
    Pod: true
    CustomResources:
      - APIGroup: agents.kruise.io
        APIVersion: v1alpha1
        Resource: sandboxes
        Kind: Sandbox
        # EntityType is required (cache key, __entity_type__, pod->{EntityType});
        EntityType: kruise.sandbox  #实体类型
        CollectEntity: true
        Entity2PodRelation: "contains"
        Namespace2EntityRelation: "contains"
        PodLink:
          OwnerKind: Sandbox
          OwnerAPIGroupContains: agents.kruise.io
          PodLabelKey: agent
        # Per CustomResource item only; does not use input-level EnableLabels/EnableAnnotations.
        EnableLabels: true
        EnableAnnotations: true

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为 LoongCollector 的 Kubernetes Meta v2 增加 按配置采集第三方自定义资源(CR) 的能力:通过 dynamic informer 采集 unstructured CR,对外导出对应 实体(entity),并可选生成 Pod↔CRNamespace↔CR 的关联(link),同时补充 informer 的 watch 错误 give-up 与 cache sync 逻辑,以及配套文档与单测。

Changes:

  • service_kubernetes_meta 配置中新增 CustomResources,并在 meta collector 中注册 CR 实体与链路事件处理与发送。
  • pkg/helper/k8smeta 引入 CR 采集配置类型、crUnifiedCache(dynamic informer)、以及 Pod→CR / CR→Namespace 的 link 生成。
  • 新增/更新单测与中文文档,补充 CustomResources 的字段、RBAC 与故障行为说明。

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
plugins/input/kubernetesmetav2/service_meta.go 新增 CustomResources 配置入口与解析辅助方法
plugins/input/kubernetesmetav2/meta_collector.go 启动流程中注册/启动 CR collector,并注册对应 send/link 处理
plugins/input/kubernetesmetav2/meta_collector_cr.go 新增 CR 实体与 CR 链路(Pod/Namespace)事件转 pipeline event
plugins/input/kubernetesmetav2/meta_collector_cr_test.go 覆盖 CR 配置归一化、去重、以及 CR link/entity 处理逻辑
pkg/helper/k8smeta/k8s_meta_manager.go MetaManager 支持注册 CR cache、保存 rest.Config、并发读写 cacheMap 加锁
pkg/helper/k8smeta/k8s_meta_link.go LinkGenerator 增加 Pod→CR 与 CR→Namespace 链路生成
pkg/helper/k8smeta/k8s_meta_link_test.go 覆盖 Pod→CR(ownerRef/label fallback)与 CR→Namespace link 生成
pkg/helper/k8smeta/k8s_meta_informer_lifecycle.go 新增 informer 生命周期辅助:give-up stop 合并、watch error 处理、cache sync
pkg/helper/k8smeta/k8s_meta_informer_giveup.go 定义“应 give-up”的错误分类与阈值
pkg/helper/k8smeta/k8s_meta_custom_resource.go CustomResources 配置结构、Normalize、GVR/LinkType 辅助函数
pkg/helper/k8smeta/k8s_meta_cr_unified_cache.go dynamic informer CR cache 实现(unstructured + trimmed copy)
pkg/helper/k8smeta/k8s_meta_cache.go 内置 typed informer 引入 give-up handler 与 cache sync helper
pkg/helper/k8smeta/k8s_meta_const.go 新增 PodCustomResource / NamespaceCustomResource 结构定义
go.mod 将 controller-runtime 作为直接依赖(用于 config/apiutil)
docs/cn/plugins/input/extended/service-kubernetesmeta-v2.md 文档补充 CustomResources、RBAC、行为说明与示例

Comment thread pkg/helper/k8smeta/k8s_meta_informer_lifecycle.go Outdated
Comment thread pkg/helper/k8smeta/k8s_meta_link.go
Comment thread pkg/helper/k8smeta/k8s_meta_cr_unified_cache.go
Comment thread pkg/helper/k8smeta/k8s_meta_cr_unified_cache.go
Comment thread pkg/helper/k8smeta/k8s_meta_cr_unified_cache.go Outdated
Comment thread pkg/helper/k8smeta/k8s_meta_cr_unified_cache.go Outdated
Comment thread pkg/helper/k8smeta/k8s_meta_informer_lifecycle.go Outdated
Comment thread pkg/helper/k8smeta/k8s_meta_informer_lifecycle.go Outdated
Comment thread pkg/helper/k8smeta/k8s_meta_manager.go
Comment thread pkg/helper/k8smeta/k8s_meta_link.go
@StartE StartE requested a review from linrunqi08 May 6, 2026 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants