From 774c15f2beb712910ac86fe5ab8aa27f1c9eb29f Mon Sep 17 00:00:00 2001 From: draveness Date: Sat, 27 Apr 2019 10:50:28 +0800 Subject: [PATCH] refactor: move wait for endpoints to new pkg --- test/e2e/framework/BUILD | 1 + test/e2e/framework/endpoints/BUILD | 28 ++++++++++++++ test/e2e/framework/endpoints/wait.go | 56 ++++++++++++++++++++++++++++ test/e2e/framework/util.go | 21 ----------- test/e2e/network/BUILD | 1 + test/e2e/network/proxy.go | 5 ++- 6 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 test/e2e/framework/endpoints/BUILD create mode 100644 test/e2e/framework/endpoints/wait.go diff --git a/test/e2e/framework/BUILD b/test/e2e/framework/BUILD index 3f50edce125..a1ae210d6ce 100644 --- a/test/e2e/framework/BUILD +++ b/test/e2e/framework/BUILD @@ -149,6 +149,7 @@ filegroup( "//test/e2e/framework/auth:all-srcs", "//test/e2e/framework/config:all-srcs", "//test/e2e/framework/deployment:all-srcs", + "//test/e2e/framework/endpoints:all-srcs", "//test/e2e/framework/ginkgowrapper:all-srcs", "//test/e2e/framework/gpu:all-srcs", "//test/e2e/framework/ingress:all-srcs", diff --git a/test/e2e/framework/endpoints/BUILD b/test/e2e/framework/endpoints/BUILD new file mode 100644 index 00000000000..29eae19fe48 --- /dev/null +++ b/test/e2e/framework/endpoints/BUILD @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["wait.go"], + importpath = "k8s.io/kubernetes/test/e2e/framework/endpoints", + visibility = ["//visibility:public"], + deps = [ + "//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes:go_default_library", + "//test/e2e/framework:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/test/e2e/framework/endpoints/wait.go b/test/e2e/framework/endpoints/wait.go new file mode 100644 index 00000000000..7f7c3f59033 --- /dev/null +++ b/test/e2e/framework/endpoints/wait.go @@ -0,0 +1,56 @@ +/* +Copyright 2019 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. +*/ + +/* +This soak tests places a specified number of pods on each node and then +repeatedly sends queries to a service running on these pods via +a serivce +*/ + +package endpoints + +import ( + "fmt" + "time" + + apierrs "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/kubernetes/test/e2e/framework" +) + +const ( + // registerTimeout is how long to wait for an endpoint to be registered. + registerTimeout = time.Minute +) + +// WaitForEndpoint waits for the specified endpoint to be ready. +func WaitForEndpoint(c clientset.Interface, ns, name string) error { + for t := time.Now(); time.Since(t) < registerTimeout; time.Sleep(framework.Poll) { + endpoint, err := c.CoreV1().Endpoints(ns).Get(name, metav1.GetOptions{}) + if apierrs.IsNotFound(err) { + framework.Logf("Endpoint %s/%s is not ready yet", ns, name) + continue + } + framework.ExpectNoError(err, "Failed to get endpoints for %s/%s", ns, name) + if len(endpoint.Subsets) == 0 || len(endpoint.Subsets[0].Addresses) == 0 { + framework.Logf("Endpoint %s/%s is not ready yet", ns, name) + continue + } + return nil + } + return fmt.Errorf("failed to get endpoints for %s/%s", ns, name) +} diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index bf105a0bc2c..36c1711627a 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -158,8 +158,6 @@ const ( podRespondingTimeout = 15 * time.Minute // ServiceRespondingTimeout is how long to wait for a service to be responding. ServiceRespondingTimeout = 2 * time.Minute - // EndpointRegisterTimeout is how long to wait for an endpoint to be registered. - EndpointRegisterTimeout = time.Minute // ClaimProvisionTimeout is how long claims have to become dynamically provisioned. ClaimProvisionTimeout = 5 * time.Minute @@ -1748,25 +1746,6 @@ func countEndpointsNum(e *v1.Endpoints) int { return num } -// WaitForEndpoint waits for the specified endpoint to be ready. -func WaitForEndpoint(c clientset.Interface, ns, name string) error { - for t := time.Now(); time.Since(t) < EndpointRegisterTimeout; time.Sleep(Poll) { - endpoint, err := c.CoreV1().Endpoints(ns).Get(name, metav1.GetOptions{}) - if apierrs.IsNotFound(err) { - Logf("Endpoint %s/%s is not ready yet", ns, name) - continue - } - ExpectNoError(err, "Failed to get endpoints for %s/%s", ns, name) - if len(endpoint.Subsets) == 0 || len(endpoint.Subsets[0].Addresses) == 0 { - Logf("Endpoint %s/%s is not ready yet", ns, name) - continue - } else { - return nil - } - } - return fmt.Errorf("Failed to get endpoints for %s/%s", ns, name) -} - // PodProxyResponseChecker is a context for checking pods responses by issuing GETs to them (via the API // proxy) and verifying that they answer with their own pod name. type PodProxyResponseChecker struct { diff --git a/test/e2e/network/BUILD b/test/e2e/network/BUILD index fd215ea1860..c149d7431e2 100644 --- a/test/e2e/network/BUILD +++ b/test/e2e/network/BUILD @@ -59,6 +59,7 @@ go_library( "//staging/src/k8s.io/cloud-provider:go_default_library", "//test/e2e/framework:go_default_library", "//test/e2e/framework/auth:go_default_library", + "//test/e2e/framework/endpoints:go_default_library", "//test/e2e/framework/ingress:go_default_library", "//test/e2e/framework/providers/gce:go_default_library", "//test/e2e/network/scale:go_default_library", diff --git a/test/e2e/network/proxy.go b/test/e2e/network/proxy.go index 0a5627aac9d..476f9f92f03 100644 --- a/test/e2e/network/proxy.go +++ b/test/e2e/network/proxy.go @@ -26,13 +26,14 @@ import ( "sync" "time" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/net" clientset "k8s.io/client-go/kubernetes" "k8s.io/kubernetes/test/e2e/framework" + "k8s.io/kubernetes/test/e2e/framework/endpoints" testutils "k8s.io/kubernetes/test/utils" imageutils "k8s.io/kubernetes/test/utils/image" @@ -161,7 +162,7 @@ var _ = SIGDescribe("Proxy", func() { Expect(framework.RunRC(cfg)).NotTo(HaveOccurred()) defer framework.DeleteRCAndWaitForGC(f.ClientSet, f.Namespace.Name, cfg.Name) - Expect(framework.WaitForEndpoint(f.ClientSet, f.Namespace.Name, service.Name)).NotTo(HaveOccurred()) + Expect(endpoints.WaitForEndpoint(f.ClientSet, f.Namespace.Name, service.Name)).NotTo(HaveOccurred()) // table constructors // Try proxying through the service and directly to through the pod.