Skip to content

🟢 [Low Priority] 代码重复 - VNI-aware identifier 逻辑 #14

@xgopilot

Description

@xgopilot

优先级

🟢 低优先级

问题描述

VNI-aware identifier 生成逻辑在两个地方重复实现:

  1. pkg/endpoint/identifiers.go:138 - endpoint.Identifiers() 方法
  2. pkg/endpoint/api/endpoint_api_manager.go:277-291 - IP 冲突检测

这种重复导致:

  • 维护负担(需要同步修改)
  • 潜在的不一致风险
  • 代码可读性降低

影响范围

代码质量:

  • 违反 DRY (Don't Repeat Yourself) 原则
  • 增加维护成本
  • 未来修改时容易遗漏其中一处

相关代码位置

  • 文件: pkg/endpoint/identifiers.go:138
  • 文件: pkg/endpoint/api/endpoint_api_manager.go:277-291
  • PR: 7niu v1.19.0 rc.1 #6

当前状态

建议的解决方案

提取为共享辅助方法:

// 在 pkg/endpoint/endpoint.go 中添加
func (e *Endpoint) GetIPIdentifier(ip netip.Addr) string {
    if !ip.IsValid() {
        return ""
    }
    if e.VNIID > 0 {
        return id.NewVNIIPPrefixID(ip, e.VNIID)
    }
    return id.NewIPPrefixID(ip)
}

然后在两处使用该方法:

// pkg/endpoint/identifiers.go
if e.IPv4.IsValid() {
    refs[id.IPv4Prefix] = e.GetIPIdentifier(e.IPv4)
}

// pkg/endpoint/api/endpoint_api_manager.go
if ep.IPv4.IsValid() {
    checkIDs = append(checkIDs, ep.GetIPIdentifier(ep.IPv4))
}

额外改进

  • 添加单元测试确保两处逻辑一致
  • 考虑为 IPv6 也提供类似的辅助方法
  • 在代码注释中说明 identifier 生成的规则

相关链接


此 issue 由 PR #6 code review 自动生成

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions