Skip to content

feat: Add a new CRD WasmPluginMatchRule for config decoupling - #3217

Draft
CH3CHO wants to merge 1 commit into
higress-group:mainfrom
CH3CHO:feat/wasm-plugin-match-rule
Draft

feat: Add a new CRD WasmPluginMatchRule for config decoupling#3217
CH3CHO wants to merge 1 commit into
higress-group:mainfrom
CH3CHO:feat/wasm-plugin-match-rule

Conversation

@CH3CHO

@CH3CHO CH3CHO commented Dec 12, 2025

Copy link
Copy Markdown
Collaborator

Ⅰ. Describe what this PR did

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

Ⅵ. AI Coding Tool Usage Checklist (if applicable)

Please check all applicable items:

  • For new standalone features (e.g., new wasm plugin or golang-filter plugin):

    • I have created a design/ directory in the plugin folder
    • I have added the design document to the design/ directory
    • I have included the AI Coding summary below
  • For regular updates/changes (not new plugins):

    • I have provided the prompts/instructions I gave to the AI Coding tool below
    • I have included the AI Coding summary below

AI Coding Prompts (for regular updates)

AI Coding Summary

@CH3CHO
CH3CHO marked this pull request as draft December 12, 2025 05:35
@lingma-agents

lingma-agents Bot commented Dec 12, 2025

Copy link
Copy Markdown

新增 CRD WasmPluginMatchRule 以实现配置解耦

变更概述
  • 新功能

    • 引入了新的 Kubernetes 自定义资源定义(CRD)WasmPluginMatchRule,用于将 WASM 插件的匹配规则从主插件配置中分离出来。
    • extensions.higress.io/v1alpha1 组中添加了 WasmPluginMatchRule 类型及相关结构体定义。
    • 支持通过 parent 字段关联到具体的 WasmPlugin 资源,并支持按 ingress、domain 和 service 进行匹配。
    • 实现了对 WasmPluginMatchRule 的控制器逻辑和事件处理机制,确保其能被正确加载并影响对应的 WASM 插件行为。
  • 重构

    • 修改了 convertIstioWasmPlugin 方法以整合来自独立 WasmPluginMatchRule 资源的匹配规则。
    • 更新了 AddOrUpdateWasmPlugin 函数来获取和应用相关的 WasmPluginMatchRule 配置。
    • 添加了排序函数 compareWasmPluginMatchRule 来保证多个匹配规则之间的执行顺序一致性。
  • 依赖更新

    • 扩展了客户端集合、列表器及 informer 等组件,使其能够识别和操作新的 WasmPluginMatchRule 资源类型。
    • 增加了必要的 deepcopy 及 JSON 序列化方法以便于在不同层级之间传递该对象。
  • 测试更新

    • (隐含)由于这是一个新增的功能模块,在后续需要补充单元测试用例验证匹配规则是否生效以及父子关系管理是否准确无误。
