Skip to content

Commit 53bb829

Browse files
authored
Merge pull request kubernetes#84712 from chendotjs/patch-cc
Refactor the process to get ip address of loopback interface
2 parents 0ed6635 + d9cbad7 commit 53bb829

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

staging/src/k8s.io/apiserver/pkg/server/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ go_test(
4040
"//vendor/github.com/go-openapi/spec:go_default_library",
4141
"//vendor/github.com/stretchr/testify/assert:go_default_library",
4242
"//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
43+
"//vendor/k8s.io/utils/net:go_default_library",
4344
],
4445
)
4546

@@ -120,6 +121,7 @@ go_library(
120121
"//vendor/k8s.io/kube-openapi/pkg/handler:go_default_library",
121122
"//vendor/k8s.io/kube-openapi/pkg/util:go_default_library",
122123
"//vendor/k8s.io/kube-openapi/pkg/util/proto:go_default_library",
124+
"//vendor/k8s.io/utils/net:go_default_library",
123125
],
124126
)
125127

staging/src/k8s.io/apiserver/pkg/server/config_selfclient.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222

2323
restclient "k8s.io/client-go/rest"
24+
netutils "k8s.io/utils/net"
2425
)
2526

2627
// LoopbackClientServerNameOverride is passed to the apiserver from the loopback client in order to
@@ -70,23 +71,27 @@ func LoopbackHostPort(bindAddress string) (string, string, error) {
7071
return "", "", fmt.Errorf("invalid server bind address: %q", bindAddress)
7172
}
7273

73-
isIPv6 := net.ParseIP(host).To4() == nil
74+
isIPv6 := netutils.IsIPv6String(host)
7475

7576
// Value is expected to be an IP or DNS name, not "0.0.0.0".
7677
if host == "0.0.0.0" || host == "::" {
77-
host = "localhost"
7878
// Get ip of local interface, but fall back to "localhost".
7979
// Note that "localhost" is resolved with the external nameserver first with Go's stdlib.
8080
// So if localhost.<yoursearchdomain> resolves, we don't get a 127.0.0.1 as expected.
81-
addrs, err := net.InterfaceAddrs()
82-
if err == nil {
83-
for _, address := range addrs {
84-
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && isIPv6 == (ipnet.IP.To4() == nil) {
85-
host = ipnet.IP.String()
86-
break
87-
}
81+
host = getLoopbackAddress(isIPv6)
82+
}
83+
return host, port, nil
84+
}
85+
86+
// getLoopbackAddress returns the ip address of local loopback interface. If any error occurs or loopback interface is not found, will fall back to "localhost"
87+
func getLoopbackAddress(wantIPv6 bool) string {
88+
addrs, err := net.InterfaceAddrs()
89+
if err == nil {
90+
for _, address := range addrs {
91+
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && wantIPv6 == netutils.IsIPv6(ipnet.IP) {
92+
return ipnet.IP.String()
8893
}
8994
}
9095
}
91-
return host, port, nil
96+
return "localhost"
9297
}

staging/src/k8s.io/apiserver/pkg/server/config_selfclient_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package server
1919
import (
2020
"net"
2121
"testing"
22+
23+
netutils "k8s.io/utils/net"
2224
)
2325

2426
func TestLoopbackHostPortIPv4(t *testing.T) {
@@ -95,7 +97,7 @@ func isIPv6LoopbackSupported() (ipv6 bool, ipv6only bool, err error) {
9597
if !ok || !ipnet.IP.IsLoopback() {
9698
continue
9799
}
98-
if ipnet.IP.To4() == nil {
100+
if netutils.IsIPv6(ipnet.IP) {
99101
ipv6 = true
100102
continue
101103
}

0 commit comments

Comments
 (0)