diff --git a/test/e2e/framework/service/jig.go b/test/e2e/framework/service/jig.go index 78d01aac836..78cd28d1a05 100644 --- a/test/e2e/framework/service/jig.go +++ b/test/e2e/framework/service/jig.go @@ -169,6 +169,15 @@ func ReserveStaticNodePort(port int32) bool { // ReleaseStaticNodePort releases the specified port. // The corresponding service should have already been deleted, to ensure that the // port allocator doesn't try to reuse it before the apiserver considers it available. +// The caller should do it like below: +// +// ginkgo.DeferCleanup(func(ctx context.Context) { +// err := cs.CoreV1().Services(ns).Delete(ctx, serviceName, metav1.DeleteOptions{}) +// if err != nil && !apierrors.IsNotFound(err) { +// framework.ExpectNoError(err, "failed to delete service %s in namespace %s", serviceName, ns) +// } +// e2eservice.ReleaseStaticNodePort(nodePort) +// }) func ReleaseStaticNodePort(port int32) { staticPortAllocator.releasePort(port) } diff --git a/test/e2e/network/service.go b/test/e2e/network/service.go index 74055a34b34..5b64d9b9f9d 100644 --- a/test/e2e/network/service.go +++ b/test/e2e/network/service.go @@ -1747,7 +1747,13 @@ var _ = common.SIGDescribe("Services", func() { } nodePort := service.Spec.Ports[0].NodePort - defer e2eservice.ReleaseStaticNodePort(nodePort) + ginkgo.DeferCleanup(func(ctx context.Context) { + err := cs.CoreV1().Services(ns).Delete(ctx, serviceName, metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + framework.ExpectNoError(err, "failed to delete service %s in namespace %s", serviceName, ns) + } + e2eservice.ReleaseStaticNodePort(nodePort) + }) if service.Spec.Type != v1.ServiceTypeNodePort { framework.Failf("got unexpected Spec.Type for new service: %v", service)