变更文件
文件路径 变更说明
api/​extensions/​v1alpha1/​wasmplugin.​pb.​go 添加了 WasmPluginMatchRule 消息类型的 Protobuf 定义及其相关辅助方法。
api/​extensions/​v1alpha1/​wasmplugin.​proto 在 .proto 文件中声明了 WasmPluginMatchRule 消息格式,并设置了生成 CRD 所需的注解标签。
api/​extensions/​v1alpha1/​wasmplugin_​deepcopy.​gen.​go 为 WasmPluginMatchRule 自动生成了深度拷贝所需的方法。
api/​extensions/​v1alpha1/​wasmplugin_​json.​gen.​go 实现了 WasmPluginMatchRule 的自定义 JSON 编组与解组接口。
api/​kubernetes/​customresourcedefinitions.​gen.​yaml 注册了 WasmPluginMatchRule 的 CRD 规范,包括字段约束和服务暴露方式等信息。
client/​pkg/​apis/​extensions/​v1alpha1/​register.​gen.​go 向 scheme 中注册了 WasmPluginMatchRule 类型,使得它可以作为标准 Kubernetes 对象使用。
client/​pkg/​apis/​extensions/​v1alpha1/​types.​gen.​go 定义了供客户端使用的 Go 结构体 WasmPluginMatchRule 及其 List 形式。
client/​pkg/​apis/​extensions/​v1alpha1/​zz_​generated.​deepcopy.​gen.​go 补全了由 code-generator 工具自动产生的 deepcopy 方法。
client/​pkg/​applyconfiguration/​utils.​go 扩展工具函数 ForKind 以支持动态构建 WasmPluginMatchRule ApplyConfiguration 对象。
client/​pkg/​clientset/​versioned/​typed/​extensions/​v1alpha1/​extensions_​client.​gen.​go 扩展 ExtensionsV1alpha1Interface 接口,加入访问 WasmPluginMatchRule 资源的相关方法。
client/​pkg/​clientset/​versioned/​typed/​extensions/​v1alpha1/​fake/​fake_​extensions_​client.​gen.​go 为模拟客户端增加了针对 WasmPluginMatchRule 的假数据访问接口。
client/​pkg/​clientset/​versioned/​typed/​extensions/​v1alpha1/​generated_​expansion.​gen.​go 预留扩展点,允许用户为 WasmPluginMatchRule 控制器添加额外功能。
client/​pkg/​informers/​externalversions/​extensions/​v1alpha1/​interface.​gen.​go 更新外部版本 Informer 接口以包含 WasmPluginMatchRule 相关通知机制。
client/​pkg/​informers/​externalversions/​generic.​gen.​go 增强了通用资源查找逻辑,现在可以定位到 WasmPluginMatchRule 的共享 Informer。
client/​pkg/​listers/​extensions/​v1alpha1/​expansion_​generated.​gen.​go 为 WasmPluginMatchRule 列表器添加可扩展性接口定义。
pkg/​ingress/​config/​ingress_​config.​go 集成 WasmPluginMatchRule 控制器,监听变化并在转换过程中合并外部规则;同时维护父级引用映射。
pkg/​ingress/​kube/​wasmplugin/​controller.​go 新建专门用于监控 WasmPluginMatchRule 资源变动的控制器实例及相关初始化过程。
时序图
sequenceDiagram
    participant WC as WasmPluginController
    participant WMR as WasmPluginMatchRuleController
    participant IC as IngressConfig
    participant K8sAPI as Kubernetes API Server

    Note over WC,WMR: 启动时初始化各自控制器
    activate WC
    activate WMR
    WC->>K8sAPI: Watch WasmPlugin events
    WMR->>K8sAPI: Watch WasmPluginMatchRule events

    Note over IC: 当收到 WasmPlugin 更新请求时
    IC->>IC: Load all related WasmPluginMatchRules via parent reference
    IC->>IC: Convert combined configuration into Istio WasmPlugin model
    IC->>XDS: Push updated config to data plane

    Note over IC: 当收到 WasmPluginMatchRule 更新/删除请求时
    IC->>IC: Track affected WasmPlugin through parent mapping
    IC->>IC: Trigger reprocessing of impacted WasmPlugins
    IC->>XDS: Propagate changes downstream
Loading

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

