@@ -32,27 +32,25 @@ import (
32
32
)
33
33
34
34
// 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 ,
36
36
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 )
39
39
}
40
40
41
41
// deleteStaleServiceConntrackEntries takes care of flushing stale conntrack entries related
42
42
// to UDP Service IPs. When a service has no endpoints and we drop traffic to it, conntrack
43
43
// may create "black hole" entries for that IP+port. When the service gets endpoints we
44
44
// 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 ) {
46
46
var filters []netlink.CustomConntrackFilter
47
47
conntrackCleanupServiceIPs := serviceUpdateResult .DeletedUDPClusterIPs
48
48
conntrackCleanupServiceNodePorts := sets .New [int ]()
49
- isIPv6 := false
50
49
51
50
// merge newly active services gathered from endpointsUpdateResult
52
51
// a UDP service that changes from 0 to non-0 endpoints is newly active.
53
52
for _ , svcPortName := range endpointsUpdateResult .NewlyActiveUDPServices {
54
53
if svcInfo , ok := svcPortMap [svcPortName ]; ok {
55
- isIPv6 = netutils .IsIPv6 (svcInfo .ClusterIP ())
56
54
klog .V (4 ).InfoS ("Newly-active UDP service may have stale conntrack entries" , "servicePortName" , svcPortName )
57
55
conntrackCleanupServiceIPs .Insert (svcInfo .ClusterIP ().String ())
58
56
for _ , extIP := range svcInfo .ExternalIPs () {
@@ -77,20 +75,18 @@ func deleteStaleServiceConntrackEntries(ct Interface, svcPortMap proxy.ServicePo
77
75
filters = append (filters , filterForPort (nodePort , v1 .ProtocolUDP ))
78
76
}
79
77
80
- if err := ct .ClearEntries (getUnixIPFamily ( isIPv6 ) , filters ... ); err != nil {
78
+ if err := ct .ClearEntries (ipFamilyMap [ ipFamily ] , filters ... ); err != nil {
81
79
klog .ErrorS (err , "Failed to delete stale service connections" )
82
80
}
83
81
}
84
82
85
83
// deleteStaleEndpointConntrackEntries takes care of flushing stale conntrack entries related
86
84
// to UDP endpoints. After a UDP endpoint is removed we must flush any conntrack entries
87
85
// 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 ) {
89
87
var filters []netlink.CustomConntrackFilter
90
- isIPv6 := false
91
88
for _ , epSvcPair := range endpointsUpdateResult .DeletedUDPEndpoints {
92
89
if svcInfo , ok := svcPortMap [epSvcPair .ServicePortName ]; ok {
93
- isIPv6 = netutils .IsIPv6 (svcInfo .ClusterIP ())
94
90
endpointIP := proxyutil .IPPart (epSvcPair .Endpoint )
95
91
nodePort := svcInfo .NodePort ()
96
92
if nodePort != 0 {
@@ -107,17 +103,15 @@ func deleteStaleEndpointConntrackEntries(ct Interface, svcPortMap proxy.ServiceP
107
103
}
108
104
}
109
105
110
- if err := ct .ClearEntries (getUnixIPFamily ( isIPv6 ) , filters ... ); err != nil {
106
+ if err := ct .ClearEntries (ipFamilyMap [ ipFamily ] , filters ... ); err != nil {
111
107
klog .ErrorS (err , "Failed to delete stale endpoint connections" )
112
108
}
113
109
}
114
110
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 ,
121
115
}
122
116
123
117
// protocolMap maps v1.Protocol to the Assigned Internet Protocol Number.
0 commit comments