Skip to content

Commit 15a5512

Browse files
🐛 Fix e2e test flakes when webhooks are scaffolded
Projects with webhooks may experience flaky e2e tests due to webhook server not being ready when the metrics test creates the curl-metrics pod.
1 parent 9eee7a7 commit 15a5512

File tree

8 files changed

+311
-256
lines changed

8 files changed

+311
-256
lines changed

docs/book/src/cronjob-tutorial/testdata/project/test/e2e/e2e_test.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -197,55 +197,59 @@ var _ = Describe("Manager", Ordered, func() {
197197
Expect(err).NotTo(HaveOccurred())
198198
Expect(token).NotTo(BeEmpty())
199199

200-
By("waiting for the metrics endpoint to be ready")
201-
verifyMetricsEndpointReady := func(g Gomega) {
202-
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
200+
By("ensuring the controller pod is fully ready before creating test pods")
201+
verifyControllerPodReady := func(g Gomega) {
202+
cmd := exec.Command("kubectl", "get", "pod", controllerPodName, "-n", namespace,
203+
"-o", "jsonpath={.status.conditions[?(@.type=='Ready')].status}")
203204
output, err := utils.Run(cmd)
204205
g.Expect(err).NotTo(HaveOccurred())
205-
g.Expect(output).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
206+
g.Expect(output).To(Equal("True"), "Controller pod not ready")
206207
}
207-
Eventually(verifyMetricsEndpointReady).Should(Succeed())
208+
Eventually(verifyControllerPodReady, 3*time.Minute, time.Second).Should(Succeed())
208209

209210
By("verifying that the controller manager is serving the metrics server")
210211
verifyMetricsServerStarted := func(g Gomega) {
211212
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
212213
output, err := utils.Run(cmd)
213214
g.Expect(err).NotTo(HaveOccurred())
214-
g.Expect(output).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
215+
g.Expect(output).To(ContainSubstring("Serving metrics server"),
215216
"Metrics server not yet started")
216217
}
217-
Eventually(verifyMetricsServerStarted).Should(Succeed())
218+
Eventually(verifyMetricsServerStarted, 3*time.Minute, time.Second).Should(Succeed())
218219

219220
By("creating the curl-metrics pod to access the metrics endpoint")
220-
cmd = exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
221-
"--namespace", namespace,
222-
"--image=curlimages/curl:latest",
223-
"--overrides",
224-
fmt.Sprintf(`{
225-
"spec": {
226-
"containers": [{
227-
"name": "curl",
228-
"image": "curlimages/curl:latest",
229-
"command": ["/bin/sh", "-c"],
230-
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
231-
"securityContext": {
232-
"readOnlyRootFilesystem": true,
233-
"allowPrivilegeEscalation": false,
234-
"capabilities": {
235-
"drop": ["ALL"]
236-
},
237-
"runAsNonRoot": true,
238-
"runAsUser": 1000,
239-
"seccompProfile": {
240-
"type": "RuntimeDefault"
221+
createCurlMetricsPod := func() error {
222+
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
223+
"--namespace", namespace,
224+
"--image=curlimages/curl:latest",
225+
"--overrides",
226+
fmt.Sprintf(`{
227+
"spec": {
228+
"containers": [{
229+
"name": "curl",
230+
"image": "curlimages/curl:latest",
231+
"command": ["/bin/sh", "-c"],
232+
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
233+
"securityContext": {
234+
"readOnlyRootFilesystem": true,
235+
"allowPrivilegeEscalation": false,
236+
"capabilities": {
237+
"drop": ["ALL"]
238+
},
239+
"runAsNonRoot": true,
240+
"runAsUser": 1000,
241+
"seccompProfile": {
242+
"type": "RuntimeDefault"
243+
}
241244
}
242-
}
243-
}],
244-
"serviceAccountName": "%s"
245-
}
246-
}`, token, metricsServiceName, namespace, serviceAccountName))
247-
_, err = utils.Run(cmd)
248-
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
245+
}],
246+
"serviceAccountName": "%s"
247+
}
248+
}`, token, metricsServiceName, namespace, serviceAccountName))
249+
_, err := utils.Run(cmd)
250+
return err
251+
}
252+
Eventually(createCurlMetricsPod, 3*time.Minute, time.Second).Should(Succeed(), "Failed to create curl-metrics pod")
249253

