Skip to content

Commit 2189ddb

Browse files
authored
Merge branch 'main' into bridge-clean-masq-if-netns-empty
2 parents bbf8920 + fa737f8 commit 2189ddb

68 files changed

Lines changed: 5443 additions & 2297 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ jobs:
1313
matrix:
1414
goarch: [amd64, arm, arm64, mips64le, ppc64le, riscv64, s390x]
1515
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
1619
- name: Install Go
1720
uses: actions/setup-go@v5
1821
with:
19-
go-version: 1.22
20-
21-
- name: Checkout code
22-
uses: actions/checkout@v4
22+
go-version-file: go.mod
2323

2424
- name: Build
2525
env:
@@ -68,13 +68,13 @@ jobs:
6868
- name: Install dos2unix
6969
run: sudo apt-get install dos2unix
7070

71+
- name: Checkout code
72+
uses: actions/checkout@v4
73+
7174
- name: Install Go
7275
uses: actions/setup-go@v5
7376
with:
74-
go-version: 1.21
75-
76-
- name: Checkout code
77-
uses: actions/checkout@v4
77+
go-version-file: go.mod
7878

7979
- name: Build
8080
env:

.github/workflows/test.yaml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ on:
55
pull_request: {}
66

77
env:
8-
# be sure to update the version in release.yaml too
9-
GO_VERSION: "1.22"
108
LINUX_ARCHES: "amd64 386 arm arm64 s390x mips64le ppc64le riscv64"
119

1210
jobs:
@@ -18,15 +16,28 @@ jobs:
1816
- name: setup go
1917
uses: actions/setup-go@v5
2018
with:
21-
go-version: ${{ env.GO_VERSION }}
19+
go-version-file: go.mod
2220
- uses: ibiqlik/action-yamllint@v3
2321
with:
2422
format: auto
2523
- uses: golangci/golangci-lint-action@v6
2624
with:
27-
version: v1.55.2
25+
version: v1.61.0
2826
args: -v
29-
skip-cache: true
27+
verify-vendor:
28+
name: Verify vendor directory
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
- name: Install Go
33+
uses: actions/setup-go@v5
34+
with:
35+
go-version-file: go.mod
36+
- name: Check module vendoring
37+
run: |
38+
go mod tidy
39+
go mod vendor
40+
test -z "$(git status --porcelain)" || (echo "please run 'go mod tidy && go mod vendor', and submit your changes"; exit 1)
3041
build:
3142
name: Build all linux architectures
3243
needs: lint
@@ -36,7 +47,7 @@ jobs:
3647
- name: setup go
3748
uses: actions/setup-go@v5
3849
with:
39-
go-version: ${{ env.GO_VERSION }}
50+
go-version-file: go.mod
4051
- name: Build on all supported architectures
4152
run: |
4253
set -e
@@ -60,7 +71,7 @@ jobs:
6071
- name: setup go
6172
uses: actions/setup-go@v5
6273
with:
63-
go-version: ${{ env.GO_VERSION }}
74+
go-version-file: go.mod
6475
- name: Set up Go for root
6576
run: |
6677
sudo ln -sf `which go` `sudo which go` || true
@@ -91,6 +102,6 @@ jobs:
91102
- name: setup go
92103
uses: actions/setup-go@v5
93104
with:
94-
go-version: ${{ env.GO_VERSION }}
105+
go-version-file: go.mod
95106
- name: test
96107
run: bash ./test_windows.sh

.golangci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ linters-settings:
4040
- prefix(github.com/containernetworking)
4141

4242
run:
43-
skip-dirs:
44-
- vendor
4543
timeout: 5m
44+
modules-download-mode: vendor

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/containernetworking/plugins
22

3-
go 1.21
3+
go 1.23
44

