From a2b57b9fb25738fbefbca8ca2a1f61ca050101ab Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 22 Feb 2021 16:48:28 +0100 Subject: [PATCH 1/4] fix test udp reachability --- test/e2e/framework/service/jig.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index c48b0785caf..033652aa699 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -890,19 +890,24 @@ func testEndpointReachability(endpoint string, port int32, protocol v1.Protocol, cmd := "" switch protocol { case v1.ProtocolTCP: - cmd = fmt.Sprintf("nc -zv -t -w 2 %s %v", endpoint, port) + cmd = fmt.Sprintf("echo hostName | nc -v -t -w 2 %s %v", endpoint, port) case v1.ProtocolUDP: - cmd = fmt.Sprintf("nc -zv -u -w 2 %s %v", endpoint, port) + cmd = fmt.Sprintf("echo hostName | nc -v -u -w 2 %s %v", endpoint, port) default: - return fmt.Errorf("service reachablity check is not supported for %v", protocol) + return fmt.Errorf("service reachability check is not supported for %v", protocol) } err := wait.PollImmediate(1*time.Second, ServiceReachabilityShortPollTimeout, func() (bool, error) { - if _, err := framework.RunHostCmd(execPod.Namespace, execPod.Name, cmd); err != nil { + stdout, err := framework.RunHostCmd(execPod.Namespace, execPod.Name, cmd) + if err != nil { framework.Logf("Service reachability failing with error: %v\nRetrying...", err) return false, nil } - return true, nil + trimmed := strings.TrimSpace(stdout) + if trimmed != "" { + return true, nil + } + return false, nil }) if err != nil { return fmt.Errorf("service is not reachable within %v timeout on endpoint %s over %s protocol", ServiceReachabilityShortPollTimeout, ep, protocol) From c9cbe41347072378037812c8eaba64868ac16bfa Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 22 Feb 2021 17:39:50 +0100 Subject: [PATCH 2/4] remove duplicate functions --- test/e2e/framework/service/jig.go | 18 +----------------- test/e2e/network/service.go | 2 +- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index 033652aa699..f8c6da7a540 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -1026,24 +1026,8 @@ func (j *TestJig) CheckServiceReachability(svc *v1.Service, pod *v1.Pod) error { } } -// CreateServicePods creates a replication controller with the label same as service. Service listens to HTTP. +// CreateServicePods creates a replication controller with the label same as service. Service listens to TCP and UDP. func (j *TestJig) CreateServicePods(replica int) error { - config := testutils.RCConfig{ - Client: j.Client, - Name: j.Name, - Image: framework.ServeHostnameImage, - Command: []string{"/agnhost", "serve-hostname"}, - Namespace: j.Namespace, - Labels: j.Labels, - PollInterval: 3 * time.Second, - Timeout: framework.PodReadyBeforeTimeout, - Replicas: replica, - } - return e2erc.RunRC(config) -} - -// CreateTCPUDPServicePods creates a replication controller with the label same as service. Service listens to TCP and UDP. -func (j *TestJig) CreateTCPUDPServicePods(replica int) error { config := testutils.RCConfig{ Client: j.Client, Name: j.Name, diff --git a/test/e2e/network/service.go b/test/e2e/network/service.go index 9f668c52e89..3bed313b6a3 100644 --- a/test/e2e/network/service.go +++ b/test/e2e/network/service.go @@ -1228,7 +1228,7 @@ var _ = SIGDescribe("Services", func() { }) framework.ExpectNoError(err) - err = jig.CreateTCPUDPServicePods(2) + err = jig.CreateServicePods(2) framework.ExpectNoError(err) execPod := e2epod.CreateExecPodOrFail(cs, ns, "execpod", nil) err = jig.CheckServiceReachability(nodePortService, execPod) From 21ccf1d7fb06db02d31f972a6e6f410fd9ed9cb7 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 22 Feb 2021 16:49:57 +0100 Subject: [PATCH 3/4] simplify clusterIP IP validation --- test/e2e/framework/service/jig.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index f8c6da7a540..0ad15bc8c40 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "net" - "regexp" "strconv" "strings" "time" @@ -44,7 +43,6 @@ import ( clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/cache" "k8s.io/kubernetes/test/e2e/framework" - e2enetwork "k8s.io/kubernetes/test/e2e/framework/network" e2enode "k8s.io/kubernetes/test/e2e/framework/node" e2epod "k8s.io/kubernetes/test/e2e/framework/pod" e2erc "k8s.io/kubernetes/test/e2e/framework/rc" @@ -819,14 +817,10 @@ func testReachabilityOverServiceName(serviceName string, sp v1.ServicePort, exec func testReachabilityOverClusterIP(clusterIP string, sp v1.ServicePort, execPod *v1.Pod) error { // If .spec.clusterIP is set to "" or "None" for service, ClusterIP is not created, so reachability can not be tested over clusterIP:servicePort - isClusterIPV46, err := regexp.MatchString(e2enetwork.RegexIPv4+"||"+e2enetwork.RegexIPv6, clusterIP) - if err != nil { + if net.ParseIP(clusterIP) == nil { return fmt.Errorf("unable to parse ClusterIP: %s", clusterIP) } - if isClusterIPV46 { - return testEndpointReachability(clusterIP, sp.Port, sp.Protocol, execPod) - } - return nil + return testEndpointReachability(clusterIP, sp.Port, sp.Protocol, execPod) } func testReachabilityOverExternalIP(externalIP string, sp v1.ServicePort, execPod *v1.Pod) error { From cf5d98b17e24dfccd7ae95eb16d6acd284e14274 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 22 Feb 2021 17:37:22 +0100 Subject: [PATCH 4/4] use utils/net ip family helpers --- test/e2e/framework/service/BUILD | 1 + test/e2e/framework/service/jig.go | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/e2e/framework/service/BUILD b/test/e2e/framework/service/BUILD index 3af1bb6cf31..550204b04c2 100644 --- a/test/e2e/framework/service/BUILD +++ b/test/e2e/framework/service/BUILD @@ -38,6 +38,7 @@ go_library( "//test/utils:go_default_library", "//test/utils/image:go_default_library", "//vendor/github.com/onsi/ginkgo:go_default_library", + "//vendor/k8s.io/utils/net:go_default_library", ], ) diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index 0ad15bc8c40..bb1f7ac68ec 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -48,6 +48,7 @@ import ( e2erc "k8s.io/kubernetes/test/e2e/framework/rc" testutils "k8s.io/kubernetes/test/utils" imageutils "k8s.io/kubernetes/test/utils/image" + utilsnet "k8s.io/utils/net" ) // NodePortRange should match whatever the default/configured range is @@ -830,7 +831,7 @@ func testReachabilityOverExternalIP(externalIP string, sp v1.ServicePort, execPo func testReachabilityOverNodePorts(nodes *v1.NodeList, sp v1.ServicePort, pod *v1.Pod, clusterIP string) error { internalAddrs := e2enode.CollectAddresses(nodes, v1.NodeInternalIP) externalAddrs := e2enode.CollectAddresses(nodes, v1.NodeExternalIP) - isClusterIPV4 := net.ParseIP(clusterIP).To4() != nil + isClusterIPV4 := utilsnet.IsIPv4String(clusterIP) for _, internalAddr := range internalAddrs { // If the node's internal address points to localhost, then we are not @@ -839,10 +840,8 @@ func testReachabilityOverNodePorts(nodes *v1.NodeList, sp v1.ServicePort, pod *v framework.Logf("skipping testEndpointReachability() for internal adddress %s", internalAddr) continue } - isNodeInternalIPV4 := net.ParseIP(internalAddr).To4() != nil - // Check service reachability on the node internalIP which is same family - // as clusterIP - if isClusterIPV4 != isNodeInternalIPV4 { + // Check service reachability on the node internalIP which is same family as clusterIP + if isClusterIPV4 != utilsnet.IsIPv4String(internalAddr) { framework.Logf("skipping testEndpointReachability() for internal adddress %s as it does not match clusterIP (%s) family", internalAddr, clusterIP) continue } @@ -853,8 +852,7 @@ func testReachabilityOverNodePorts(nodes *v1.NodeList, sp v1.ServicePort, pod *v } } for _, externalAddr := range externalAddrs { - isNodeExternalIPV4 := net.ParseIP(externalAddr).To4() != nil - if isClusterIPV4 != isNodeExternalIPV4 { + if isClusterIPV4 != utilsnet.IsIPv4String(externalAddr) { framework.Logf("skipping testEndpointReachability() for external adddress %s as it does not match clusterIP (%s) family", externalAddr, clusterIP) continue }