-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: support ipv6 single stack network
- Loading branch information
1 parent
190f6fe
commit cfe0d6e
Showing
13 changed files
with
160 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ip | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
utilnet "k8s.io/utils/net" | ||
) | ||
|
||
const ( | ||
DefaultLoopbackIP4 = "127.0.0.1" | ||
DefaultLoopbackIP6 = "::1" | ||
) | ||
|
||
// MustGetLoopbackIP is a wrapper for GetLoopbackIP. If any error occurs or loopback interface is not found, | ||
// will fall back to 127.0.0.1 for ipv4 or ::1 for ipv6. | ||
func MustGetLoopbackIP(wantIPv6 bool) string { | ||
ip, err := GetLoopbackIP(wantIPv6) | ||
if err != nil { | ||
klog.Errorf("failed to get loopback addr: %v", err) | ||
} | ||
if ip != "" { | ||
return ip | ||
} | ||
if wantIPv6 { | ||
return DefaultLoopbackIP6 | ||
} | ||
return DefaultLoopbackIP4 | ||
} | ||
|
||
// GetLoopbackIP returns the ip address of local loopback interface. | ||
func GetLoopbackIP(wantIPv6 bool) (string, error) { | ||
addrs, err := net.InterfaceAddrs() | ||
if err != nil { | ||
return "", err | ||
} | ||
for _, address := range addrs { | ||
if ipnet, ok := address.(*net.IPNet); ok && ipnet.IP.IsLoopback() && wantIPv6 == utilnet.IsIPv6(ipnet.IP) { | ||
return ipnet.IP.String(), nil | ||
} | ||
} | ||
return "", nil | ||
} | ||
|
||
func JoinIPStrings(ips []net.IP) string { | ||
var strs []string | ||
for _, ip := range ips { | ||
strs = append(strs, ip.String()) | ||
} | ||
return strings.Join(strs, ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ip | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetLoopbackIP(t *testing.T) { | ||
lo4, err := GetLoopbackIP(false) | ||
if err != nil { | ||
t.Errorf("failed to get ipv4 loopback address: %v", err) | ||
} | ||
t.Logf("got ipv4 loopback address: %s", lo4) | ||
if lo4 != "127.0.0.1" { | ||
t.Errorf("got ipv4 loopback addr: '%s', expect: '127.0.0.1'", lo4) | ||
} | ||
|
||
lo6, err := GetLoopbackIP(true) | ||
if err != nil { | ||
t.Errorf("failed to get ipv6 loopback address: %v", err) | ||
} | ||
if lo6 != "" { | ||
// dual stack env | ||
t.Logf("got ipv6 loopback address: %s", lo6) | ||
if lo6 != "::1" { | ||
t.Errorf("got ipv6 loopback addr: '%s', expect: '::1'", lo6) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.