55
require (
66
github.com/Microsoft/hcsshim v0.12.6
@@ -15,12 +15,13 @@ require (
1515
github.com/godbus/dbus/v5 v5.1.0
1616
github.com/mattn/go-shellwords v1.0.12
1717
github.com/networkplumbing/go-nft v0.4.0
18-
github.com/onsi/ginkgo/v2 v2.20.1
19-
github.com/onsi/gomega v1.34.1
18+
github.com/onsi/ginkgo/v2 v2.20.2
19+
github.com/onsi/gomega v1.34.2
2020
github.com/opencontainers/selinux v1.11.0
2121
github.com/safchain/ethtool v0.4.1
2222
github.com/vishvananda/netlink v1.3.0
23-
golang.org/x/sys v0.23.0
23+
golang.org/x/sys v0.24.0
24+
sigs.k8s.io/knftables v0.0.17
2425
)
2526

2627
require (
@@ -32,12 +33,11 @@ require (
3233
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
3334
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3435
github.com/google/go-cmp v0.6.0 // indirect
35-
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
36+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
3637
github.com/pkg/errors v0.9.1 // indirect
3738
github.com/sirupsen/logrus v1.9.3 // indirect
3839
github.com/vishvananda/netns v0.0.4 // indirect
3940
go.opencensus.io v0.24.0 // indirect
40-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
4141
golang.org/x/net v0.28.0 // indirect
4242
golang.org/x/text v0.17.0 // indirect
4343
golang.org/x/tools v0.24.0 // indirect

go.sum

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
6565
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6666
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6767
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
68-
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
69-
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
68+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
69+
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
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=
7476
github.com/networkplumbing/go-nft v0.4.0/go.mod h1:HnnM+tYvlGAsMU7yoYwXEVLLiDW9gdMmb5HoGcwpuQs=
75-
github.com/onsi/ginkgo/v2 v2.20.1 h1:YlVIbqct+ZmnEph770q9Q7NVAz4wwIiVNahee6JyUzo=
76-
github.com/onsi/ginkgo/v2 v2.20.1/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
77-
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
78-
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
77+
github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4=
78+
github.com/onsi/ginkgo/v2 v2.20.2/go.mod h1:K9gyxPIlb+aIvnZ8bd9Ak+YP18w3APlR+5coaZoE2ag=
79+
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
80+
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
7981
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
8082
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
8183
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -108,8 +110,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
108110
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
109111
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
110112
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
111-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
112-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
113113
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
114114
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
115115
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@@ -141,8 +141,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
141141
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
142142
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
143143
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
144-
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
145-
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
144+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
145+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
146146
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
147147
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
148148
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -194,3 +194,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
194194
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
195195
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
196196
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
197+
sigs.k8s.io/knftables v0.0.17 h1:wGchTyRF/iGTIjd+vRaR1m676HM7jB8soFtyr/148ic=
198+
sigs.k8s.io/knftables v0.0.17/go.mod h1:f/5ZLKYEUPUhVjUCg6l80ACdL7CIIyeL0DxfgojGRTk=

pkg/errors/errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestAnnotate(t *testing.T) {
4343

4444
for _, test := range tests {
4545
t.Run(test.name, func(t *testing.T) {
46-
if !reflect.DeepEqual(Annotatef(test.existingErr, test.contextMessage), test.expectedErr) {
46+
if !reflect.DeepEqual(Annotate(test.existingErr, test.contextMessage), test.expectedErr) {
4747
t.Errorf("test case %s fails", test.name)
4848
return
4949
}

pkg/ip/ipmasq_iptables_linux.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2015 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 ip
16+
17+
import (
18+
"fmt"
19+
"net"
20+
21+
"github.com/coreos/go-iptables/iptables"
22+
23+
"github.com/containernetworking/cni/pkg/types"
24+
"github.com/containernetworking/plugins/pkg/utils"
25+
)
26+
27+
// setupIPMasqIPTables is the iptables-based implementation of SetupIPMasqForNetwork
28+
func setupIPMasqIPTables(ipn *net.IPNet, network, _, containerID string) error {
29+
// Note: for historical reasons, the iptables implementation ignores ifname.
30+
chain := utils.FormatChainName(network, containerID)
31+
comment := utils.FormatComment(network, containerID)
32+
return SetupIPMasq(ipn, chain, comment)
33+
}
34+
35+
// SetupIPMasq installs iptables rules to masquerade traffic
36+
// coming from ip of ipn and going outside of ipn.
37+
// Deprecated: This function only supports iptables. Use SetupIPMasqForNetwork, which
38+
// supports both iptables and nftables.
39+
func SetupIPMasq(ipn *net.IPNet, chain string, comment string) error {
40+
isV6 := ipn.IP.To4() == nil
41+
42+
var ipt *iptables.IPTables
43+
var err error
44+
var multicastNet string
45+
46+
if isV6 {
47+
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
48+
multicastNet = "ff00::/8"
49+
} else {
50+
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
51+
multicastNet = "224.0.0.0/4"
52+
}
53+
if err != nil {
54+
return fmt.Errorf("failed to locate iptables: %v", err)
55+
}
56+
57+
// Create chain if doesn't exist
58+
exists := false
59+
chains, err := ipt.ListChains("nat")
60+
if err != nil {
61+
return fmt.Errorf("failed to list chains: %v", err)
62+
}
63+
for _, ch := range chains {
64+
if ch == chain {
65+
exists = true
66+
break
67+
}
68+
}
69+
if !exists {
70+
if err = ipt.NewChain("nat", chain); err != nil {
71+
return err
72+
}
73+
}
74+
75+
// Packets to this network should not be touched
76+
if err := ipt.AppendUnique("nat", chain, "-d", ipn.String(), "-j", "ACCEPT", "-m", "comment", "--comment", comment); err != nil {
77+
return err
78+
}
79+
80+
// Don't masquerade multicast - pods should be able to talk to other pods
81+
// on the local network via multicast.
82+
if err := ipt.AppendUnique("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment); err != nil {
83+
return err
84+
}
85+
86+
// Packets from the specific IP of this network will hit the chain
87+
return ipt.AppendUnique("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment)
88+
}
89+
90+
// teardownIPMasqIPTables is the iptables-based implementation of TeardownIPMasqForNetwork
91+
func teardownIPMasqIPTables(ipn *net.IPNet, network, _, containerID string) error {
92+
// Note: for historical reasons, the iptables implementation ignores ifname.
93+
chain := utils.FormatChainName(network, containerID)
94+
comment := utils.FormatComment(network, containerID)
95+
return TeardownIPMasq(ipn, chain, comment)
96+
}
97+
98+
// TeardownIPMasq undoes the effects of SetupIPMasq.
99+
// Deprecated: This function only supports iptables. Use TeardownIPMasqForNetwork, which
100+
// supports both iptables and nftables.
101+
func TeardownIPMasq(ipn *net.IPNet, chain string, comment string) error {
102+
isV6 := ipn.IP.To4() == nil
103+
104+
var ipt *iptables.IPTables
105+
var err error
106+
107+
if isV6 {
108+
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
109+
} else {
110+
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
111+
}
112+
if err != nil {
113+
return fmt.Errorf("failed to locate iptables: %v", err)
114+
}
115+
116+
err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment)
117+
if err != nil && !isNotExist(err) {
118+
return err
119+
}
120+
121+
// for downward compatibility
122+
err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.String(), "-j", chain, "-m", "comment", "--comment", comment)
123+
if err != nil && !isNotExist(err) {
124+
return err
125+
}
126+
127+
err = ipt.ClearChain("nat", chain)
128+
if err != nil && !isNotExist(err) {
129+
return err
130+
}
131+
132+
err = ipt.DeleteChain("nat", chain)
133+
if err != nil && !isNotExist(err) {
134+
return err
135+
}
136+
137+
return nil
138+
}
139+
140+
// gcIPMasqIPTables is the iptables-based implementation of GCIPMasqForNetwork
141+
func gcIPMasqIPTables(_ string, _ []types.GCAttachment) error {
142+
// FIXME: The iptables implementation does not support GC.
143+
//
144+
// (In theory, it _could_ backward-compatibly support it, by adding a no-op rule
145+
// with a comment indicating the network to each chain it creates, so that it
146+
// could later figure out which chains corresponded to which networks; older
147+
// implementations would ignore the extra rule but would still correctly delete
148+
// the chain on teardown (because they ClearChain() before doing DeleteChain()).
149+
150+
return nil
151+
}
152+
153+
// isNotExist returnst true if the error is from iptables indicating
154+
// that the target does not exist.
155+
func isNotExist(err error) bool {
156+
e, ok := err.(*iptables.Error)
157+
if !ok {
158+
return false
159+
}
160+
return e.IsNotExist()
161+
}

0 commit comments

Comments
 (0)