@lingma-agents lingma-agents Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 0 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 2 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 1 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 3 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
📦 api/extensions/v1alpha1/wasmplugin.proto (1 💬)
⚙️ api/kubernetes/customresourcedefinitions.gen.yaml (1 💬)
🔹 client/pkg/apis/extensions/v1alpha1/types.gen.go (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. WasmPluginMatchRule控制器与事件处理机制需加强测试覆盖与错误恢复策略

新增的WasmPluginMatchRule控制器及其事件处理器(AddOrUpdateWasmPluginMatchRule、DeleteWasmPluginMatchRule)在pkg/ingress/config/ingress_config.go中被引入,但缺乏明确的错误恢复和重试逻辑。若WasmPluginMatchRule资源更新失败或依赖的父级WasmPlugin不存在,可能导致配置不一致且无日志追踪。建议增加对异常情况的监控指标,并确保事件驱动流程具备幂等性和容错能力。此外,当前代码未展示针对这些新功能的单元测试或集成测试用例,存在潜在维护风险。

📌 关键代码

func (m *IngressConfig) AddOrUpdateWasmPluginMatchRule(...) {...}

⚠️ 潜在风险

系统可能因单个资源更新失败导致整个插件规则链失效;缺少可观测性将使问题排查困难。

🔍2. WasmPluginMatchRule排序逻辑复杂度高,可能影响大规模部署下的性能表现

convertIstioWasmPlugin方法中的matchRuleResources排序使用了自定义比较函数compareWasmPluginMatchRule,该函数涉及多次字符串切片排序和逐项对比,在大量WasmPluginMatchRule资源场景下会显著拖慢转换过程。尤其当服务、入口或域名匹配项较多时,slice sorting操作重复执行会造成CPU开销增大。应评估是否可通过预计算哈希值或其他优化手段降低时间复杂度。

📌 关键代码

slices.SortFunc(matchRuleResources, compareWasmPluginMatchRule)
func compareWasmPluginMatchRule(...) {...}

⚠️ 潜在风险

在包含大量WasmPluginMatchRule规则的大规模集群中,XDS推送延迟增加,控制面响应变慢。

🔍3. WasmPluginMatchRule Parent字段引用关系缺乏校验,易引发悬挂指针问题

WasmPluginMatchRule通过Parent字段关联到WasmPlugin资源名,但在处理过程中并未验证所指向的WasmPlugin是否存在或有效。如果用户误配或删除了对应的WasmPlugin而保留MatchRule,则会在运行时造成空引用或者无效配置加载。应在AddOrUpdateWasmPluginMatchRule方法内加入对Parent资源存在的前置检查,并提供清晰的状态反馈。

📌 关键代码

string parent = 20;

⚠️ 潜在风险

配置漂移、运行时报错以及难以调试的非预期行为。

🔍4. 多来源MatchRule合并顺序语义不明,业务一致性存在隐患

convertIstioWasmPlugin方法中同时处理嵌入式MatchRules及外部独立WasmPluginMatchRule资源,虽然注释说明后者优先,但实际合并策略没有文档化说明,容易引起使用者混淆。特别是两者混合使用时,最终生效规则取决于内部实现细节而非显式契约。这违反了业务逻辑一致性原则,应当明确定义并公开优先级规则,最好能体现在CRD规范或配套文档中。

📌 关键代码

// Separated WasmPluginMatchRule configurations always come after those embedded in the WasmPlugin resource.

⚠️ 潜在风险

规则覆盖不符合预期,引发安全策略绕过或插件行为异常等问题。

🔍5. 新增CRD WasmPluginMatchRule缺少状态子资源初始化逻辑

尽管为WasmPluginMatchRule添加了status子资源支持(如CRD定义和Go结构体),但是在控制器层面没有任何关于Status更新的操作逻辑。这意味着即使发生错误或状态变更也无法反映给用户,降低了可观察性和运维效率。需要补充相应的status管理器来跟踪同步状态、健康状况等关键信息。

📌 关键代码

status:
  type: object
  x-kubernetes-preserve-unknown-fields: true
Status metav1alpha1.IstioStatus `json:"status"`

⚠️ 潜在风险

无法获知资源配置的实际应用效果,阻碍自动化运维工具构建闭环。

审查详情
📒 文件清单 (17 个文件)
📝 变更: 17 个文件

📝 变更文件:

  • api/extensions/v1alpha1/wasmplugin.pb.go
  • api/extensions/v1alpha1/wasmplugin.proto
  • api/extensions/v1alpha1/wasmplugin_deepcopy.gen.go
  • api/extensions/v1alpha1/wasmplugin_json.gen.go
  • api/kubernetes/customresourcedefinitions.gen.yaml
  • client/pkg/apis/extensions/v1alpha1/register.gen.go
  • client/pkg/apis/extensions/v1alpha1/types.gen.go
  • client/pkg/apis/extensions/v1alpha1/zz_generated.deepcopy.gen.go
  • client/pkg/applyconfiguration/utils.go
  • client/pkg/clientset/versioned/typed/extensions/v1alpha1/extensions_client.gen.go
  • client/pkg/clientset/versioned/typed/extensions/v1alpha1/fake/fake_extensions_client.gen.go
  • client/pkg/clientset/versioned/typed/extensions/v1alpha1/generated_expansion.gen.go
  • client/pkg/informers/externalversions/extensions/v1alpha1/interface.gen.go
  • client/pkg/informers/externalversions/generic.gen.go
  • client/pkg/listers/extensions/v1alpha1/expansion_generated.gen.go
  • pkg/ingress/config/ingress_config.go
  • pkg/ingress/kube/wasmplugin/controller.go

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Comment on lines +230 to +237
message WasmPluginMatchRule {
repeated string ingress = 1;
repeated string domain = 2;
google.protobuf.Struct config = 3;
google.protobuf.BoolValue config_disable = 4;
repeated string service = 5;
string parent = 20;
} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

为新增的消息类型 WasmPluginMatchRule 添加 Status 字段,以便于跟踪其运行时状态。

🟡 Major | 🧹 Code Smells

📋 问题详情

新引入的 WasmPluginMatchRule 消息没有包含 Status 字段,这对于记录和管理插件匹配规则的实际执行状态至关重要。

💡 解决方案

WasmPluginMatchRule 中增加一个可选的 status 字段来表示当前的状态。

+  google.protobuf.Struct status = 21;

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Comment on lines +170 to +192
properties:
spec:
properties:
config:
type: object
x-kubernetes-preserve-unknown-fields: true
configDisable:
type: boolean
domain:
items:
type: string
type: array
ingress:
items:
type: string
type: array
parent:
type: string
service:
items:
type: string
type: array
type: object

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

扩展 OpenAPI 规范定义以涵盖新增的 status 属性。

🟡 Major | 🧹 Code Smells

📋 问题详情

CRD 的 OpenAPIV3Schema 定义未包括 status 字段,这意味着用户可能无法通过标准工具验证或操作这个重要属性。

💡 解决方案

status 字段添加进 CRD 的 schema 中,指定它是一个保留未知字段的对象类型。

+              status:
+                type: object
+                x-kubernetes-preserve-unknown-fields: true

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Comment on lines +95 to +105
type WasmPluginMatchRule struct {
v1.TypeMeta `json:",inline"`
// +optional
v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

// Spec defines the implementation of this definition.
// +optional
Spec extensionsv1alpha1.WasmPluginMatchRule `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

Status metav1alpha1.IstioStatus `json:"status"`
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

调整客户端侧结构体定义以反映后端模型的变化。

🟢 Minor | 🧹 Code Smells

📋 问题详情

尽管服务器端已经增加了 Status 支持,但在客户端对应的 Go 类型中也应体现这一点,否则会造成一致性问题并影响后续开发工作流。

💡 解决方案

目前的 Status 字段已经是 metav1alpha1.IstioStatus 类型,这是一个通用且合适的占位符。如果将来有更具体的结构需求,则可以考虑进一步细化此字段的类型。


您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@lcfang

lcfang commented Dec 22, 2025

Copy link
Copy Markdown
Contributor

follow
通过higress来进行rest2mcp代理的话,这个功能太需要了,否则wasmplugin的配置很快就达到了etcd的资源配置大小上限(默认1.5M)
@johnlanni 大佬看看呢


follow
If you use higress to perform rest2mcp proxy, this function is very necessary, otherwise the configuration of wasmplugin will quickly reach the upper limit of etcd resource configuration size (default 1.5M)
@johnlanni Let’s take a look

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.

2 participants