feat: support upstream ipv6 static address (#3384)

Co-authored-by: EricaLiu <30773688+Erica177@users.noreply.github.com>
This commit is contained in:
ThxCode-Chen
2026-01-26 17:30:09 +08:00
committed by GitHub
parent 255f0bde76
commit 819f773297

View File

@@ -145,22 +145,43 @@ func (w *watcher) generateServiceEntry(host string) *v1alpha3.ServiceEntry {
for _, ep := range strings.Split(w.Domain, common.CommaSeparator) { for _, ep := range strings.Split(w.Domain, common.CommaSeparator) {
var endpoint *v1alpha3.WorkloadEntry var endpoint *v1alpha3.WorkloadEntry
if w.Type == string(registry.Static) { if w.Type == string(registry.Static) {
pair := strings.Split(ep, common.ColonSeparator) var ip string
if len(pair) != 2 { var portStr string
log.Errorf("invalid endpoint:%s with static type", ep) // Support IPv6 format: [2001:db8::1]:8080
return nil if strings.HasPrefix(ep, "[") {
// IPv6 format: [IPv6]:port
lastBracket := strings.LastIndex(ep, "]")
if lastBracket == -1 {
log.Errorf("invalid IPv6 endpoint format:%s with static type", ep)
return nil
}
ip = ep[1:lastBracket] // Extract IPv6 address without brackets
if lastBracket+1 >= len(ep) || ep[lastBracket+1] != ':' {
log.Errorf("invalid IPv6 endpoint format:%s with static type, missing colon after bracket", ep)
return nil
}
portStr = ep[lastBracket+2:] // Extract port after "]:"
} else {
// IPv4 format: 192.168.1.1:8080
pair := strings.Split(ep, common.ColonSeparator)
if len(pair) != 2 {
log.Errorf("invalid endpoint:%s with static type", ep)
return nil
}
ip = pair[0]
portStr = pair[1]
} }
port, err := strconv.ParseUint(pair[1], 10, 32) port, err := strconv.ParseUint(portStr, 10, 32)
if err != nil { if err != nil {
log.Errorf("invalid port:%s of endpoint:%s", pair[1], ep) log.Errorf("invalid port:%s of endpoint:%s", portStr, ep)
return nil return nil
} }
if net.ParseIP(pair[0]) == nil { if net.ParseIP(ip) == nil {
log.Errorf("invalid ip:%s of endpoint:%s", pair[0], ep) log.Errorf("invalid ip:%s of endpoint:%s", ip, ep)
return nil return nil
} }
endpoint = &v1alpha3.WorkloadEntry{ endpoint = &v1alpha3.WorkloadEntry{
Address: pair[0], Address: ip,
Ports: map[string]uint32{protocol: uint32(port)}, Ports: map[string]uint32{protocol: uint32(port)},
} }
} else if w.Type == string(registry.DNS) { } else if w.Type == string(registry.DNS) {
@@ -247,3 +268,4 @@ func (w *watcher) getSni(se *v1alpha3.ServiceEntry) string {
func (w *watcher) GetRegistryType() string { func (w *watcher) GetRegistryType() string {
return w.RegistryConfig.Type return w.RegistryConfig.Type
} }