Skip to content

Commit c34b20f

Browse files
committed
proxy/conntrack: use proxier ip family for conntrack cleanup
Signed-off-by: Daman Arora <[email protected]>
1 parent a6b4aa7 commit c34b20f

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

pkg/proxy/conntrack/cleanup.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,25 @@ import (
3232
)
3333

3434
// CleanStaleEntries takes care of flushing stale conntrack entries for services and endpoints.
35-
func CleanStaleEntries(ct Interface, svcPortMap proxy.ServicePortMap,
35+
func CleanStaleEntries(ct Interface, ipFamily v1.IPFamily, svcPortMap proxy.ServicePortMap,
3636
serviceUpdateResult proxy.UpdateServiceMapResult, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
37-
deleteStaleServiceConntrackEntries(ct, svcPortMap, serviceUpdateResult, endpointsUpdateResult)
38-
deleteStaleEndpointConntrackEntries(ct, svcPortMap, endpointsUpdateResult)
37+
deleteStaleServiceConntrackEntries(ct, ipFamily, svcPortMap, serviceUpdateResult, endpointsUpdateResult)
38+
deleteStaleEndpointConntrackEntries(ct, ipFamily, svcPortMap, endpointsUpdateResult)
3939
}
4040

4141
// deleteStaleServiceConntrackEntries takes care of flushing stale conntrack entries related
4242
// to UDP Service IPs. When a service has no endpoints and we drop traffic to it, conntrack
4343
// may create "black hole" entries for that IP+port. When the service gets endpoints we
4444
// need to delete those entries so further traffic doesn't get dropped.
45-
func deleteStaleServiceConntrackEntries(ct Interface, svcPortMap proxy.ServicePortMap, serviceUpdateResult proxy.UpdateServiceMapResult, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
45+
func deleteStaleServiceConntrackEntries(ct Interface, ipFamily v1.IPFamily, svcPortMap proxy.ServicePortMap, serviceUpdateResult proxy.UpdateServiceMapResult, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
4646
var filters []netlink.CustomConntrackFilter
4747
conntrackCleanupServiceIPs := serviceUpdateResult.DeletedUDPClusterIPs
4848
conntrackCleanupServiceNodePorts := sets.New[int]()
49-
isIPv6 := false
5049

5150
// merge newly active services gathered from endpointsUpdateResult
5251
// a UDP service that changes from 0 to non-0 endpoints is newly active.
5352
for _, svcPortName := range endpointsUpdateResult.NewlyActiveUDPServices {
5453
if svcInfo, ok := svcPortMap[svcPortName]; ok {
55-
isIPv6 = netutils.IsIPv6(svcInfo.ClusterIP())
5654
klog.V(4).InfoS("Newly-active UDP service may have stale conntrack entries", "servicePortName", svcPortName)
5755
conntrackCleanupServiceIPs.Insert(svcInfo.ClusterIP().String())
5856
for _, extIP := range svcInfo.ExternalIPs() {
@@ -77,20 +75,18 @@ func deleteStaleServiceConntrackEntries(ct Interface, svcPortMap proxy.ServicePo
7775
filters = append(filters, filterForPort(nodePort, v1.ProtocolUDP))
7876
}
7977

80-
if err := ct.ClearEntries(getUnixIPFamily(isIPv6), filters...); err != nil {
78+
if err := ct.ClearEntries(ipFamilyMap[ipFamily], filters...); err != nil {
8179
klog.ErrorS(err, "Failed to delete stale service connections")
8280
}
8381
}
8482

8583
// deleteStaleEndpointConntrackEntries takes care of flushing stale conntrack entries related
8684
// to UDP endpoints. After a UDP endpoint is removed we must flush any conntrack entries
8785
// for it so that if the same client keeps sending, the packets will get routed to a new endpoint.
88-
func deleteStaleEndpointConntrackEntries(ct Interface, svcPortMap proxy.ServicePortMap, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
86+
func deleteStaleEndpointConntrackEntries(ct Interface, ipFamily v1.IPFamily, svcPortMap proxy.ServicePortMap, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
8987
var filters []netlink.CustomConntrackFilter
90-
isIPv6 := false
9188
for _, epSvcPair := range endpointsUpdateResult.DeletedUDPEndpoints {
9289
if svcInfo, ok := svcPortMap[epSvcPair.ServicePortName]; ok {
93-
isIPv6 = netutils.IsIPv6(svcInfo.ClusterIP())
9490
endpointIP := proxyutil.IPPart(epSvcPair.Endpoint)
9591
nodePort := svcInfo.NodePort()
9692
if nodePort != 0 {
@@ -107,17 +103,15 @@ func deleteStaleEndpointConntrackEntries(ct Interface, svcPortMap proxy.ServiceP
107103
}
108104
}
109105

110-
if err := ct.ClearEntries(getUnixIPFamily(isIPv6), filters...); err != nil {
106+
if err := ct.ClearEntries(ipFamilyMap[ipFamily], filters...); err != nil {
111107
klog.ErrorS(err, "Failed to delete stale endpoint connections")
112108
}
113109
}
114110

115-
// getUnixIPFamily returns the unix IPFamily constant.
116-
func getUnixIPFamily(isIPv6 bool) uint8 {
117-
if isIPv6 {
118-
return unix.AF_INET6
119-
}
120-
return unix.AF_INET
111+
// ipFamilyMap maps v1.IPFamily to the corresponding unix constant.
112+
var ipFamilyMap = map[v1.IPFamily]uint8{
113+
v1.IPv4Protocol: unix.AF_INET,
114+
v1.IPv6Protocol: unix.AF_INET6,
121115
}
122116

123117
// protocolMap maps v1.Protocol to the Assigned Internet Protocol Number.

pkg/proxy/conntrack/cleanup_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
)
3737

3838
const (
39+
testIPFamily = v1.IPv4Protocol
3940
testClusterIP = "172.30.1.1"
4041
testExternalIP = "192.168.99.100"
4142
testLoadBalancerIP = "1.2.3.4"
@@ -249,7 +250,7 @@ func TestCleanStaleEntries(t *testing.T) {
249250
for _, tc := range testCases {
250251
t.Run(tc.description, func(t *testing.T) {
251252
fake := NewFake()
252-
CleanStaleEntries(fake, svcPortMap, tc.serviceUpdates, tc.endpointsUpdates)
253+
CleanStaleEntries(fake, testIPFamily, svcPortMap, tc.serviceUpdates, tc.endpointsUpdates)
253254
if !fake.ClearedIPs.Equal(tc.result.ClearedIPs) {
254255
t.Errorf("Expected ClearedIPs=%v, got %v", tc.result.ClearedIPs, fake.ClearedIPs)
255256
}

pkg/proxy/iptables/proxier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ func (proxier *Proxier) syncProxyRules() {
15951595
}
15961596

15971597
// Finish housekeeping, clear stale conntrack entries for UDP Services
1598-
conntrack.CleanStaleEntries(proxier.conntrack, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
1598+
conntrack.CleanStaleEntries(proxier.conntrack, proxier.ipFamily, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
15991599
}
16001600

16011601
func (proxier *Proxier) writeServiceToEndpointRules(natRules proxyutil.LineBuffer, svcPortNameString string, svcInfo proxy.ServicePort, svcChain utiliptables.Chain, endpoints []proxy.Endpoint, args []string) {

pkg/proxy/ipvs/proxier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ func (proxier *Proxier) syncProxyRules() {
14981498
metrics.SyncProxyRulesNoLocalEndpointsTotal.WithLabelValues("external").Set(float64(proxier.serviceNoLocalEndpointsExternal.Len()))
14991499

15001500
// Finish housekeeping, clear stale conntrack entries for UDP Services
1501-
conntrack.CleanStaleEntries(proxier.conntrack, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
1501+
conntrack.CleanStaleEntries(proxier.conntrack, proxier.ipFamily, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
15021502
}
15031503

15041504
// writeIptablesRules write all iptables rules to proxier.natRules or proxier.FilterRules that ipvs proxier needed

pkg/proxy/nftables/proxier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ func (proxier *Proxier) syncProxyRules() {
18391839
}
18401840

18411841
// Finish housekeeping, clear stale conntrack entries for UDP Services
1842-
conntrack.CleanStaleEntries(proxier.conntrack, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
1842+
conntrack.CleanStaleEntries(proxier.conntrack, proxier.ipFamily, proxier.svcPortMap, serviceUpdateResult, endpointUpdateResult)
18431843
}
18441844

18451845
func (proxier *Proxier) writeServiceToEndpointRules(tx *knftables.Transaction, svcInfo *servicePortInfo, svcChain string, endpoints []proxy.Endpoint) {

0 commit comments

Comments
 (0)