@@ -18,77 +18,40 @@ package infra
1818import (
1919 "crypto/tls"
2020 "net/http"
21- "time"
2221
2322 . "github.com/onsi/ginkgo/v2"
2423 "github.com/onsi/gomega"
24+ "github.com/onsi/gomega/gexec"
2525 "github.com/stretchr/testify/require"
2626
2727 "github.com/projectcontour/contour/test/e2e"
2828)
2929
3030func testMetrics () {
3131 Specify ("requests to default metrics listener are served" , func () {
32- t := f .T ()
33-
3432 res , ok := f .HTTP .MetricsRequestUntil (& e2e.HTTPRequestOpts {
3533 Path : "/stats" ,
3634 Condition : e2e .HasStatusCode (200 ),
3735 })
38- require .NotNil (t , res , "request never succeeded" )
39- require .Truef (t , ok , "expected 200 response code, got %d" , res .StatusCode )
36+ require .NotNil (f . T () , res , "request never succeeded" )
37+ require .Truef (f . T () , ok , "expected 200 response code, got %d" , res .StatusCode )
4038 })
4139}
4240
4341func testReady () {
4442 Specify ("requests to default ready listener are served" , func () {
45- t := f .T ()
46-
4743 res , ok := f .HTTP .MetricsRequestUntil (& e2e.HTTPRequestOpts {
4844 Path : "/ready" ,
4945 Condition : e2e .HasStatusCode (200 ),
5046 })
51- require .NotNil (t , res , "request never succeeded" )
52- require .Truef (t , ok , "expected 200 response code, got %d" , res .StatusCode )
47+ require .NotNil (f . T () , res , "request never succeeded" )
48+ require .Truef (f . T () , ok , "expected 200 response code, got %d" , res .StatusCode )
5349 })
5450}
5551
5652func testEnvoyMetricsOverHTTPS () {
5753 // Flake tracking issue: https://github.com/projectcontour/contour/issues/5932
5854 Specify ("requests to metrics listener are served" , FlakeAttempts (3 ), func () {
59- t := f .T ()
60-
61- // Port-forward seems to be flaky. Following sequence happens:
62- //
63- // 1. Envoy becomes ready.
64- // 2. Port-forward is started.
65- // 3. HTTPS request is sent but the connection times out with errors
66- // "error creating error stream for port 18003 -> 8003: Timeout occurred",
67- // "error creating forwarding stream for port 18003 -> 8003: Timeout occurred"
68- // 4. Meanwhile the metrics listener gets added.
69- // 5. Sometimes (one out of ~1-50 runs) port-forward gets stuck and packets are not forwarded
70- // even after listener is up and connection attempts are still regularly retried.
71- //
72- // When the problem occurs, Wireshark does not show any traffic on the container side.
73- // The problem could be e.g. undiscovered race condition with Kubernetes port-forward.
74- //
75- // Following workarounds seem to work:
76- //
77- // a) Add a fixed delay before port-forwarding.
78- // b) Wait for Envoy to have listener by observing Envoy logs before port-forwarding.
79- // c) Restart port-forwarding when connection attempts fail.
80- //
81- // Executing port-forward started in BeforeEach(), JustBeforeEach() or combining metrics
82- // port with the admin port-forward command (127.0.0.1:19001 -> 9001) did not help.
83- //
84- // The simplest workaround (a) is taken here.
85- time .Sleep (5 * time .Second )
86-
87- // Port-forward for metrics over HTTPS
88- kubectlCmd , err := f .Kubectl .StartKubectlPortForward (18003 , 8003 , "projectcontour" , f .Deployment .EnvoyResourceAndName ())
89- require .NoError (t , err )
90- defer f .Kubectl .StopKubectlPortForward (kubectlCmd )
91-
9255 clientCert , caBundle := f .Certs .GetTLSCertificate ("projectcontour" , "metrics-client" )
9356 client := http.Client {
9457 Transport : & http.Transport {
@@ -100,13 +63,32 @@ func testEnvoyMetricsOverHTTPS() {
10063 },
10164 }
10265
66+ var kubectlCmd * gexec.Session
67+ defer func () {
68+ if kubectlCmd != nil {
69+ f .Kubectl .StopKubectlPortForward (kubectlCmd )
70+ }
71+ }()
72+
10373 gomega .Eventually (func () int {
74+ var err error
75+ if kubectlCmd == nil {
76+ kubectlCmd , err = f .Kubectl .StartKubectlPortForward (18003 , 8003 , "projectcontour" , f .Deployment .EnvoyResourceAndName ())
77+ if err != nil {
78+ GinkgoWriter .Println ("failed to start port-forward:" , err )
79+ return 0
80+ }
81+ }
82+
10483 resp , err := client .Get ("https://localhost:18003/stats" )
10584 if err != nil {
106- GinkgoWriter .Println (err )
85+ GinkgoWriter .Println ("request failed, restarting port-forward:" , err )
86+ f .Kubectl .StopKubectlPortForward (kubectlCmd )
87+ kubectlCmd = nil
10788 return 0
10889 }
90+ defer resp .Body .Close ()
10991 return resp .StatusCode
110- }, "10s " , "1s" ).Should (gomega .Equal (http .StatusOK ))
92+ }, "30s " , "1s" ).Should (gomega .Equal (http .StatusOK ))
11193 })
11294}
0 commit comments