Skip to content

Commit 3325cbb

Browse files
authored
Merge pull request kubernetes#84261 from oomichi/move-to-e2e-network-framework
Move funcs of networking_utils to e2e network
2 parents 9844261 + 6be4fd4 commit 3325cbb

File tree

3 files changed

+73
-71
lines changed

3 files changed

+73
-71
lines changed

test/e2e/framework/networking_utils.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -683,25 +683,6 @@ func (config *NetworkingTestConfig) getServiceClient() coreclientset.ServiceInte
683683
return config.f.ClientSet.CoreV1().Services(config.Namespace)
684684
}
685685

686-
// CheckReachabilityFromPod checks reachability from the specified pod.
687-
func CheckReachabilityFromPod(expectToBeReachable bool, timeout time.Duration, namespace, pod, target string) {
688-
cmd := fmt.Sprintf("wget -T 5 -qO- %q", target)
689-
err := wait.PollImmediate(Poll, timeout, func() (bool, error) {
690-
_, err := RunHostCmd(namespace, pod, cmd)
691-
if expectToBeReachable && err != nil {
692-
Logf("Expect target to be reachable. But got err: %v. Retry until timeout", err)
693-
return false, nil
694-
}
695-
696-
if !expectToBeReachable && err == nil {
697-
Logf("Expect target NOT to be reachable. But it is reachable. Retry until timeout")
698-
return false, nil
699-
}
700-
return true, nil
701-
})
702-
ExpectNoError(err)
703-
}
704-
705686
// HTTPPokeParams is a struct for HTTP poke parameters.
706687
type HTTPPokeParams struct {
707688
Timeout time.Duration
@@ -979,49 +960,6 @@ func PokeUDP(host string, port int, request string, params *UDPPokeParams) UDPPo
979960
return ret
980961
}
981962

982-
// TestHitNodesFromOutside checkes HTTP connectivity from outside.
983-
func TestHitNodesFromOutside(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String) error {
984-
return TestHitNodesFromOutsideWithCount(externalIP, httpPort, timeout, expectedHosts, 1)
985-
}
986-
987-
// TestHitNodesFromOutsideWithCount checkes HTTP connectivity from outside with count.
988-
func TestHitNodesFromOutsideWithCount(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String,
989-
countToSucceed int) error {
990-
Logf("Waiting up to %v for satisfying expectedHosts for %v times", timeout, countToSucceed)
991-
hittedHosts := sets.NewString()
992-
count := 0
993-
condition := func() (bool, error) {
994-
result := PokeHTTP(externalIP, int(httpPort), "/hostname", &HTTPPokeParams{Timeout: 1 * time.Second})
995-
if result.Status != HTTPSuccess {
996-
return false, nil
997-
}
998-
999-
hittedHost := strings.TrimSpace(string(result.Body))
1000-
if !expectedHosts.Has(hittedHost) {
1001-
Logf("Error hitting unexpected host: %v, reset counter: %v", hittedHost, count)
1002-
count = 0
1003-
return false, nil
1004-
}
1005-
if !hittedHosts.Has(hittedHost) {
1006-
hittedHosts.Insert(hittedHost)
1007-
Logf("Missing %+v, got %+v", expectedHosts.Difference(hittedHosts), hittedHosts)
1008-
}
1009-
if hittedHosts.Equal(expectedHosts) {
1010-
count++
1011-
if count >= countToSucceed {
1012-
return true, nil
1013-
}
1014-
}
1015-
return false, nil
1016-
}
1017-
1018-
if err := wait.Poll(time.Second, timeout, condition); err != nil {
1019-
return fmt.Errorf("error waiting for expectedHosts: %v, hittedHosts: %v, count: %v, expected count: %v",
1020-
expectedHosts, hittedHosts, count, countToSucceed)
1021-
}
1022-
return nil
1023-
}
1024-
1025963
// TestUnderTemporaryNetworkFailure blocks outgoing network traffic on 'node'. Then runs testFunc and returns its status.
1026964
// At the end (even in case of errors), the network traffic is brought back to normal.
1027965
// This function executes commands on a node so it will work only for some

