Skip to content

Commit ba22a30

Browse files
author
maxgio92
authored
Network: Rework IPv6 check. (#12905)
Signed-off-by: Massimiliano Giovagnoli <me@maxgio.it>
1 parent f32b6dd commit ba22a30

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

internal/net/net.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package net
1919
import (
2020
"fmt"
2121
_net "net"
22-
"os/exec"
22+
"os"
23+
"strings"
2324
)
2425

2526
// IsIPV6 checks if the input contains a valid IPV6 address
@@ -41,11 +42,25 @@ func IsPortAvailable(p int) bool {
4142
// IsIPv6Enabled checks if IPV6 is enabled or not and we have
4243
// at least one configured in the pod
4344
func IsIPv6Enabled() bool {
44-
cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
45-
if cmd.Run() != nil {
45+
// Skip interface checks if the IPv6 kernel feature is disabled.
46+
disable, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
47+
if err != nil {
48+
return false
49+
}
50+
if strings.TrimSpace(string(disable)) == "1" {
51+
return false
52+
}
53+
54+
// Check that there are interfaces with IPv6 enabled.
55+
ifaces, err := os.Stat("/proc/net/if_inet6")
56+
if err != nil {
57+
return false
58+
}
59+
if ifaces.IsDir() {
4660
return false
4761
}
4862

63+
// Check IPv6 addresses on interfaces.
4964
addrs, err := _net.InterfaceAddrs()
5065
if err != nil {
5166
return false

internal/net/net_ipv6_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build test_ipv6
2+
3+
/*
4+
Copyright 2015 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package net
20+
21+
import "testing"
22+
23+
func TestIsIPv6Enabled(t *testing.T) {
24+
isEnabled := IsIPv6Enabled()
25+
if !isEnabled {
26+
t.Fatalf("expected IPV6 be enabled")
27+
}
28+
}

internal/net/net_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,3 @@ func TestIsPortAvailable(t *testing.T) {
5858
t.Fatalf("expected port %v to not be available", p)
5959
}
6060
}
61-
62-
/*
63-
// TODO: this test should be optional or running behind a flag
64-
func TestIsIPv6Enabled(t *testing.T) {
65-
isEnabled := IsIPv6Enabled()
66-
if !isEnabled {
67-
t.Fatalf("expected IPV6 be enabled")
68-
}
69-
}
70-
*/

0 commit comments

Comments
 (0)