Merge pull request #84712 from chendotjs/patch-cc

Refactor the process to get ip address of loopback interface
This commit is contained in:
Kubernetes Prow Robot 2019-11-04 10:31:57 -08:00 committed by GitHub
commit 53bb82994a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 12 deletions

View File

@ -40,6 +40,7 @@ go_test(
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
"//vendor/k8s.io/utils/net:go_default_library",
],
)
@ -120,6 +121,7 @@ go_library(
"//vendor/k8s.io/kube-openapi/pkg/handler:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/util:go_default_library",
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
"//vendor/k8s.io/utils/net:go_default_library",
],
)

View File

@ -21,6 +21,7 @@ import (
"net"
restclient "k8s.io/client-go/rest"
netutils "k8s.io/utils/net"
)
// LoopbackClientServerNameOverride is passed to the apiserver from the loopback client in order to
@ -70,23 +71,27 @@ func LoopbackHostPort(bindAddress string) (string, string, error) {
return "", "", fmt.Errorf("invalid server bind address: %q", bindAddress)
}
isIPv6 := net.ParseIP(host).To4() == nil
isIPv6 := netutils.IsIPv6String(host)
// Value is expected to be an IP or DNS name, not "0.0.0.0".
if host == "0.0.0.0" || host == "::" {
host = "localhost"
// Get ip of local interface, but fall back to "localhost".
// Note that "localhost" is resolved with the external nameserver first with Go's stdlib.
// So if localhost.<yoursearchdomain> resolves, we don't get a 127.0.0.1 as expected.
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && isIPv6 == (ipnet.IP.To4() == nil) {
host = ipnet.IP.String()
break
}
}
}
host = getLoopbackAddress(isIPv6)
}
return host, port, nil
}
// getLoopbackAddress returns the ip address of local loopback interface. If any error occurs or loopback interface is not found, will fall back to "localhost"
func getLoopbackAddress(wantIPv6 bool) string {
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && wantIPv6 == netutils.IsIPv6(ipnet.IP) {
return ipnet.IP.String()
}
}
}
return "localhost"
}

View File

@ -19,6 +19,8 @@ package server
import (
"net"
"testing"
netutils "k8s.io/utils/net"
)
func TestLoopbackHostPortIPv4(t *testing.T) {
@ -95,7 +97,7 @@ func isIPv6LoopbackSupported() (ipv6 bool, ipv6only bool, err error) {
if !ok || !ipnet.IP.IsLoopback() {
continue
}
if ipnet.IP.To4() == nil {
if netutils.IsIPv6(ipnet.IP) {
ipv6 = true
continue
}