Skip to content

Network: Rework IPv6 check. #12905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package net
import (
"fmt"
_net "net"
"os/exec"
"os"
"strings"
)

// IsIPV6 checks if the input contains a valid IPV6 address
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions internal/net/net_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
10 changes: 0 additions & 10 deletions internal/net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
*/
Loading