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

     1  /*
     2  Copyright 2016 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  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/klog/v2"
    26  	"k8s.io/utils/exec"
    27  	utilnet "k8s.io/utils/net"
    28  )
    29  
    30  // Utilities for dealing with conntrack
    31  
    32  // NoConnectionToDelete is the error string returned by conntrack when no matching connections are found
    33  const NoConnectionToDelete = "0 flow entries have been deleted"
    34  
    35  func protoStr(proto v1.Protocol) string {
    36  	return strings.ToLower(string(proto))
    37  }
    38  
    39  func parametersWithFamily(isIPv6 bool, parameters ...string) []string {
    40  	if isIPv6 {
    41  		parameters = append(parameters, "-f", "ipv6")
    42  	}
    43  	return parameters
    44  }
    45  
    46  // ClearEntriesForIP uses the conntrack tool to delete the conntrack entries
    47  // for the UDP connections specified by the given service IP
    48  func ClearEntriesForIP(execer exec.Interface, ip string, protocol v1.Protocol) error {
    49  	parameters := parametersWithFamily(utilnet.IsIPv6String(ip), "-D", "--orig-dst", ip, "-p", protoStr(protocol))
    50  	err := Exec(execer, parameters...)
    51  	if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
    52  		// TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
    53  		// These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
    54  		// is expensive to baby-sit all udp connections to kubernetes services.
    55  		return fmt.Errorf("error deleting connection tracking state for UDP service IP: %s, error: %v", ip, err)
    56  	}
    57  	return nil
    58  }
    59  
    60  // Exec executes the conntrack tool using the given parameters
    61  func Exec(execer exec.Interface, parameters ...string) error {
    62  	conntrackPath, err := execer.LookPath("conntrack")
    63  	if err != nil {
    64  		return fmt.Errorf("error looking for path of conntrack: %v", err)
    65  	}
    66  	klog.V(4).InfoS("Clearing conntrack entries", "parameters", parameters)
    67  	output, err := execer.Command(conntrackPath, parameters...).CombinedOutput()
    68  	if err != nil {
    69  		return fmt.Errorf("conntrack command returned: %q, error message: %s", string(output), err)
    70  	}
    71  	klog.V(4).InfoS("Conntrack entries deleted", "output", string(output))
    72  	return nil
    73  }
    74  
    75  // ClearEntriesForPort uses the conntrack tool to delete the conntrack entries
    76  // for connections specified by the port.
    77  // When a packet arrives, it will not go through NAT table again, because it is not "the first" packet.
    78  // The solution is clearing the conntrack. Known issues:
    79  // https://github.com/docker/docker/issues/8795
    80  // https://github.com/kubernetes/kubernetes/issues/31983
    81  func ClearEntriesForPort(execer exec.Interface, port int, isIPv6 bool, protocol v1.Protocol) error {
    82  	if port <= 0 {
    83  		return fmt.Errorf("wrong port number. The port number must be greater than zero")
    84  	}
    85  	parameters := parametersWithFamily(isIPv6, "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port))
    86  	err := Exec(execer, parameters...)
    87  	if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
    88  		return fmt.Errorf("error deleting conntrack entries for UDP port: %d, error: %v", port, err)
    89  	}
    90  	return nil
    91  }
    92  
    93  // ClearEntriesForNAT uses the conntrack tool to delete the conntrack entries
    94  // for connections specified by the {origin, dest} IP pair.
    95  func ClearEntriesForNAT(execer exec.Interface, origin, dest string, protocol v1.Protocol) error {
    96  	parameters := parametersWithFamily(utilnet.IsIPv6String(origin), "-D", "--orig-dst", origin, "--dst-nat", dest,
    97  		"-p", protoStr(protocol))
    98  	err := Exec(execer, parameters...)
    99  	if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
   100  		// TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
   101  		// These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
   102  		// is expensive to baby sit all udp connections to kubernetes services.
   103  		return fmt.Errorf("error deleting conntrack entries for UDP peer {%s, %s}, error: %v", origin, dest, err)
   104  	}
   105  	return nil
   106  }
   107  
   108  // ClearEntriesForPortNAT uses the conntrack tool to delete the conntrack entries
   109  // for connections specified by the {dest IP, port} pair.
   110  // Known issue:
   111  // https://github.com/kubernetes/kubernetes/issues/59368
   112  func ClearEntriesForPortNAT(execer exec.Interface, dest string, port int, protocol v1.Protocol) error {
   113  	if port <= 0 {
   114  		return fmt.Errorf("wrong port number. The port number must be greater than zero")
   115  	}
   116  	parameters := parametersWithFamily(utilnet.IsIPv6String(dest), "-D", "-p", protoStr(protocol), "--dport", strconv.Itoa(port), "--dst-nat", dest)
   117  	err := Exec(execer, parameters...)
   118  	if err != nil && !strings.Contains(err.Error(), NoConnectionToDelete) {
   119  		return fmt.Errorf("error deleting conntrack entries for UDP port: %d, error: %v", port, err)
   120  	}
   121  	return nil
   122  }