250254
By("waiting for the curl-metrics pod to complete.")
251255
verifyCurlUp := func(g Gomega) {

docs/book/src/getting-started/testdata/project/test/e2e/e2e_test.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -192,55 +192,59 @@ var _ = Describe("Manager", Ordered, func() {
192192
Expect(err).NotTo(HaveOccurred())
193193
Expect(token).NotTo(BeEmpty())
194194

195-
By("waiting for the metrics endpoint to be ready")
196-
verifyMetricsEndpointReady := func(g Gomega) {
197-
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
195+
By("ensuring the controller pod is fully ready before creating test pods")
196+
verifyControllerPodReady := func(g Gomega) {
197+
cmd := exec.Command("kubectl", "get", "pod", controllerPodName, "-n", namespace,
198+
"-o", "jsonpath={.status.conditions[?(@.type=='Ready')].status}")
198199
output, err := utils.Run(cmd)
199200
g.Expect(err).NotTo(HaveOccurred())
200-
g.Expect(output).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
201+
g.Expect(output).To(Equal("True"), "Controller pod not ready")
201202
}
202-
Eventually(verifyMetricsEndpointReady).Should(Succeed())
203+
Eventually(verifyControllerPodReady, 3*time.Minute, time.Second).Should(Succeed())
203204

204205
By("verifying that the controller manager is serving the metrics server")
205206
verifyMetricsServerStarted := func(g Gomega) {
206207
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
207208
output, err := utils.Run(cmd)
208209
g.Expect(err).NotTo(HaveOccurred())
209-
g.Expect(output).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
210+
g.Expect(output).To(ContainSubstring("Serving metrics server"),
210211
"Metrics server not yet started")
211212
}
212-
Eventually(verifyMetricsServerStarted).Should(Succeed())
213+
Eventually(verifyMetricsServerStarted, 3*time.Minute, time.Second).Should(Succeed())
213214

214215
By("creating the curl-metrics pod to access the metrics endpoint")
215-
cmd = exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
216-
"--namespace", namespace,
217-
"--image=curlimages/curl:latest",
218-
"--overrides",
219-
fmt.Sprintf(`{
220-
"spec": {
221-
"containers": [{
222-
"name": "curl",
223-
"image": "curlimages/curl:latest",
224-
"command": ["/bin/sh", "-c"],
225-
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
226-
"securityContext": {
227-
"readOnlyRootFilesystem": true,
228-
"allowPrivilegeEscalation": false,
229-
"capabilities": {
230-
"drop": ["ALL"]
231-
},
232-
"runAsNonRoot": true,
233-
"runAsUser": 1000,
234-
"seccompProfile": {
235-
"type": "RuntimeDefault"
216+
createCurlMetricsPod := func() error {
217+
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
218+
"--namespace", namespace,
219+
"--image=curlimages/curl:latest",
220+
"--overrides",
221+
fmt.Sprintf(`{
222+
"spec": {
223+
"containers": [{
224+
"name": "curl",
225+
"image": "curlimages/curl:latest",
226+
"command": ["/bin/sh", "-c"],
227+
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
228+
"securityContext": {
229+
"readOnlyRootFilesystem": true,
230+
"allowPrivilegeEscalation": false,
231+
"capabilities": {
232+
"drop": ["ALL"]
233+
},
234+
"runAsNonRoot": true,
235+
"runAsUser": 1000,
236+
"seccompProfile": {
237+
"type": "RuntimeDefault"
238+
}
236239
}
237-
}
238-
}],
239-
"serviceAccountName": "%s"
240-
}
241-
}`, token, metricsServiceName, namespace, serviceAccountName))
242-
_, err = utils.Run(cmd)
243-
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
240+
}],
241+
"serviceAccountName": "%s"
242+
}
243+
}`, token, metricsServiceName, namespace, serviceAccountName))
244+
_, err := utils.Run(cmd)
245+
return err
246+
}
247+
Eventually(createCurlMetricsPod, 3*time.Minute, time.Second).Should(Succeed(), "Failed to create curl-metrics pod")
244248

245249
By("waiting for the curl-metrics pod to complete.")
246250
verifyCurlUp := func(g Gomega) {

docs/book/src/multiversion-tutorial/testdata/project/test/e2e/e2e_test.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -204,55 +204,59 @@ var _ = Describe("Manager", Ordered, func() {
204204
Expect(err).NotTo(HaveOccurred())
205205
Expect(token).NotTo(BeEmpty())
206206

207-
By("waiting for the metrics endpoint to be ready")
208-
verifyMetricsEndpointReady := func(g Gomega) {
209-
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
207+
By("ensuring the controller pod is fully ready before creating test pods")
208+
verifyControllerPodReady := func(g Gomega) {
209+
cmd := exec.Command("kubectl", "get", "pod", controllerPodName, "-n", namespace,
210+
"-o", "jsonpath={.status.conditions[?(@.type=='Ready')].status}")
210211
output, err := utils.Run(cmd)
211212
g.Expect(err).NotTo(HaveOccurred())
212-
g.Expect(output).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
213+
g.Expect(output).To(Equal("True"), "Controller pod not ready")
213214
}
214-
Eventually(verifyMetricsEndpointReady).Should(Succeed())
215+
Eventually(verifyControllerPodReady, 3*time.Minute, time.Second).Should(Succeed())
215216

216217
By("verifying that the controller manager is serving the metrics server")
217218
verifyMetricsServerStarted := func(g Gomega) {
218219
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
219220
output, err := utils.Run(cmd)
220221
g.Expect(err).NotTo(HaveOccurred())
221-
g.Expect(output).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
222+
g.Expect(output).To(ContainSubstring("Serving metrics server"),
222223
"Metrics server not yet started")
223224
}
224-
Eventually(verifyMetricsServerStarted).Should(Succeed())
225+
Eventually(verifyMetricsServerStarted, 3*time.Minute, time.Second).Should(Succeed())
225226

226227
By("creating the curl-metrics pod to access the metrics endpoint")
227-
cmd = exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
228-
"--namespace", namespace,
229-
"--image=curlimages/curl:latest",
230-
"--overrides",
231-
fmt.Sprintf(`{
232-
"spec": {
233-
"containers": [{
234-
"name": "curl",
235-
"image": "curlimages/curl:latest",
236-
"command": ["/bin/sh", "-c"],
237-
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
238-
"securityContext": {
239-
"readOnlyRootFilesystem": true,
240-
"allowPrivilegeEscalation": false,
241-
"capabilities": {
242-
"drop": ["ALL"]
243-
},
244-
"runAsNonRoot": true,
245-
"runAsUser": 1000,
246-
"seccompProfile": {
247-
"type": "RuntimeDefault"
228+
createCurlMetricsPod := func() error {
229+
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
230+
"--namespace", namespace,
231+
"--image=curlimages/curl:latest",
232+
"--overrides",
233+
fmt.Sprintf(`{
234+
"spec": {
235+
"containers": [{
236+
"name": "curl",
237+
"image": "curlimages/curl:latest",
238+
"command": ["/bin/sh", "-c"],
239+
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
240+
"securityContext": {
241+
"readOnlyRootFilesystem": true,
242+
"allowPrivilegeEscalation": false,
243+
"capabilities": {
244+
"drop": ["ALL"]
245+
},
246+
"runAsNonRoot": true,
247+
"runAsUser": 1000,
248+
"seccompProfile": {
249+
"type": "RuntimeDefault"
250+
}
248251
}
249-
}
250-
}],
251-
"serviceAccountName": "%s"
252-
}
253-
}`, token, metricsServiceName, namespace, serviceAccountName))
254-
_, err = utils.Run(cmd)
255-
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
252+
}],
253+
"serviceAccountName": "%s"
254+
}
255+
}`, token, metricsServiceName, namespace, serviceAccountName))
256+
_, err := utils.Run(cmd)
257+
return err
258+
}
259+
Eventually(createCurlMetricsPod, 3*time.Minute, time.Second).Should(Succeed(), "Failed to create curl-metrics pod")
256260

257261
By("waiting for the curl-metrics pod to complete.")
258262
verifyCurlUp := func(g Gomega) {

pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -375,55 +375,59 @@ var _ = Describe("Manager", Ordered, func() {
375375
Expect(err).NotTo(HaveOccurred())
376376
Expect(token).NotTo(BeEmpty())
377377
378-
By("waiting for the metrics endpoint to be ready")
379-
verifyMetricsEndpointReady := func(g Gomega) {
380-
cmd := exec.Command("kubectl", "get", "endpoints", metricsServiceName, "-n", namespace)
378+
By("ensuring the controller pod is fully ready before creating test pods")
379+
verifyControllerPodReady := func(g Gomega) {
380+
cmd := exec.Command("kubectl", "get", "pod", controllerPodName, "-n", namespace,
381+
"-o", "jsonpath={.status.conditions[?(@.type=='Ready')].status}")
381382
output, err := utils.Run(cmd)
382383
g.Expect(err).NotTo(HaveOccurred())
383-
g.Expect(output).To(ContainSubstring("8443"), "Metrics endpoint is not ready")
384+
g.Expect(output).To(Equal("True"), "Controller pod not ready")
384385
}
385-
Eventually(verifyMetricsEndpointReady).Should(Succeed())
386+
Eventually(verifyControllerPodReady, 3*time.Minute, time.Second).Should(Succeed())
386387
387388
By("verifying that the controller manager is serving the metrics server")
388389
verifyMetricsServerStarted := func(g Gomega) {
389390
cmd := exec.Command("kubectl", "logs", controllerPodName, "-n", namespace)
390391
output, err := utils.Run(cmd)
391392
g.Expect(err).NotTo(HaveOccurred())
392-
g.Expect(output).To(ContainSubstring("controller-runtime.metrics\tServing metrics server"),
393+
g.Expect(output).To(ContainSubstring("Serving metrics server"),
393394
"Metrics server not yet started")
394395
}
395-
Eventually(verifyMetricsServerStarted).Should(Succeed())
396-
396+
Eventually(verifyMetricsServerStarted, 3*time.Minute, time.Second).Should(Succeed())
397+
397398
By("creating the curl-metrics pod to access the metrics endpoint")
398-
cmd = exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
399-
"--namespace", namespace,
400-
"--image=curlimages/curl:latest",
401-
"--overrides",
402-
fmt.Sprintf(` + "`" + `{
403-
"spec": {
404-
"containers": [{
405-
"name": "curl",
406-
"image": "curlimages/curl:latest",
407-
"command": ["/bin/sh", "-c"],
408-
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
409-
"securityContext": {
410-
"readOnlyRootFilesystem": true,
411-
"allowPrivilegeEscalation": false,
412-
"capabilities": {
413-
"drop": ["ALL"]
414-
},
415-
"runAsNonRoot": true,
416-
"runAsUser": 1000,
417-
"seccompProfile": {
418-
"type": "RuntimeDefault"
399+
createCurlMetricsPod := func() error {
400+
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
401+
"--namespace", namespace,
402+
"--image=curlimages/curl:latest",
403+
"--overrides",
404+
fmt.Sprintf(` + "`" + `{
405+
"spec": {
406+
"containers": [{
407+
"name": "curl",
408+
"image": "curlimages/curl:latest",
409+
"command": ["/bin/sh", "-c"],
410+
"args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
411+
"securityContext": {
412+
"readOnlyRootFilesystem": true,
413+
"allowPrivilegeEscalation": false,
414+
"capabilities": {
415+
"drop": ["ALL"]
416+
},
417+
"runAsNonRoot": true,
418+
"runAsUser": 1000,
419+
"seccompProfile": {
420+
"type": "RuntimeDefault"
421+
}
419422
}
420-
}
421-
}],
422-
"serviceAccountName": "%s"
423-
}
424-
}` + "`" + `, token, metricsServiceName, namespace, serviceAccountName))
425-
_, err = utils.Run(cmd)
426-
Expect(err).NotTo(HaveOccurred(), "Failed to create curl-metrics pod")
423+
}],
424+
"serviceAccountName": "%s"
425+
}
426+
}` + "`" + `, token, metricsServiceName, namespace, serviceAccountName))
427+
_, err := utils.Run(cmd)
428+
return err
429+
}
430+
Eventually(createCurlMetricsPod, 3*time.Minute, time.Second).Should(Succeed(), "Failed to create curl-metrics pod")
427431
428432
By("waiting for the curl-metrics pod to complete.")
429433
verifyCurlUp := func(g Gomega) {

0 commit comments

Comments
 (0)