mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Modify LoopbackHostPort() so it returns an IPv6 Loopback address when given [::] address
Currently when LoopbackHostPort() is called with 0.0.0.0 and [::] it returns the first loopback address returned from net.InterfaceAddrs() which is typically 127.0.0.1 (golang does not specify an order that interfaces are returned). It would be more appropriate if when calling LoopbackHostPort() with [::] that an IPv6 loopback address is returned, this prevents some cert. generation failures.
This commit is contained in:
parent
080739a12a
commit
14a03dd646
@ -63,6 +63,8 @@ func LoopbackHostPort(bindAddress string) (string, string, error) {
|
||||
return "", "", fmt.Errorf("invalid server bind address: %q", bindAddress)
|
||||
}
|
||||
|
||||
isIPv6 := net.ParseIP(host).To4() == nil
|
||||
|
||||
// Value is expected to be an IP or DNS name, not "0.0.0.0".
|
||||
if host == "0.0.0.0" || host == "::" {
|
||||
host = "localhost"
|
||||
@ -72,7 +74,7 @@ func LoopbackHostPort(bindAddress string) (string, string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err == nil {
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && isIPv6 == (ipnet.IP.To4() == nil) {
|
||||
host = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
|
@ -59,9 +59,10 @@ func TestLoopbackHostPort(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() {
|
||||
t.Fatalf("expected host to be loopback, got %q", host)
|
||||
if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() || ip.To4() != nil {
|
||||
t.Fatalf("expected IPv6 host to be loopback, got %q", host)
|
||||
}
|
||||
|
||||
if port != "443" {
|
||||
t.Fatalf("expected 443 as port, got %q", port)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user