@@ -58,37 +58,6 @@ func testEnvoyMetricsOverHTTPS() {
5858 Specify ("requests to metrics listener are served" , FlakeAttempts (3 ), func () {
5959 t := f .T ()
6060
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-
9261 clientCert , caBundle := f .Certs .GetTLSCertificate ("projectcontour" , "metrics-client" )
9362 client := http.Client {
9463 Transport : & http.Transport {
@@ -100,13 +69,32 @@ func testEnvoyMetricsOverHTTPS() {
10069 },
10170 }
10271
72+ var kubectlCmd * gexec.Session
73+ defer func () {
74+ if kubectlCmd != nil {
75+ f .Kubectl .StopKubectlPortForward (kubectlCmd )
76+ }
77+ }()
78+
10379 gomega .Eventually (func () int {
80+ var err error
81+ if kubectlCmd == nil {
82+ kubectlCmd , err = f .Kubectl .StartKubectlPortForward (18003 , 8003 , "projectcontour" , f .Deployment .EnvoyResourceAndName ())
83+ if err != nil {
84+ GinkgoWriter .Println ("failed to start port-forward:" , err )
85+ return 0
86+ }
87+ }
88+
10489 resp , err := client .Get ("https://localhost:18003/stats" )
10590 if err != nil {
106- GinkgoWriter .Println (err )
91+ GinkgoWriter .Println ("request failed, restarting port-forward:" , err )
92+ f .Kubectl .StopKubectlPortForward (kubectlCmd )
93+ kubectlCmd = nil
10794 return 0
10895 }
96+ defer resp .Body .Close ()
10997 return resp .StatusCode
110- }, "10s " , "1s" ).Should (gomega .Equal (http .StatusOK ))
98+ }, "30s " , "1s" ).Should (gomega .Equal (http .StatusOK ))
11199 })
112100}
0 commit comments