Properly create hostname from IPv6 (#7431)

Generate valid hostname from IPv6 when the address ends with `::`.

Signed-off-by: Guillaume Jacquet <guillaume.jacquet@gmail.com>
This commit is contained in:
Guillaume Jacquet 2025-08-04 19:53:40 -04:00 committed by GitHub
parent 17020f09a8
commit 1025a199e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -351,7 +351,11 @@ func endpointHostname(addr object.EndpointAddress, endpointNameMode bool) string
return strings.ReplaceAll(addr.IP, ".", "-")
}
if strings.Contains(addr.IP, ":") {
return strings.ReplaceAll(addr.IP, ":", "-")
ipv6Hostname := strings.ReplaceAll(addr.IP, ":", "-")
if strings.HasSuffix(ipv6Hostname, "-") {
return ipv6Hostname + "0"
}
return ipv6Hostname
}
return ""
}

View File

@ -30,6 +30,8 @@ func TestEndpointHostname(t *testing.T) {
{"10.11.12.13", "epname", "epname", "hello-abcde", false},
{"10.11.12.13", "epname", "epname", "hello-abcde", true},
{"10.11.12.13", "", "hello-abcde", "hello-abcde", true},
{"2001:db8:3333:4444:5555:6666:7777:8888", "", "2001-db8-3333-4444-5555-6666-7777-8888", "", false},
{"2001:db8:3333:4444:5555:6666::", "", "2001-db8-3333-4444-5555-6666--0", "", false},
}
for _, test := range tests {
result := endpointHostname(object.EndpointAddress{IP: test.ip, Hostname: test.hostname, TargetRefName: test.podName}, test.endpointNameMode)