diff --git a/internal/net/net.go b/internal/net/net.go index b0952d9cc1..70dd156ca1 100644 --- a/internal/net/net.go +++ b/internal/net/net.go @@ -19,7 +19,8 @@ package net import ( "fmt" _net "net" - "os/exec" + "os" + "strings" ) // IsIPV6 checks if the input contains a valid IPV6 address @@ -41,11 +42,25 @@ func IsPortAvailable(p int) bool { // 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") - if cmd.Run() != nil { + // Skip interface checks if the IPv6 kernel feature is disabled. + disable, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6") + if err != nil { + return false + } + if strings.TrimSpace(string(disable)) == "1" { + return false + } + + // Check that there are interfaces with IPv6 enabled. + ifaces, err := os.Stat("/proc/net/if_inet6") + if err != nil { + return false + } + if ifaces.IsDir() { return false } + // Check IPv6 addresses on interfaces. addrs, err := _net.InterfaceAddrs() if err != nil { return false diff --git a/internal/net/net_ipv6_test.go b/internal/net/net_ipv6_test.go new file mode 100644 index 0000000000..c27658d318 --- /dev/null +++ b/internal/net/net_ipv6_test.go @@ -0,0 +1,28 @@ +//go:build test_ipv6 + +/* +Copyright 2015 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package net + +import "testing" + +func TestIsIPv6Enabled(t *testing.T) { + isEnabled := IsIPv6Enabled() + if !isEnabled { + t.Fatalf("expected IPV6 be enabled") + } +} diff --git a/internal/net/net_test.go b/internal/net/net_test.go index f7a4f52e10..d943ab28b7 100644 --- a/internal/net/net_test.go +++ b/internal/net/net_test.go @@ -58,13 +58,3 @@ func TestIsPortAvailable(t *testing.T) { t.Fatalf("expected port %v to not be available", p) } } - -/* -// TODO: this test should be optional or running behind a flag -func TestIsIPv6Enabled(t *testing.T) { - isEnabled := IsIPv6Enabled() - if !isEnabled { - t.Fatalf("expected IPV6 be enabled") - } -} -*/