Skip to content

Commit 729dd23

Browse files
danwinshipsqueed
authored andcommitted
Vendor nftables library, add utils.SupportsIPTables and utils.SupportsNFTables
Signed-off-by: Dan Winship <[email protected]>
1 parent a6d6efa commit 729dd23

22 files changed

+3390
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/safchain/ethtool v0.4.1
2222
github.com/vishvananda/netlink v1.3.0
2323
golang.org/x/sys v0.23.0
24+
sigs.k8s.io/knftables v0.0.17
2425
)
2526

2627
require (

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
6868
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
6969
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
7070
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
71+
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
72+
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
7173
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
7274
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
7375
github.com/networkplumbing/go-nft v0.4.0 h1:kExVMwXW48DOAukkBwyI16h4uhE5lN9iMvQd52lpTyU=
@@ -194,3 +196,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
194196
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
195197
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
196198
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
199+
sigs.k8s.io/knftables v0.0.17 h1:wGchTyRF/iGTIjd+vRaR1m676HM7jB8soFtyr/148ic=
200+
sigs.k8s.io/knftables v0.0.17/go.mod h1:f/5ZLKYEUPUhVjUCg6l80ACdL7CIIyeL0DxfgojGRTk=

pkg/utils/netfilter.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 CNI authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package utils
16+
17+
import (
18+
"github.com/coreos/go-iptables/iptables"
19+
"sigs.k8s.io/knftables"
20+
)
21+
22+
// SupportsIPTables tests whether the system supports using netfilter via the iptables API
23+
// (whether via "iptables-legacy" or "iptables-nft"). (Note that this returns true if it
24+
// is *possible* to use iptables; it does not test whether any other components on the
25+
// system are *actually* using iptables.)
26+
func SupportsIPTables() bool {
27+
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
28+
if err != nil {
29+
return false
30+
}
31+
// We don't care whether the chain actually exists, only whether we can *check*
32+
// whether it exists.
33+
_, err = ipt.ChainExists("filter", "INPUT")
34+
return err == nil
35+
}
36+
37+
// SupportsNFTables tests whether the system supports using netfilter via the nftables API
38+
// (ie, not via "iptables-nft"). (Note that this returns true if it is *possible* to use
39+
// nftables; it does not test whether any other components on the system are *actually*
40+
// using nftables.)
41+
func SupportsNFTables() bool {
42+
// knftables.New() does sanity checks so we don't need any further test like in
43+
// the iptables case.
44+
_, err := knftables.New(knftables.IPv4Family, "supports_nftables_test")
45+
return err == nil
46+
}

pkg/utils/netfilter_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 CNI authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package utils
16+
17+
import (
18+
"os"
19+
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("netfilter support", func() {
25+
When("it is available", func() {
26+
It("reports that iptables is supported", func() {
27+
Expect(SupportsIPTables()).To(BeTrue(), "This test should only fail if iptables is not available, but the test suite as a whole requires it to be available.")
28+
})
29+
It("reports that nftables is supported", func() {
30+
Expect(SupportsNFTables()).To(BeTrue(), "This test should only fail if nftables is not available, but the test suite as a whole requires it to be available.")
31+
})
32+
})
33+
34+
// These are Serial because os.Setenv has process-wide effect
35+
When("it is not available", Serial, func() {
36+
var origPath string
37+
BeforeEach(func() {
38+
origPath = os.Getenv("PATH")
39+
os.Setenv("PATH", "/does-not-exist")
40+
})
41+
AfterEach(func() {
42+
os.Setenv("PATH", origPath)
43+
})
44+
45+
It("reports that iptables is not supported", func() {
46+
Expect(SupportsIPTables()).To(BeFalse(), "found iptables outside of PATH??")
47+
})
48+
It("reports that nftables is not supported", func() {
49+
Expect(SupportsNFTables()).To(BeFalse(), "found nftables outside of PATH??")
50+
})
51+
})
52+
})

vendor/modules.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,6 @@ google.golang.org/protobuf/types/known/anypb
267267
# gopkg.in/yaml.v3 v3.0.1
268268
## explicit
269269
gopkg.in/yaml.v3
270+
# sigs.k8s.io/knftables v0.0.17
271+
## explicit; go 1.20
272+
sigs.k8s.io/knftables

vendor/sigs.k8s.io/knftables/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/sigs.k8s.io/knftables/CHANGELOG.md

Lines changed: 170 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/sigs.k8s.io/knftables/CONTRIBUTING.md

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)