File tree Expand file tree Collapse file tree 3 files changed +46
-13
lines changed Expand file tree Collapse file tree 3 files changed +46
-13
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,8 @@ package net
1919import (
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
4344func 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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- */
You can’t perform that action at this time.
0 commit comments