test/e2e/network/firewall.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package network
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
"time"
2223

2324
v1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/util/sets"
26+
"k8s.io/apimachinery/pkg/util/wait"
2527
clientset "k8s.io/client-go/kubernetes"
2628
cloudprovider "k8s.io/cloud-provider"
2729
"k8s.io/kubernetes/pkg/master/ports"
@@ -160,7 +162,7 @@ var _ = SIGDescribe("Firewall rule", func() {
160162

161163
// Send requests from outside of the cluster because internal traffic is whitelisted
162164
ginkgo.By("Accessing the external service ip from outside, all non-master nodes should be reached")
163-
err = framework.TestHitNodesFromOutside(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet)
165+
err = testHitNodesFromOutside(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet)
164166
framework.ExpectNoError(err)
165167

166168
// Check if there are overlapping tags on the firewall that extend beyond just the vms in our cluster
@@ -181,12 +183,12 @@ var _ = SIGDescribe("Firewall rule", func() {
181183
nodesSet.Insert(nodesNames[0])
182184
gce.SetInstanceTags(cloudConfig, nodesNames[0], zone, removedTags)
183185
// Make sure traffic is recovered before exit
184-
err = framework.TestHitNodesFromOutside(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet)
186+
err = testHitNodesFromOutside(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet)
185187
framework.ExpectNoError(err)
186188
}()
187189

188190
ginkgo.By("Accessing serivce through the external ip and examine got no response from the node without tags")
189-
err = framework.TestHitNodesFromOutsideWithCount(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet, 15)
191+
err = testHitNodesFromOutsideWithCount(svcExternalIP, firewallTestHTTPPort, e2eservice.LoadBalancerPropagationTimeoutDefault, nodesSet, 15)
190192
framework.ExpectNoError(err)
191193
})
192194

@@ -228,3 +230,46 @@ func assertNotReachableHTTPTimeout(ip string, port int, timeout time.Duration) {
228230
framework.Failf("Was unexpectedly able to reach %s:%d", ip, port)
229231
}
230232
}
233+
234+
// testHitNodesFromOutside checkes HTTP connectivity from outside.
235+
func testHitNodesFromOutside(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String) error {
236+
return testHitNodesFromOutsideWithCount(externalIP, httpPort, timeout, expectedHosts, 1)
237+
}
238+
239+
// testHitNodesFromOutsideWithCount checkes HTTP connectivity from outside with count.
240+
func testHitNodesFromOutsideWithCount(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String,
241+
countToSucceed int) error {
242+
framework.Logf("Waiting up to %v for satisfying expectedHosts for %v times", timeout, countToSucceed)
243+
hittedHosts := sets.NewString()
244+
count := 0
245+
condition := func() (bool, error) {
246+
result := framework.PokeHTTP(externalIP, int(httpPort), "/hostname", &framework.HTTPPokeParams{Timeout: 1 * time.Second})
247+
if result.Status != framework.HTTPSuccess {
248+
return false, nil
249+
}
250+
251+
hittedHost := strings.TrimSpace(string(result.Body))
252+
if !expectedHosts.Has(hittedHost) {
253+
framework.Logf("Error hitting unexpected host: %v, reset counter: %v", hittedHost, count)
254+
count = 0
255+
return false, nil
256+
}
257+
if !hittedHosts.Has(hittedHost) {
258+
hittedHosts.Insert(hittedHost)
259+
framework.Logf("Missing %+v, got %+v", expectedHosts.Difference(hittedHosts), hittedHosts)
260+
}
261+
if hittedHosts.Equal(expectedHosts) {
262+
count++
263+
if count >= countToSucceed {
264+
return true, nil
265+
}
266+
}
267+
return false, nil
268+
}
269+
270+
if err := wait.Poll(time.Second, timeout, condition); err != nil {
271+
return fmt.Errorf("error waiting for expectedHosts: %v, hittedHosts: %v, count: %v, expected count: %v",
272+
expectedHosts, hittedHosts, count, countToSucceed)
273+
}
274+
return nil
275+
}

