From bdce9aa94b203ac3cccb6463c7fa740373b9244e Mon Sep 17 00:00:00 2001 From: Massimiliano Giovagnoli Date: Thu, 27 Feb 2025 10:26:15 +0100 Subject: [PATCH 1/2] refactor(internal/net/net.go): remove ipv6 check with test shell builtin The previous check was requiring the test shell builtin to check interface with IPv6 under procfs. The same has been achieved with this commit using go "os" standard library. Furthermore a preliminary check on the kernel-level setting to disable IPv6 is introduced, such that id IPv6 is disabled as kernel feature no further checks on interfaces is done in order to check if IPv6 is enabled. Signed-off-by: Massimiliano Giovagnoli --- internal/net/net.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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 From 8ce61fe0ef755b696f179e9a6f3333e356d9e626 Mon Sep 17 00:00:00 2001 From: Massimiliano Giovagnoli Date: Thu, 27 Feb 2025 10:42:16 +0100 Subject: [PATCH 2/2] test(internal/net): enable ipv6 test with test_ipv6 build flag Signed-off-by: Massimiliano Giovagnoli --- internal/net/net_ipv6_test.go | 28 ++++++++++++++++++++++++++++ internal/net/net_test.go | 10 ---------- 2 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 internal/net/net_ipv6_test.go 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") - } -} -*/