From facf702e6424edd698fa3ff2f7d9131582e9b841 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Fri, 17 May 2024 21:11:39 +0800 Subject: [PATCH] Remove useless util functions --- pkg/proxy/util/endpoints.go | 17 --- pkg/proxy/util/endpoints_test.go | 52 --------- pkg/proxy/util/utils.go | 97 +---------------- pkg/proxy/util/utils_test.go | 175 ------------------------------- 4 files changed, 2 insertions(+), 339 deletions(-) diff --git a/pkg/proxy/util/endpoints.go b/pkg/proxy/util/endpoints.go index d5e08e4c1bc..c504fa23a84 100644 --- a/pkg/proxy/util/endpoints.go +++ b/pkg/proxy/util/endpoints.go @@ -18,7 +18,6 @@ package util import ( "net" - "strconv" "k8s.io/klog/v2" netutils "k8s.io/utils/net" @@ -46,19 +45,3 @@ func IPPart(s string) string { } return ip.String() } - -// PortPart returns just the port part of an endpoint string. -func PortPart(s string) (int, error) { - // Must be IP:port - _, port, err := net.SplitHostPort(s) - if err != nil { - klog.ErrorS(err, "Failed to parse host-port", "input", s) - return -1, err - } - portNumber, err := strconv.Atoi(port) - if err != nil { - klog.ErrorS(err, "Failed to parse port", "input", port) - return -1, err - } - return portNumber, nil -} diff --git a/pkg/proxy/util/endpoints_test.go b/pkg/proxy/util/endpoints_test.go index 16a29d80a88..148784f57ca 100644 --- a/pkg/proxy/util/endpoints_test.go +++ b/pkg/proxy/util/endpoints_test.go @@ -48,55 +48,3 @@ func TestIPPart(t *testing.T) { } } } - -func TestPortPart(t *testing.T) { - tests := []struct { - name string - endpoint string - want int - wantErr bool - }{ - { - "no error parsing from ipv4-ip:port", - "1.2.3.4:1024", - 1024, - false, - }, - { - "no error parsing from ipv6-ip:port", - "[2001:db8::2:2]:9999", - 9999, - false, - }, - { - "error: missing port", - "1.2.3.4", - -1, - true, - }, - { - "error: invalid port '1-2'", - "1.2.3.4:1-2", - -1, - true, - }, - { - "error: invalid port 'port'", - "100.200.3.4:port", - -1, - true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := PortPart(tt.endpoint) - if (err != nil) != tt.wantErr { - t.Errorf("PortPart() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("PortPart() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/proxy/util/utils.go b/pkg/proxy/util/utils.go index 260894461d5..01d63eef132 100644 --- a/pkg/proxy/util/utils.go +++ b/pkg/proxy/util/utils.go @@ -19,21 +19,18 @@ package util import ( "fmt" "net" - "strconv" "strings" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" - utilrand "k8s.io/apimachinery/pkg/util/rand" "k8s.io/apimachinery/pkg/util/sets" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/tools/events" utilsysctl "k8s.io/component-helpers/node/util/sysctl" - helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" + "k8s.io/klog/v2" + "k8s.io/kubernetes/pkg/apis/core/v1/helper" "k8s.io/kubernetes/pkg/features" netutils "k8s.io/utils/net" - - "k8s.io/klog/v2" ) const ( @@ -49,25 +46,6 @@ func isValidEndpoint(host string, port int) bool { return host != "" && port > 0 } -// BuildPortsToEndpointsMap builds a map of portname -> all ip:ports for that -// portname. Explode Endpoints.Subsets[*] into this structure. -func BuildPortsToEndpointsMap(endpoints *v1.Endpoints) map[string][]string { - portsToEndpoints := map[string][]string{} - for i := range endpoints.Subsets { - ss := &endpoints.Subsets[i] - for i := range ss.Ports { - port := &ss.Ports[i] - for i := range ss.Addresses { - addr := &ss.Addresses[i] - if isValidEndpoint(addr.IP, int(port.Port)) { - portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], net.JoinHostPort(addr.IP, strconv.Itoa(int(port.Port)))) - } - } - } - } - return portsToEndpoints -} - // IsZeroCIDR checks whether the input CIDR string is either // the IPv4 or IPv6 zero CIDR func IsZeroCIDR(cidr string) bool { @@ -77,51 +55,6 @@ func IsZeroCIDR(cidr string) bool { return false } -// IsLoopBack checks if a given IP address is a loopback address. -func IsLoopBack(ip string) bool { - netIP := netutils.ParseIPSloppy(ip) - if netIP != nil { - return netIP.IsLoopback() - } - return false -} - -// GetLocalAddrs returns a list of all network addresses on the local system -func GetLocalAddrs() ([]net.IP, error) { - var localAddrs []net.IP - - addrs, err := net.InterfaceAddrs() - if err != nil { - return nil, err - } - - for _, addr := range addrs { - ip, _, err := netutils.ParseCIDRSloppy(addr.String()) - if err != nil { - return nil, err - } - - localAddrs = append(localAddrs, ip) - } - - return localAddrs, nil -} - -// GetLocalAddrSet return a local IPSet. -// If failed to get local addr, will assume no local ips. -func GetLocalAddrSet() netutils.IPSet { - localAddrs, err := GetLocalAddrs() - if err != nil { - klog.ErrorS(err, "Failed to get local addresses assuming no local IPs") - } else if len(localAddrs) == 0 { - klog.InfoS("No local addresses were found") - } - - localAddrSet := netutils.IPSet{} - localAddrSet.Insert(localAddrs...) - return localAddrSet -} - // ShouldSkipService checks if a given service should skip proxying func ShouldSkipService(service *v1.Service) bool { // if ClusterIP is "None" or empty, skip proxying @@ -261,20 +194,6 @@ func AppendPortIfNeeded(addr string, port int32) string { return fmt.Sprintf("[%s]:%d", addr, port) } -// ShuffleStrings copies strings from the specified slice into a copy in random -// order. It returns a new slice. -func ShuffleStrings(s []string) []string { - if s == nil { - return nil - } - shuffled := make([]string, len(s)) - perm := utilrand.Perm(len(s)) - for i, j := range perm { - shuffled[j] = s[i] - } - return shuffled -} - // EnsureSysctl sets a kernel sysctl to a given numeric value. func EnsureSysctl(sysctl utilsysctl.Interface, name string, newVal int) error { if oldVal, _ := sysctl.GetSysctl(name); oldVal != newVal { @@ -313,18 +232,6 @@ func GetClusterIPByFamily(ipFamily v1.IPFamily, service *v1.Service) string { return "" } -// RevertPorts is closing ports in replacementPortsMap but not in originalPortsMap. In other words, it only -// closes the ports opened in this sync. -func RevertPorts(replacementPortsMap, originalPortsMap map[netutils.LocalPort]netutils.Closeable) { - for k, v := range replacementPortsMap { - // Only close newly opened local ports - leave ones that were open before this update - if originalPortsMap[k] == nil { - klog.V(2).InfoS("Closing local port", "port", k.String()) - v.Close() - } - } -} - func IsVIPMode(ing v1.LoadBalancerIngress) bool { if !utilfeature.DefaultFeatureGate.Enabled(features.LoadBalancerIPMode) { return true // backwards compat diff --git a/pkg/proxy/util/utils_test.go b/pkg/proxy/util/utils_test.go index 3895cd755db..1f82de0fce9 100644 --- a/pkg/proxy/util/utils_test.go +++ b/pkg/proxy/util/utils_test.go @@ -42,57 +42,6 @@ func TestValidateWorks(t *testing.T) { } } -func TestBuildPortsToEndpointsMap(t *testing.T) { - endpoints := &v1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "testnamespace"}, - Subsets: []v1.EndpointSubset{ - { - Addresses: []v1.EndpointAddress{ - {IP: "10.0.0.1"}, - {IP: "10.0.0.2"}, - }, - Ports: []v1.EndpointPort{ - {Name: "http", Port: 80}, - {Name: "https", Port: 443}, - }, - }, - { - Addresses: []v1.EndpointAddress{ - {IP: "10.0.0.1"}, - {IP: "10.0.0.3"}, - }, - Ports: []v1.EndpointPort{ - {Name: "http", Port: 8080}, - {Name: "dns", Port: 53}, - }, - }, - { - Addresses: []v1.EndpointAddress{}, - Ports: []v1.EndpointPort{ - {Name: "http", Port: 8888}, - {Name: "ssh", Port: 22}, - }, - }, - { - Addresses: []v1.EndpointAddress{ - {IP: "10.0.0.1"}, - }, - Ports: []v1.EndpointPort{}, - }, - }, - } - expectedPortsToEndpoints := map[string][]string{ - "http": {"10.0.0.1:80", "10.0.0.2:80", "10.0.0.1:8080", "10.0.0.3:8080"}, - "https": {"10.0.0.1:443", "10.0.0.2:443"}, - "dns": {"10.0.0.1:53", "10.0.0.3:53"}, - } - - portsToEndpoints := BuildPortsToEndpointsMap(endpoints) - if !reflect.DeepEqual(expectedPortsToEndpoints, portsToEndpoints) { - t.Errorf("expected ports to endpoints not seen") - } -} - func TestShouldSkipService(t *testing.T) { testCases := []struct { service *v1.Service @@ -219,33 +168,6 @@ func TestAppendPortIfNeeded(t *testing.T) { } } -func TestShuffleStrings(t *testing.T) { - var src []string - dest := ShuffleStrings(src) - - if dest != nil { - t.Errorf("ShuffleStrings for a nil slice got a non-nil slice") - } - - src = []string{"a", "b", "c", "d", "e", "f"} - dest = ShuffleStrings(src) - - if len(src) != len(dest) { - t.Errorf("Shuffled slice is wrong length, expected %v got %v", len(src), len(dest)) - } - - m := make(map[string]bool, len(dest)) - for _, s := range dest { - m[s] = true - } - - for _, k := range src { - if _, exists := m[k]; !exists { - t.Errorf("Element %v missing from shuffled slice", k) - } - } -} - func TestMapIPsByIPFamily(t *testing.T) { testCases := []struct { desc string @@ -625,103 +547,6 @@ func TestGetClusterIPByFamily(t *testing.T) { } } -type fakeClosable struct { - closed bool -} - -func (c *fakeClosable) Close() error { - c.closed = true - return nil -} - -func TestRevertPorts(t *testing.T) { - testCases := []struct { - replacementPorts []netutils.LocalPort - existingPorts []netutils.LocalPort - expectToBeClose []bool - }{ - { - replacementPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []netutils.LocalPort{}, - expectToBeClose: []bool{true, true, true}, - }, - { - replacementPorts: []netutils.LocalPort{}, - existingPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - expectToBeClose: []bool{}, - }, - { - replacementPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - expectToBeClose: []bool{false, false, false}, - }, - { - replacementPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5003}, - }, - expectToBeClose: []bool{false, true, false}, - }, - { - replacementPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []netutils.LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - {Port: 5004}, - }, - expectToBeClose: []bool{false, false, false}, - }, - } - - for i, tc := range testCases { - replacementPortsMap := make(map[netutils.LocalPort]netutils.Closeable) - for _, lp := range tc.replacementPorts { - replacementPortsMap[lp] = &fakeClosable{} - } - existingPortsMap := make(map[netutils.LocalPort]netutils.Closeable) - for _, lp := range tc.existingPorts { - existingPortsMap[lp] = &fakeClosable{} - } - RevertPorts(replacementPortsMap, existingPortsMap) - for j, expectation := range tc.expectToBeClose { - if replacementPortsMap[tc.replacementPorts[j]].(*fakeClosable).closed != expectation { - t.Errorf("Expect replacement localport %v to be %v in test case %v", tc.replacementPorts[j], expectation, i) - } - } - for _, lp := range tc.existingPorts { - if existingPortsMap[lp].(*fakeClosable).closed { - t.Errorf("Expect existing localport %v to be false in test case %v", lp, i) - } - } - } -} - func mustParseIPAddr(str string) net.Addr { a, err := net.ResolveIPAddr("ip", str) if err != nil {