Fix IPV6 detection
This commit is contained in:
parent
a68820808a
commit
123ffc0c38
3 changed files with 34 additions and 3 deletions
|
|
@ -24,7 +24,7 @@ import (
|
|||
|
||||
// IsIPV6 checks if the input contains a valid IPV6 address
|
||||
func IsIPV6(ip _net.IP) bool {
|
||||
return ip.To4() == nil
|
||||
return ip != nil && ip.To4() == nil
|
||||
}
|
||||
|
||||
// IsPortAvailable checks if a TCP port is available or not
|
||||
|
|
@ -37,8 +37,25 @@ func IsPortAvailable(p int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// IsIPv6Enabled checks if IPV6 is enabled or not
|
||||
// IsIPv6Enabled checks if IPV6 is enabled or not and we have
|
||||
// at least one configured in the pod
|
||||
func IsIPv6Enabled() bool {
|
||||
cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
|
||||
return cmd.Run() == nil
|
||||
if cmd.Run() != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
addrs, err := _net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
ip, _, _ := _net.ParseCIDR(addr.String())
|
||||
if IsIPV6(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,3 +58,10 @@ func TestIsPortAvailable(t *testing.T) {
|
|||
t.Fatalf("expected port %v to not be available", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIPv6Enabled(t *testing.T) {
|
||||
isEnabled := IsIPv6Enabled()
|
||||
if !isEnabled {
|
||||
t.Fatalf("expected IPV6 be enabled")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue