1515package ip
1616
1717import (
18+ "net"
1819 "strings"
1920 "testing"
2021
@@ -31,43 +32,55 @@ func Test_setupIPMasqNFTables(t *testing.T) {
3132 network string
3233 ifname string
3334 containerID string
34- addr string
35+ addrs [] string
3536 }{
3637 {
3738 network : "unit-test" ,
3839 ifname : "eth0" ,
3940 containerID : "one" ,
40- addr : "192.168.1.1/24" ,
41+ addrs : [] string { "192.168.1.1/24" } ,
4142 },
4243 {
4344 network : "unit-test" ,
4445 ifname : "eth0" ,
4546 containerID : "two" ,
46- addr : "192.168.1.2/24" ,
47+ addrs : [] string { "192.168.1.2/24" , "2001:db8::2/64" } ,
4748 },
4849 {
4950 network : "unit-test" ,
5051 ifname : "eth0" ,
5152 containerID : "three" ,
52- addr : "192.168.99.5/24" ,
53+ addrs : [] string { "192.168.99.5/24" } ,
5354 },
5455 {
5556 network : "alternate" ,
5657 ifname : "net1" ,
5758 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+ },
5967 },
6068 }
6169
6270 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 )
6678 }
67- err = setupIPMasqNFTablesWithInterface (nft , addr . IPNet , c .network , c .ifname , c .containerID )
79+ err : = setupIPMasqNFTablesWithInterface (nft , ipns , c .network , c .ifname , c .containerID )
6880 if err != nil {
6981 t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
7082 }
83+
7184 }
7285
7386 expected := strings .TrimSpace (`
@@ -76,8 +89,14 @@ add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic
7689add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
7790add 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"
7891add 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"
7993add 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"
8094add 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"
81100add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
82101add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
83102add rule inet cni_plugins_masquerade postrouting goto masq_checks
@@ -88,22 +107,18 @@ add rule inet cni_plugins_masquerade postrouting goto masq_checks
88107 }
89108
90109 // 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 ])
92112 if err != nil {
93113 t .Fatalf ("failed to parse test addr: %v" , err )
94114 }
95- err = setupIPMasqNFTablesWithInterface (nft , addr .IPNet , "unit-test" , "eth0" , "four" )
115+ err = setupIPMasqNFTablesWithInterface (nft , [] * net. IPNet { addr .IPNet } , "unit-test" , "eth0" , "four" )
96116 if err != nil {
97117 t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
98118 }
99119
100120 // 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 )
107122 if err != nil {
108123 t .Fatalf ("error from teardownIPMasqNFTables: %v" , err )
109124 }
@@ -114,8 +129,14 @@ add table inet cni_plugins_masquerade { comment "Masquerading for plugins from g
114129add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
115130add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
116131add 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"
117133add 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"
118134add 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"
119140add 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"
120141add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
121142add 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
150171add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
151172add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
152173add 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"
153175add 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"
154176add 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"
155182add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
156183add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
157184add rule inet cni_plugins_masquerade postrouting goto masq_checks
0 commit comments