15
15
package ip
16
16
17
17
import (
18
+ "net"
18
19
"strings"
19
20
"testing"
20
21
@@ -31,43 +32,55 @@ func Test_setupIPMasqNFTables(t *testing.T) {
31
32
network string
32
33
ifname string
33
34
containerID string
34
- addr string
35
+ addrs [] string
35
36
}{
36
37
{
37
38
network : "unit-test" ,
38
39
ifname : "eth0" ,
39
40
containerID : "one" ,
40
- addr : "192.168.1.1/24" ,
41
+ addrs : [] string { "192.168.1.1/24" } ,
41
42
},
42
43
{
43
44
network : "unit-test" ,
44
45
ifname : "eth0" ,
45
46
containerID : "two" ,
46
- addr : "192.168.1.2/24" ,
47
+ addrs : [] string { "192.168.1.2/24" , "2001:db8::2/64" } ,
47
48
},
48
49
{
49
50
network : "unit-test" ,
50
51
ifname : "eth0" ,
51
52
containerID : "three" ,
52
- addr : "192.168.99.5/24" ,
53
+ addrs : [] string { "192.168.99.5/24" } ,
53
54
},
54
55
{
55
56
network : "alternate" ,
56
57
ifname : "net1" ,
57
58
containerID : "three" ,
58
- addr : "10.0.0.5/24" ,
59
+ addrs : []string {
60
+ "10.0.0.5/24" ,
61
+ "10.0.0.6/24" ,
62
+ "10.0.1.7/24" ,
63
+ "2001:db8::5/64" ,
64
+ "2001:db8::6/64" ,
65
+ "2001:db8:1::7/64" ,
66
+ },
59
67
},
60
68
}
61
69
62
70
for _ , c := range containers {
63
- addr , err := netlink .ParseAddr (c .addr )
64
- if err != nil {
65
- t .Fatalf ("failed to parse test addr: %v" , err )
71
+ ipns := []* net.IPNet {}
72
+ for _ , addr := range c .addrs {
73
+ nladdr , err := netlink .ParseAddr (addr )
74
+ if err != nil {
75
+ t .Fatalf ("failed to parse test addr: %v" , err )
76
+ }
77
+ ipns = append (ipns , nladdr .IPNet )
66
78
}
67
- err = setupIPMasqNFTablesWithInterface (nft , addr . IPNet , c .network , c .ifname , c .containerID )
79
+ err : = setupIPMasqNFTablesWithInterface (nft , ipns , c .network , c .ifname , c .containerID )
68
80
if err != nil {
69
81
t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
70
82
}
83
+
71
84
}
72
85
73
86
expected := strings .TrimSpace (`
@@ -76,8 +89,14 @@ add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic
76
89
add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
77
90
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-287fc69eff0574a2, net: unit-test, if: eth0, id: one"
78
91
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
92
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
79
93
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
80
94
add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
95
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
96
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
97
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
98
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
99
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
81
100
add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
82
101
add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
83
102
add rule inet cni_plugins_masquerade postrouting goto masq_checks
@@ -88,22 +107,18 @@ add rule inet cni_plugins_masquerade postrouting goto masq_checks
88
107
}
89
108
90
109
// Add a new container reusing "one"'s address, before deleting "one"
91
- addr , err := netlink .ParseAddr (containers [0 ].addr )
110
+ c := containers [0 ]
111
+ addr , err := netlink .ParseAddr (c .addrs [0 ])
92
112
if err != nil {
93
113
t .Fatalf ("failed to parse test addr: %v" , err )
94
114
}
95
- err = setupIPMasqNFTablesWithInterface (nft , addr .IPNet , "unit-test" , "eth0" , "four" )
115
+ err = setupIPMasqNFTablesWithInterface (nft , [] * net. IPNet { addr .IPNet } , "unit-test" , "eth0" , "four" )
96
116
if err != nil {
97
117
t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
98
118
}
99
119
100
120
// Remove "one"
101
- c := containers [0 ]
102
- addr , err = netlink .ParseAddr (c .addr )
103
- if err != nil {
104
- t .Fatalf ("failed to parse test addr: %v" , err )
105
- }
106
- err = teardownIPMasqNFTablesWithInterface (nft , addr .IPNet , c .network , c .ifname , c .containerID )
121
+ err = teardownIPMasqNFTablesWithInterface (nft , []* net.IPNet {addr .IPNet }, c .network , c .ifname , c .containerID )
107
122
if err != nil {
108
123
t .Fatalf ("error from teardownIPMasqNFTables: %v" , err )
109
124
}
@@ -114,8 +129,14 @@ add table inet cni_plugins_masquerade { comment "Masquerading for plugins from g
114
129
add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
115
130
add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
116
131
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
132
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
117
133
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
118
134
add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
135
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
136
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
137
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
138
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
139
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
119
140
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-e766de567ef6c543, net: unit-test, if: eth0, id: four"
120
141
add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
121
142
add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
@@ -150,8 +171,14 @@ add table inet cni_plugins_masquerade { comment "Masquerading for plugins from g
150
171
add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
151
172
add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
152
173
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
174
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
153
175
add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
154
176
add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
177
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.6 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
178
+ add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.1.7 ip daddr != 10.0.1.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
179
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
180
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::6 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
181
+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8:1::7 ip6 daddr != 2001:db8:1::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
155
182
add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
156
183
add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
157
184
add rule inet cni_plugins_masquerade postrouting goto masq_checks
0 commit comments