k8s.io/kubernetes@v1.29.3/pkg/proxy/conntrack/cleanup.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package conntrack
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/util/sets"
    22  	"k8s.io/klog/v2"
    23  	"k8s.io/kubernetes/pkg/proxy"
    24  	proxyutil "k8s.io/kubernetes/pkg/proxy/util"
    25  	utilexec "k8s.io/utils/exec"
    26  )
    27  
    28  // CleanStaleEntries takes care of flushing stale conntrack entries for services and endpoints.
    29  func CleanStaleEntries(isIPv6 bool, exec utilexec.Interface, svcPortMap proxy.ServicePortMap,
    30  	serviceUpdateResult proxy.UpdateServiceMapResult, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
    31  
    32  	deleteStaleServiceConntrackEntries(isIPv6, exec, svcPortMap, serviceUpdateResult, endpointsUpdateResult)
    33  	deleteStaleEndpointConntrackEntries(exec, svcPortMap, endpointsUpdateResult)
    34  }
    35  
    36  // deleteStaleServiceConntrackEntries takes care of flushing stale conntrack entries related
    37  // to UDP Service IPs. When a service has no endpoints and we drop traffic to it, conntrack
    38  // may create "black hole" entries for that IP+port. When the service gets endpoints we
    39  // need to delete those entries so further traffic doesn't get dropped.
    40  func deleteStaleServiceConntrackEntries(isIPv6 bool, exec utilexec.Interface, svcPortMap proxy.ServicePortMap, serviceUpdateResult proxy.UpdateServiceMapResult, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
    41  	conntrackCleanupServiceIPs := serviceUpdateResult.DeletedUDPClusterIPs
    42  	conntrackCleanupServiceNodePorts := sets.New[int]()
    43  
    44  	// merge newly active services gathered from endpointsUpdateResult
    45  	// a UDP service that changes from 0 to non-0 endpoints is newly active.
    46  	for _, svcPortName := range endpointsUpdateResult.NewlyActiveUDPServices {
    47  		if svcInfo, ok := svcPortMap[svcPortName]; ok {
    48  			klog.V(4).InfoS("Newly-active UDP service may have stale conntrack entries", "servicePortName", svcPortName)
    49  			conntrackCleanupServiceIPs.Insert(svcInfo.ClusterIP().String())
    50  			for _, extIP := range svcInfo.ExternalIPStrings() {
    51  				conntrackCleanupServiceIPs.Insert(extIP)
    52  			}
    53  			for _, lbIP := range svcInfo.LoadBalancerVIPStrings() {
    54  				conntrackCleanupServiceIPs.Insert(lbIP)
    55  			}
    56  			nodePort := svcInfo.NodePort()
    57  			if svcInfo.Protocol() == v1.ProtocolUDP && nodePort != 0 {
    58  				conntrackCleanupServiceNodePorts.Insert(nodePort)
    59  			}
    60  		}
    61  	}
    62  
    63  	klog.V(4).InfoS("Deleting conntrack stale entries for services", "IPs", conntrackCleanupServiceIPs.UnsortedList())
    64  	for _, svcIP := range conntrackCleanupServiceIPs.UnsortedList() {
    65  		if err := ClearEntriesForIP(exec, svcIP, v1.ProtocolUDP); err != nil {
    66  			klog.ErrorS(err, "Failed to delete stale service connections", "IP", svcIP)
    67  		}
    68  	}
    69  	klog.V(4).InfoS("Deleting conntrack stale entries for services", "nodePorts", conntrackCleanupServiceNodePorts.UnsortedList())
    70  	for _, nodePort := range conntrackCleanupServiceNodePorts.UnsortedList() {
    71  		err := ClearEntriesForPort(exec, nodePort, isIPv6, v1.ProtocolUDP)
    72  		if err != nil {
    73  			klog.ErrorS(err, "Failed to clear udp conntrack", "nodePort", nodePort)
    74  		}
    75  	}
    76  }
    77  
    78  // deleteStaleEndpointConntrackEntries takes care of flushing stale conntrack entries related
    79  // to UDP endpoints. After a UDP endpoint is removed we must flush any conntrack entries
    80  // for it so that if the same client keeps sending, the packets will get routed to a new endpoint.
    81  func deleteStaleEndpointConntrackEntries(exec utilexec.Interface, svcPortMap proxy.ServicePortMap, endpointsUpdateResult proxy.UpdateEndpointsMapResult) {
    82  	for _, epSvcPair := range endpointsUpdateResult.DeletedUDPEndpoints {
    83  		if svcInfo, ok := svcPortMap[epSvcPair.ServicePortName]; ok {
    84  			endpointIP := proxyutil.IPPart(epSvcPair.Endpoint)
    85  			nodePort := svcInfo.NodePort()
    86  			var err error
    87  			if nodePort != 0 {
    88  				err = ClearEntriesForPortNAT(exec, endpointIP, nodePort, v1.ProtocolUDP)
    89  				if err != nil {
    90  					klog.ErrorS(err, "Failed to delete nodeport-related endpoint connections", "servicePortName", epSvcPair.ServicePortName)
    91  				}
    92  			}
    93  			err = ClearEntriesForNAT(exec, svcInfo.ClusterIP().String(), endpointIP, v1.ProtocolUDP)
    94  			if err != nil {
    95  				klog.ErrorS(err, "Failed to delete endpoint connections", "servicePortName", epSvcPair.ServicePortName)
    96  			}
    97  			for _, extIP := range svcInfo.ExternalIPStrings() {
    98  				err := ClearEntriesForNAT(exec, extIP, endpointIP, v1.ProtocolUDP)
    99  				if err != nil {
   100  					klog.ErrorS(err, "Failed to delete endpoint connections for externalIP", "servicePortName", epSvcPair.ServicePortName, "externalIP", extIP)
   101  				}
   102  			}
   103  			for _, lbIP := range svcInfo.LoadBalancerVIPStrings() {
   104  				err := ClearEntriesForNAT(exec, lbIP, endpointIP, v1.ProtocolUDP)
   105  				if err != nil {
   106  					klog.ErrorS(err, "Failed to delete endpoint connections for LoadBalancerIP", "servicePortName", epSvcPair.ServicePortName, "loadBalancerIP", lbIP)
   107  				}
   108  			}
   109  		}
   110  	}
   111  }