Skip to content

Conversation

@erasernoob
Copy link
Collaborator

Ⅰ. Describe what this PR did

fix #2626
Support external FQDN in mirror annotation and add test.

Ⅱ. 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

@lingma-agents
Copy link

lingma-agents bot commented Jul 29, 2025

支持在镜像注解中使用外部服务FQDN

变更概述
  • 新功能
    • 新增对镜像注解中外部FQDN的支持,允许将流量镜像到外部服务
    • 添加了两个新的注解键:mirror-target-fqdnmirror-target-fqdn-port,用于指定外部服务的域名和端口
    • 扩展了 MirrorConfig 结构体,新增 FQDNFPort 字段以存储外部服务信息
    • 实现了优先解析FQDN的逻辑,如果未设置FQDN则回退到原有服务解析逻辑
  • 测试更新
    • mirror_test.go 中新增了针对FQDN场景的测试用例
    • 包含了仅设置FQDN和同时设置FQDN及端口的测试场景
    • 添加了 ApplyRoute 方法中处理FQDN的测试逻辑验证
  • 重构
    • 将百分比解析逻辑提取为独立的 parsePercentage 函数,提升代码复用性
    • 优化了 ApplyRoute 方法中目标主机和端口的判断逻辑,支持FQDN和内部服务的统一处理
变更文件
文件路径 变更说明
pkg/​ingress/​kube/​annotations/​mirror.​go 新增对外部FQDN的支持,添加了新的注解常量和配置字段,重构了解析和应用逻辑以支持外部服务镜像。
pkg/​ingress/​kube/​annotations/​mirror_​test.​go 增加了针对新增FQDN功能的测试用例,包括解析和路由应用的验证。
时序图
sequenceDiagram
    participant A as Annotations
    participant M as Mirror Parser
    participant C as Ingress Config
    participant R as HTTP Route
    
    A->>M: Parse annotations
    alt FQDN exists
        M->>M: Parse FQDN and port
        M->>C: Set MirrorConfig with FQDN
    else Service exists
        M->>M: Parse service info
        M->>C: Set MirrorConfig with service
    end
    C->>M: ApplyRoute
    alt FQDN configured
        M->>R: Set mirror destination with FQDN
    else Service configured
        M->>R: Set mirror destination with service FQDN
    end
Loading

💡 小贴士

与 lingma-agents 交流的方式

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

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

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

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

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

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

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

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

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

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

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

🔎 代码评审报告

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

总计: 3 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
🔹 pkg/ingress/kube/annotations/mirror.go (2 💬)
🔹 pkg/ingress/kube/annotations/mirror_test.go (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. FQDN解析逻辑与服务发现逻辑存在重复代码风险

mirror.go中,FQDN和Service的解析逻辑分别独立实现,但两者在构建MirrorConfig时结构相似。建议将共用逻辑抽象为统一的解析函数,以减少重复代码并提升可维护性。

📌 关键代码

if fadn, err := annotations.ParseStringASAP(mirrorTargetFQDN); err == nil {
		// default is 80
		var port uint32
		port = 80

		if p, err := annotations.ParseInt32ASAP(mirrorTargetFQDNPort); err == nil {
			port = uint32(p)
		}

		config.Mirror = &MirrorConfig{
			Percentage: parsePercentage(annotations),
			FQDN:       fadn,
			FPort:      port,
		}
		return nil
	}
config.Mirror = &MirrorConfig{
		ServiceInfo: serviceInfo,
		Percentage:  parsePercentage(annotations),
	}

⚠️ 潜在风险

未来如果需要修改MirrorConfig的构造方式,可能需要同时修改两处逻辑,增加维护成本和出错风险。

🔍2. MirrorConfig结构体设计可能导致扩展性问题

当前MirrorConfig结构体中同时包含ServiceInfo和FQDN相关字段,这种混合使用方式可能会导致结构体职责不清。建议将FQDN相关的配置单独封装成一个子结构体,以提高结构体的清晰度和可扩展性。

📌 关键代码

type MirrorConfig struct {
	util.ServiceInfo
	Percentage *wrappers.DoubleValue
	FQDN       string
	FPort      uint32 // Port for FQDN
}

⚠️ 潜在风险

随着功能的扩展,MirrorConfig可能会变得越来越复杂,难以维护和理解。

🔍3. 测试用例未覆盖FQDN和Service同时配置的情况

当前测试用例只单独测试了FQDN或Service的配置情况,没有测试当两者同时配置时的行为是否符合预期。这可能导致在实际使用中出现不可预知的问题。

📌 关键代码

{
			input: []map[string]string{
				{buildHigressAnnotationKey(mirrorTargetFQDN): "www.example.com"},
				{buildNginxAnnotationKey(mirrorTargetFQDN): "www.example.com"},
			},
			expect: &MirrorConfig{
				ServiceInfo: util.ServiceInfo{},
				FQDN:        "www.example.com",
				FPort:       80,
			},
		},

⚠️ 潜在风险

在生产环境中,用户可能会同时配置FQDN和服务名称,若系统行为不符合预期,可能会导致流量镜像失败或错误地路由到不正确的服务。

🔍4. FQDN端口默认值硬编码可能引发安全或兼容性问题

在FQDN解析逻辑中,默认端口被硬编码为80。虽然这是一个常见的HTTP端口,但在某些场景下(如HTTPS服务),默认端口应为443。建议通过配置或注解支持默认端口的自定义,以增强灵活性和安全性。

📌 关键代码

// default is 80
		var port uint32
		port = 80

⚠️ 潜在风险

如果目标服务运行在非80端口上,而用户未显式指定端口,则流量可能无法正确到达目标,导致服务不可用或数据泄露。

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

📝 变更文件:

  • pkg/ingress/kube/annotations/mirror.go
  • pkg/ingress/kube/annotations/mirror_test.go

💡 小贴士

与 lingma-agents 交流的方式

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

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

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

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

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

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

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

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

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

@codecov-commenter
Copy link

codecov-commenter commented Jul 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46.16%. Comparing base (ef31e09) to head (2627c2b).
⚠️ Report is 626 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2679       +/-   ##
===========================================
+ Coverage   35.91%   46.16%   +10.25%     
===========================================
  Files          69       81       +12     
  Lines       11576    13047     +1471     
===========================================
+ Hits         4157     6023     +1866     
+ Misses       7104     6678      -426     
- Partials      315      346       +31     
Files with missing lines Coverage Δ
pkg/ingress/kube/annotations/mirror.go 60.43% <100.00%> (ø)

... and 77 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

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

LGTM

@johnlanni johnlanni merged commit 2768022 into alibaba:main Jul 30, 2025
8 checks passed
ink-hz pushed a commit to ink-hz/higress-ai-capability-auth that referenced this pull request Nov 5, 2025
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.

Mirror annotation needs to support services other than k8s services.

3 participants