Skip to content

Commit 11f55ea

Browse files
committed
Reduce some duplication in nftables unit tests
1 parent 7693a7e commit 11f55ea

File tree

1 file changed

+55
-110
lines changed

1 file changed

+55
-110
lines changed

pkg/proxy/nftables/proxier_test.go

Lines changed: 55 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,60 @@ func NewFakeProxier(ipFamily v1.IPFamily) (*knftables.Fake, *Proxier) {
137137
return nft, p
138138
}
139139

140+
var baseRules = dedent.Dedent(`
141+
add table ip kube-proxy { comment "rules for kube-proxy" ; }
142+
143+
add chain ip kube-proxy cluster-ips-check
144+
add chain ip kube-proxy filter-prerouting { type filter hook prerouting priority -110 ; }
145+
add chain ip kube-proxy filter-forward { type filter hook forward priority -110 ; }
146+
add chain ip kube-proxy filter-input { type filter hook input priority -110 ; }
147+
add chain ip kube-proxy filter-output { type filter hook output priority -110 ; }
148+
add chain ip kube-proxy filter-output-post-dnat { type filter hook output priority -90 ; }
149+
add chain ip kube-proxy firewall-check
150+
add chain ip kube-proxy mark-for-masquerade
151+
add chain ip kube-proxy masquerading
152+
add chain ip kube-proxy nat-output { type nat hook output priority -100 ; }
153+
add chain ip kube-proxy nat-postrouting { type nat hook postrouting priority 100 ; }
154+
add chain ip kube-proxy nat-prerouting { type nat hook prerouting priority -100 ; }
155+
add chain ip kube-proxy nodeport-endpoints-check
156+
add chain ip kube-proxy reject-chain { comment "helper for @no-endpoint-services / @no-endpoint-nodeports" ; }
157+
add chain ip kube-proxy services
158+
add chain ip kube-proxy service-endpoints-check
159+
160+
add rule ip kube-proxy cluster-ips-check ip daddr @cluster-ips reject comment "Reject traffic to invalid ports of ClusterIPs"
161+
add rule ip kube-proxy cluster-ips-check ip daddr { 172.30.0.0/16 } drop comment "Drop traffic to unallocated ClusterIPs"
162+
add rule ip kube-proxy filter-prerouting ct state new jump firewall-check
163+
add rule ip kube-proxy filter-forward ct state new jump service-endpoints-check
164+
add rule ip kube-proxy filter-forward ct state new jump cluster-ips-check
165+
add rule ip kube-proxy filter-input ct state new jump nodeport-endpoints-check
166+
add rule ip kube-proxy filter-input ct state new jump service-endpoints-check
167+
add rule ip kube-proxy filter-output ct state new jump service-endpoints-check
168+
add rule ip kube-proxy filter-output ct state new jump firewall-check
169+
add rule ip kube-proxy filter-output-post-dnat ct state new jump cluster-ips-check
170+
add rule ip kube-proxy firewall-check ip daddr . meta l4proto . th dport vmap @firewall-ips
171+
add rule ip kube-proxy mark-for-masquerade mark set mark or 0x4000
172+
add rule ip kube-proxy masquerading mark and 0x4000 == 0 return
173+
add rule ip kube-proxy masquerading mark set mark xor 0x4000
174+
add rule ip kube-proxy masquerading masquerade fully-random
175+
add rule ip kube-proxy nat-output jump services
176+
add rule ip kube-proxy nat-postrouting jump masquerading
177+
add rule ip kube-proxy nat-prerouting jump services
178+
add rule ip kube-proxy nodeport-endpoints-check ip daddr @nodeport-ips meta l4proto . th dport vmap @no-endpoint-nodeports
179+
add rule ip kube-proxy reject-chain reject
180+
add rule ip kube-proxy services ip daddr . meta l4proto . th dport vmap @service-ips
181+
add rule ip kube-proxy services ip daddr @nodeport-ips meta l4proto . th dport vmap @service-nodeports
182+
add set ip kube-proxy cluster-ips { type ipv4_addr ; comment "Active ClusterIPs" ; }
183+
add set ip kube-proxy nodeport-ips { type ipv4_addr ; comment "IPs that accept NodePort traffic" ; }
184+
add element ip kube-proxy nodeport-ips { 192.168.0.2 }
185+
add rule ip kube-proxy service-endpoints-check ip daddr . meta l4proto . th dport vmap @no-endpoint-services
186+
187+
add map ip kube-proxy firewall-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "destinations that are subject to LoadBalancerSourceRanges" ; }
188+
add map ip kube-proxy no-endpoint-nodeports { type inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to service nodeports with no endpoints" ; }
189+
add map ip kube-proxy no-endpoint-services { type ipv4_addr . inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to services with no endpoints" ; }
190+
add map ip kube-proxy service-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "ClusterIP, ExternalIP and LoadBalancer IP traffic" ; }
191+
add map ip kube-proxy service-nodeports { type inet_proto . inet_service : verdict ; comment "NodePort traffic" ; }
192+
`)
193+
140194
// TestOverallNFTablesRules creates a variety of services and verifies that the generated
141195
// rules are exactly as expected.
142196
func TestOverallNFTablesRules(t *testing.T) {
@@ -301,62 +355,7 @@ func TestOverallNFTablesRules(t *testing.T) {
301355

302356
fp.syncProxyRules()
303357

304-
expected := dedent.Dedent(`
305-
add table ip kube-proxy { comment "rules for kube-proxy" ; }
306-
307-
add chain ip kube-proxy mark-for-masquerade
308-
add rule ip kube-proxy mark-for-masquerade mark set mark or 0x4000
309-
add chain ip kube-proxy masquerading
310-
add rule ip kube-proxy masquerading mark and 0x4000 == 0 return
311-
add rule ip kube-proxy masquerading mark set mark xor 0x4000
312-
add rule ip kube-proxy masquerading masquerade fully-random
313-
add chain ip kube-proxy services
314-
add chain ip kube-proxy service-endpoints-check
315-
add rule ip kube-proxy service-endpoints-check ip daddr . meta l4proto . th dport vmap @no-endpoint-services
316-
add chain ip kube-proxy filter-prerouting { type filter hook prerouting priority -110 ; }
317-
add rule ip kube-proxy filter-prerouting ct state new jump firewall-check
318-
add chain ip kube-proxy filter-forward { type filter hook forward priority -110 ; }
319-
add rule ip kube-proxy filter-forward ct state new jump service-endpoints-check
320-
add rule ip kube-proxy filter-forward ct state new jump cluster-ips-check
321-
add chain ip kube-proxy filter-input { type filter hook input priority -110 ; }
322-
add rule ip kube-proxy filter-input ct state new jump nodeport-endpoints-check
323-
add rule ip kube-proxy filter-input ct state new jump service-endpoints-check
324-
add chain ip kube-proxy filter-output { type filter hook output priority -110 ; }
325-
add rule ip kube-proxy filter-output ct state new jump service-endpoints-check
326-
add rule ip kube-proxy filter-output ct state new jump firewall-check
327-
add chain ip kube-proxy filter-output-post-dnat { type filter hook output priority -90 ; }
328-
add rule ip kube-proxy filter-output-post-dnat ct state new jump cluster-ips-check
329-
add chain ip kube-proxy nat-output { type nat hook output priority -100 ; }
330-
add rule ip kube-proxy nat-output jump services
331-
add chain ip kube-proxy nat-postrouting { type nat hook postrouting priority 100 ; }
332-
add rule ip kube-proxy nat-postrouting jump masquerading
333-
add chain ip kube-proxy nat-prerouting { type nat hook prerouting priority -100 ; }
334-
add rule ip kube-proxy nat-prerouting jump services
335-
add chain ip kube-proxy nodeport-endpoints-check
336-
add rule ip kube-proxy nodeport-endpoints-check ip daddr @nodeport-ips meta l4proto . th dport vmap @no-endpoint-nodeports
337-
338-
add set ip kube-proxy cluster-ips { type ipv4_addr ; comment "Active ClusterIPs" ; }
339-
add chain ip kube-proxy cluster-ips-check
340-
add rule ip kube-proxy cluster-ips-check ip daddr @cluster-ips reject comment "Reject traffic to invalid ports of ClusterIPs"
341-
add rule ip kube-proxy cluster-ips-check ip daddr { 172.30.0.0/16 } drop comment "Drop traffic to unallocated ClusterIPs"
342-
343-
add set ip kube-proxy nodeport-ips { type ipv4_addr ; comment "IPs that accept NodePort traffic" ; }
344-
add map ip kube-proxy firewall-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "destinations that are subject to LoadBalancerSourceRanges" ; }
345-
add chain ip kube-proxy firewall-check
346-
add rule ip kube-proxy firewall-check ip daddr . meta l4proto . th dport vmap @firewall-ips
347-
348-
add chain ip kube-proxy reject-chain { comment "helper for @no-endpoint-services / @no-endpoint-nodeports" ; }
349-
add rule ip kube-proxy reject-chain reject
350-
351-
add map ip kube-proxy no-endpoint-services { type ipv4_addr . inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to services with no endpoints" ; }
352-
add map ip kube-proxy no-endpoint-nodeports { type inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to service nodeports with no endpoints" ; }
353-
354-
add map ip kube-proxy service-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "ClusterIP, ExternalIP and LoadBalancer IP traffic" ; }
355-
add map ip kube-proxy service-nodeports { type inet_proto . inet_service : verdict ; comment "NodePort traffic" ; }
356-
add rule ip kube-proxy services ip daddr . meta l4proto . th dport vmap @service-ips
357-
add rule ip kube-proxy services ip daddr @nodeport-ips meta l4proto . th dport vmap @service-nodeports
358-
add element ip kube-proxy nodeport-ips { 192.168.0.2 }
359-
358+
expected := baseRules + dedent.Dedent(`
360359
# svc1
361360
add chain ip kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80
362361
add rule ip kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 ip daddr 172.30.0.41 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade
@@ -3942,60 +3941,6 @@ func TestInternalExternalMasquerade(t *testing.T) {
39423941
func TestSyncProxyRulesRepeated(t *testing.T) {
39433942
nft, fp := NewFakeProxier(v1.IPv4Protocol)
39443943

3945-
baseRules := dedent.Dedent(`
3946-
add table ip kube-proxy { comment "rules for kube-proxy" ; }
3947-
3948-
add chain ip kube-proxy cluster-ips-check
3949-
add chain ip kube-proxy filter-prerouting { type filter hook prerouting priority -110 ; }
3950-
add chain ip kube-proxy filter-forward { type filter hook forward priority -110 ; }
3951-
add chain ip kube-proxy filter-input { type filter hook input priority -110 ; }
3952-
add chain ip kube-proxy filter-output { type filter hook output priority -110 ; }
3953-
add chain ip kube-proxy filter-output-post-dnat { type filter hook output priority -90 ; }
3954-
add chain ip kube-proxy firewall-check
3955-
add chain ip kube-proxy mark-for-masquerade
3956-
add chain ip kube-proxy masquerading
3957-
add chain ip kube-proxy nat-output { type nat hook output priority -100 ; }
3958-
add chain ip kube-proxy nat-postrouting { type nat hook postrouting priority 100 ; }
3959-
add chain ip kube-proxy nat-prerouting { type nat hook prerouting priority -100 ; }
3960-
add chain ip kube-proxy nodeport-endpoints-check
3961-
add chain ip kube-proxy reject-chain { comment "helper for @no-endpoint-services / @no-endpoint-nodeports" ; }
3962-
add chain ip kube-proxy services
3963-
add chain ip kube-proxy service-endpoints-check
3964-
3965-
add rule ip kube-proxy cluster-ips-check ip daddr @cluster-ips reject comment "Reject traffic to invalid ports of ClusterIPs"
3966-
add rule ip kube-proxy cluster-ips-check ip daddr { 172.30.0.0/16 } drop comment "Drop traffic to unallocated ClusterIPs"
3967-
add rule ip kube-proxy filter-prerouting ct state new jump firewall-check
3968-
add rule ip kube-proxy filter-forward ct state new jump service-endpoints-check
3969-
add rule ip kube-proxy filter-forward ct state new jump cluster-ips-check
3970-
add rule ip kube-proxy filter-input ct state new jump nodeport-endpoints-check
3971-
add rule ip kube-proxy filter-input ct state new jump service-endpoints-check
3972-
add rule ip kube-proxy filter-output ct state new jump service-endpoints-check
3973-
add rule ip kube-proxy filter-output ct state new jump firewall-check
3974-
add rule ip kube-proxy filter-output-post-dnat ct state new jump cluster-ips-check
3975-
add rule ip kube-proxy firewall-check ip daddr . meta l4proto . th dport vmap @firewall-ips
3976-
add rule ip kube-proxy mark-for-masquerade mark set mark or 0x4000
3977-
add rule ip kube-proxy masquerading mark and 0x4000 == 0 return
3978-
add rule ip kube-proxy masquerading mark set mark xor 0x4000
3979-
add rule ip kube-proxy masquerading masquerade fully-random
3980-
add rule ip kube-proxy nat-output jump services
3981-
add rule ip kube-proxy nat-postrouting jump masquerading
3982-
add rule ip kube-proxy nat-prerouting jump services
3983-
add rule ip kube-proxy nodeport-endpoints-check ip daddr @nodeport-ips meta l4proto . th dport vmap @no-endpoint-nodeports
3984-
add rule ip kube-proxy reject-chain reject
3985-
add rule ip kube-proxy services ip daddr . meta l4proto . th dport vmap @service-ips
3986-
add rule ip kube-proxy services ip daddr @nodeport-ips meta l4proto . th dport vmap @service-nodeports
3987-
add set ip kube-proxy cluster-ips { type ipv4_addr ; comment "Active ClusterIPs" ; }
3988-
add set ip kube-proxy nodeport-ips { type ipv4_addr ; comment "IPs that accept NodePort traffic" ; }
3989-
add element ip kube-proxy nodeport-ips { 192.168.0.2 }
3990-
add rule ip kube-proxy service-endpoints-check ip daddr . meta l4proto . th dport vmap @no-endpoint-services
3991-
3992-
add map ip kube-proxy firewall-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "destinations that are subject to LoadBalancerSourceRanges" ; }
3993-
add map ip kube-proxy no-endpoint-nodeports { type inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to service nodeports with no endpoints" ; }
3994-
add map ip kube-proxy no-endpoint-services { type ipv4_addr . inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to services with no endpoints" ; }
3995-
add map ip kube-proxy service-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "ClusterIP, ExternalIP and LoadBalancer IP traffic" ; }
3996-
add map ip kube-proxy service-nodeports { type inet_proto . inet_service : verdict ; comment "NodePort traffic" ; }
3997-
`)
3998-
39993944
// Helper function to make it look like time has passed (from the point of view of
40003945
// the stale-chain-deletion code).
40013946
ageStaleChains := func() {

0 commit comments

Comments
 (0)