From 00e26e97855dbc47e47dcd09569430642470a480 Mon Sep 17 00:00:00 2001 From: jornshen Date: Mon, 18 Jan 2021 21:38:32 +0800 Subject: [PATCH] clear pkg/proxy/port.go port_test.go file --- pkg/proxy/util/BUILD | 3 +- pkg/proxy/util/port.go | 67 ---------------- pkg/proxy/util/port_test.go | 145 ----------------------------------- pkg/proxy/util/utils.go | 12 +++ pkg/proxy/util/utils_test.go | 97 +++++++++++++++++++++++ 5 files changed, 110 insertions(+), 214 deletions(-) delete mode 100644 pkg/proxy/util/port.go delete mode 100644 pkg/proxy/util/port_test.go diff --git a/pkg/proxy/util/BUILD b/pkg/proxy/util/BUILD index 4f1ce9864b1..842bb1f7947 100644 --- a/pkg/proxy/util/BUILD +++ b/pkg/proxy/util/BUILD @@ -5,7 +5,6 @@ go_library( srcs = [ "endpoints.go", "network.go", - "port.go", "utils.go", ], importpath = "k8s.io/kubernetes/pkg/proxy/util", @@ -27,7 +26,6 @@ go_test( name = "go_default_test", srcs = [ "endpoints_test.go", - "port_test.go", "utils_test.go", ], embed = [":go_default_library"], @@ -36,6 +34,7 @@ go_test( "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", + "//vendor/k8s.io/utils/net:go_default_library", ], ) diff --git a/pkg/proxy/util/port.go b/pkg/proxy/util/port.go deleted file mode 100644 index e256bd9ec44..00000000000 --- a/pkg/proxy/util/port.go +++ /dev/null @@ -1,67 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "fmt" - "net" - "strconv" - - "k8s.io/klog/v2" -) - -// LocalPort describes a port on specific IP address and protocol -type LocalPort struct { - // Description is the identity message of a given local port. - Description string - // IP is the IP address part of a given local port. - // If this string is empty, the port binds to all local IP addresses. - IP string - // Port is the port part of a given local port. - Port int - // Protocol is the protocol part of a given local port. - // The value is assumed to be lower-case. For example, "udp" not "UDP", "tcp" not "TCP". - Protocol string -} - -func (lp *LocalPort) String() string { - ipPort := net.JoinHostPort(lp.IP, strconv.Itoa(lp.Port)) - return fmt.Sprintf("%q (%s/%s)", lp.Description, ipPort, lp.Protocol) -} - -// Closeable is an interface around closing a port. -type Closeable interface { - Close() error -} - -// PortOpener is an interface around port opening/closing. -// Abstracted out for testing. -type PortOpener interface { - OpenLocalPort(lp *LocalPort, isIPv6 bool) (Closeable, error) -} - -// 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[LocalPort]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).Infof("Closing local port %s", k.String()) - v.Close() - } - } -} diff --git a/pkg/proxy/util/port_test.go b/pkg/proxy/util/port_test.go deleted file mode 100644 index 7295fd5000c..00000000000 --- a/pkg/proxy/util/port_test.go +++ /dev/null @@ -1,145 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import "testing" - -type fakeClosable struct { - closed bool -} - -func (c *fakeClosable) Close() error { - c.closed = true - return nil -} - -func TestLocalPortString(t *testing.T) { - testCases := []struct { - description string - ip string - port int - protocol string - expectedStr string - }{ - {"IPv4 UDP", "1.2.3.4", 9999, "udp", "\"IPv4 UDP\" (1.2.3.4:9999/udp)"}, - {"IPv4 TCP", "5.6.7.8", 1053, "tcp", "\"IPv4 TCP\" (5.6.7.8:1053/tcp)"}, - {"IPv6 TCP", "2001:db8::1", 80, "tcp", "\"IPv6 TCP\" ([2001:db8::1]:80/tcp)"}, - {"IPv4 SCTP", "9.10.11.12", 7777, "sctp", "\"IPv4 SCTP\" (9.10.11.12:7777/sctp)"}, - {"IPv6 SCTP", "2001:db8::2", 80, "sctp", "\"IPv6 SCTP\" ([2001:db8::2]:80/sctp)"}, - } - - for _, tc := range testCases { - lp := &LocalPort{ - Description: tc.description, - IP: tc.ip, - Port: tc.port, - Protocol: tc.protocol, - } - str := lp.String() - if str != tc.expectedStr { - t.Errorf("Unexpected output for %s, expected: %s, got: %s", tc.description, tc.expectedStr, str) - } - } -} - -func TestRevertPorts(t *testing.T) { - testCases := []struct { - replacementPorts []LocalPort - existingPorts []LocalPort - expectToBeClose []bool - }{ - { - replacementPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []LocalPort{}, - expectToBeClose: []bool{true, true, true}, - }, - { - replacementPorts: []LocalPort{}, - existingPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - expectToBeClose: []bool{}, - }, - { - replacementPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - expectToBeClose: []bool{false, false, false}, - }, - { - replacementPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []LocalPort{ - {Port: 5001}, - {Port: 5003}, - }, - expectToBeClose: []bool{false, true, false}, - }, - { - replacementPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - }, - existingPorts: []LocalPort{ - {Port: 5001}, - {Port: 5002}, - {Port: 5003}, - {Port: 5004}, - }, - expectToBeClose: []bool{false, false, false}, - }, - } - - for i, tc := range testCases { - replacementPortsMap := make(map[LocalPort]Closeable) - for _, lp := range tc.replacementPorts { - replacementPortsMap[lp] = &fakeClosable{} - } - existingPortsMap := make(map[LocalPort]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 == true { - t.Errorf("Expect existing localport %v to be false in test case %v", lp, i) - } - } - } -} diff --git a/pkg/proxy/util/utils.go b/pkg/proxy/util/utils.go index b4ce2772292..45b46582de5 100644 --- a/pkg/proxy/util/utils.go +++ b/pkg/proxy/util/utils.go @@ -482,3 +482,15 @@ func WriteBytesLine(buf *bytes.Buffer, bytes []byte) { buf.Write(bytes) buf.WriteByte('\n') } + +// 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[utilnet.LocalPort]utilnet.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).Infof("Closing local port %s", k.String()) + v.Close() + } + } +} diff --git a/pkg/proxy/util/utils_test.go b/pkg/proxy/util/utils_test.go index 895b3b7c245..f2467405f25 100644 --- a/pkg/proxy/util/utils_test.go +++ b/pkg/proxy/util/utils_test.go @@ -29,6 +29,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" fake "k8s.io/kubernetes/pkg/proxy/util/testing" + utilnet "k8s.io/utils/net" ) func TestValidateWorks(t *testing.T) { @@ -1048,7 +1049,103 @@ 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 []utilnet.LocalPort + existingPorts []utilnet.LocalPort + expectToBeClose []bool + }{ + { + replacementPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + existingPorts: []utilnet.LocalPort{}, + expectToBeClose: []bool{true, true, true}, + }, + { + replacementPorts: []utilnet.LocalPort{}, + existingPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + expectToBeClose: []bool{}, + }, + { + replacementPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + existingPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + expectToBeClose: []bool{false, false, false}, + }, + { + replacementPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + existingPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5003}, + }, + expectToBeClose: []bool{false, true, false}, + }, + { + replacementPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + }, + existingPorts: []utilnet.LocalPort{ + {Port: 5001}, + {Port: 5002}, + {Port: 5003}, + {Port: 5004}, + }, + expectToBeClose: []bool{false, false, false}, + }, + } + + for i, tc := range testCases { + replacementPortsMap := make(map[utilnet.LocalPort]utilnet.Closeable) + for _, lp := range tc.replacementPorts { + replacementPortsMap[lp] = &fakeClosable{} + } + existingPortsMap := make(map[utilnet.LocalPort]utilnet.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 == true { + t.Errorf("Expect existing localport %v to be false in test case %v", lp, i) + } + } + } } func TestWriteLine(t *testing.T) {