test/e2e/network/service.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,8 @@ var _ = SIGDescribe("Services", func() {
15351535
svcIP := e2eservice.GetIngressPoint(&svc.Status.LoadBalancer.Ingress[0])
15361536
// Wait longer as this is our first request after creation. We can't check using a separate method,
15371537
// because the LB should only be reachable from the "accept" pod
1538-
framework.CheckReachabilityFromPod(true, loadBalancerLagTimeout, namespace, acceptPod.Name, svcIP)
1539-
framework.CheckReachabilityFromPod(false, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
1538+
checkReachabilityFromPod(true, loadBalancerLagTimeout, namespace, acceptPod.Name, svcIP)
1539+
checkReachabilityFromPod(false, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
15401540

15411541
// Make sure dropPod is running. There are certain chances that the pod might be teminated due to unexpected reasons. dropPod, err = cs.CoreV1().Pods(namespace).Get(dropPod.Name, metav1.GetOptions{})
15421542
dropPod, err = cs.CoreV1().Pods(namespace).Get(dropPod.Name, metav1.GetOptions{})
@@ -1550,16 +1550,16 @@ var _ = SIGDescribe("Services", func() {
15501550
svc.Spec.LoadBalancerSourceRanges = []string{dropPod.Status.PodIP + "/32"}
15511551
})
15521552
framework.ExpectNoError(err)
1553-
framework.CheckReachabilityFromPod(false, normalReachabilityTimeout, namespace, acceptPod.Name, svcIP)
1554-
framework.CheckReachabilityFromPod(true, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
1553+
checkReachabilityFromPod(false, normalReachabilityTimeout, namespace, acceptPod.Name, svcIP)
1554+
checkReachabilityFromPod(true, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
15551555

15561556
ginkgo.By("Delete LoadBalancerSourceRange field and check reachability")
15571557
_, err = jig.UpdateService(func(svc *v1.Service) {
15581558
svc.Spec.LoadBalancerSourceRanges = nil
15591559
})
15601560
framework.ExpectNoError(err)
1561-
framework.CheckReachabilityFromPod(true, normalReachabilityTimeout, namespace, acceptPod.Name, svcIP)
1562-
framework.CheckReachabilityFromPod(true, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
1561+
checkReachabilityFromPod(true, normalReachabilityTimeout, namespace, acceptPod.Name, svcIP)
1562+
checkReachabilityFromPod(true, normalReachabilityTimeout, namespace, dropPod.Name, svcIP)
15631563
})
15641564

15651565
// TODO: Get rid of [DisabledForLargeClusters] tag when issue #56138 is fixed.
@@ -2572,3 +2572,22 @@ func launchHostExecPod(client clientset.Interface, ns, name string) *v1.Pod {
25722572
framework.ExpectNoError(err)
25732573
return pod
25742574
}
2575+
2576+
// checkReachabilityFromPod checks reachability from the specified pod.
2577+
func checkReachabilityFromPod(expectToBeReachable bool, timeout time.Duration, namespace, pod, target string) {
2578+
cmd := fmt.Sprintf("wget -T 5 -qO- %q", target)
2579+
err := wait.PollImmediate(framework.Poll, timeout, func() (bool, error) {
2580+
_, err := framework.RunHostCmd(namespace, pod, cmd)
2581+
if expectToBeReachable && err != nil {
2582+
framework.Logf("Expect target to be reachable. But got err: %v. Retry until timeout", err)
2583+
return false, nil
2584+
}
2585+
2586+
if !expectToBeReachable && err == nil {
2587+
framework.Logf("Expect target NOT to be reachable. But it is reachable. Retry until timeout")
2588+
return false, nil
2589+
}
2590+
return true, nil
2591+
})
2592+
framework.ExpectNoError(err)
2593+
}

0 commit comments

Comments
 (0)