@@ -25,12 +25,10 @@ import (
25
25
"strings"
26
26
"time"
27
27
28
- "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
29
-
30
28
. "github.com/onsi/ginkgo/v2"
31
-
32
29
. "github.com/onsi/gomega"
33
30
31
+ "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
34
32
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
35
33
)
36
34
@@ -43,9 +41,18 @@ var _ = Describe("kubebuilder", func() {
43
41
kbc , err = utils .NewTestContext (util .KubebuilderBinName , "GO111MODULE=on" )
44
42
Expect (err ).NotTo (HaveOccurred ())
45
43
Expect (kbc .Prepare ()).To (Succeed ())
44
+
45
+ if kbc .IsRestricted {
46
+ By ("labeling all namespaces to warn about restricted" )
47
+ err = kbc .LabelNamespacesToWarnAboutRestricted ()
48
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
49
+ }
46
50
})
47
51
48
52
AfterEach (func () {
53
+ By ("By removing restricted namespace label" )
54
+ _ = kbc .RemoveNamespaceLabelToWarnAboutRestricted ()
55
+
49
56
By ("clean up API objects created during the test" )
50
57
kbc .CleanupManifests (filepath .Join ("config" , "default" ))
51
58
@@ -85,10 +92,6 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
85
92
err = kbc .CreateManagerNamespace ()
86
93
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
87
94
88
- By ("labeling all namespaces to warn about restricted" )
89
- err = kbc .LabelAllNamespacesToWarnAboutRestricted ()
90
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
91
-
92
95
By ("updating the go.mod" )
93
96
err = kbc .Tidy ()
94
97
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -175,9 +178,6 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
175
178
ExpectWithOffset (1 , podOutput ).To (ContainSubstring ("health-probe-bind-address" ),
176
179
"Expected manager pod to have --health-probe-bind-address flag" )
177
180
178
- By ("validating the metrics endpoint" )
179
- _ = curlMetrics (kbc , hasMetrics )
180
-
181
181
if hasWebhook {
182
182
By ("validating that cert-manager has provisioned the certificate Secret" )
183
183
EventuallyWithOffset (1 , func () error {
@@ -255,13 +255,18 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
255
255
256
256
if hasMetrics {
257
257
By ("checking the metrics values to validate that the created resource object gets reconciled" )
258
- metricsOutput := curlMetrics (kbc , hasMetrics )
258
+ metricsOutput := getMetricsOutput (kbc )
259
259
ExpectWithOffset (1 , metricsOutput ).To (ContainSubstring (fmt .Sprintf (
260
260
`controller_runtime_reconcile_total{controller="%s",result="success"} 1` ,
261
261
strings .ToLower (kbc .Kind ),
262
262
)))
263
263
}
264
264
265
+ if ! hasMetrics {
266
+ By ("validating the metrics endpoint is not working" )
267
+ metricsShouldBeUnavailable (kbc )
268
+ }
269
+
265
270
if hasWebhook {
266
271
By ("validating that mutating and validating webhooks are working fine" )
267
272
cnt , err := kbc .Kubectl .Get (
@@ -273,112 +278,111 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
273
278
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
274
279
ExpectWithOffset (1 , count ).To (BeNumerically ("==" , 5 ))
275
280
}
276
-
277
281
}
278
282
279
- // curlMetrics curl's the / metrics endpoint, returning all logs once a 200 status is returned.
280
- func curlMetrics (kbc * utils.TestContext , hasMetrics bool ) string {
283
+ // getMetricsOutput return the metrics output from curl pod
284
+ func getMetricsOutput (kbc * utils.TestContext ) string {
281
285
var metricsOutput string
282
- if hasMetrics {
283
- By ("validating that the controller-manager service is available" )
284
- _ , err := kbc .Kubectl .Get (
286
+ By ("validating that the controller-manager service is available" )
287
+ _ , err := kbc .Kubectl .Get (
288
+ true ,
289
+ "service" , fmt .Sprintf ("e2e-%s-controller-manager-metrics-service" , kbc .TestSuffix ),
290
+ )
291
+ ExpectWithOffset (2 , err ).NotTo (HaveOccurred (), "Controller-manager service should exist" )
292
+
293
+ By ("ensuring the service endpoint is ready" )
294
+ eventuallyCheckServiceEndpoint := func () error {
295
+ output , err := kbc .Kubectl .Get (
285
296
true ,
286
- "service" , fmt .Sprintf ("e2e-%s-controller-manager-metrics-service" , kbc .TestSuffix ),
297
+ "endpoints" , fmt .Sprintf ("e2e-%s-controller-manager-metrics-service" , kbc .TestSuffix ),
298
+ "-o" , "jsonpath={.subsets[*].addresses[*].ip}" ,
287
299
)
288
- ExpectWithOffset (2 , err ).NotTo (HaveOccurred (), "Controller-manager service should exist" )
289
-
290
- By ("ensuring the service endpoint is ready" )
291
- eventuallyCheckServiceEndpoint := func () error {
292
- output , err := kbc .Kubectl .Get (
293
- true ,
294
- "endpoints" , fmt .Sprintf ("e2e-%s-controller-manager-metrics-service" , kbc .TestSuffix ),
295
- "-o" , "jsonpath={.subsets[*].addresses[*].ip}" ,
296
- )
297
- if err != nil {
298
- return err
299
- }
300
- if output == "" {
301
- return fmt .Errorf ("no endpoints found" )
302
- }
303
- return nil
300
+ if err != nil {
301
+ return err
304
302
}
305
- EventuallyWithOffset (2 , eventuallyCheckServiceEndpoint , 2 * time .Minute , time .Second ).Should (Succeed (),
306
- "Service endpoint should be ready" )
307
-
308
- By ("creating a curl pod to access the metrics endpoint" )
309
- // nolint:lll
310
- cmdOpts := []string {
311
- "run" , "curl" ,
312
- "--restart=Never" ,
313
- "--namespace" , kbc .Kubectl .Namespace ,
314
- "--image=curlimages/curl:7.78.0" ,
315
- "--" ,
316
- "/bin/sh" , "-c" , fmt .Sprintf ("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics" ,
317
- kbc .TestSuffix , kbc .Kubectl .Namespace ),
303
+ if output == "" {
304
+ return fmt .Errorf ("no endpoints found" )
318
305
}
319
- _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
320
- ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
306
+ return nil
307
+ }
308
+ EventuallyWithOffset (2 , eventuallyCheckServiceEndpoint , 2 * time .Minute , time .Second ).Should (Succeed (),
309
+ "Service endpoint should be ready" )
321
310
322
- By ("validating that the curl pod is running as expected" )
323
- verifyCurlUp := func () error {
324
- status , err := kbc .Kubectl .Get (
325
- true ,
326
- "pods" , "curl" , "-o" , "jsonpath={.status.phase}" )
327
- ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
328
- if status != "Succeeded" {
329
- return fmt .Errorf ("curl pod in %s status" , status )
330
- }
331
- return nil
332
- }
333
- EventuallyWithOffset (2 , verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
311
+ By ("creating a curl pod to access the metrics endpoint" )
312
+ cmdOpts := cmdOptsToCreateCurlPod (kbc )
313
+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
314
+ ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
334
315
335
- By ("validating that the metrics endpoint is serving as expected" )
336
- getCurlLogs := func () string {
337
- metricsOutput , err = kbc .Kubectl .Logs ("curl" )
338
- ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
339
- return metricsOutput
340
- }
341
- EventuallyWithOffset (2 , getCurlLogs , 10 * time .Second , time .Second ).Should (ContainSubstring ("< HTTP/1.1 200 OK" ))
342
- } else {
343
- By ("creating a curl pod to access the metrics endpoint" )
344
- // nolint:lll
345
- cmdOpts := []string {
346
- "run" , "curl" ,
347
- "--restart=Never" ,
348
- "--namespace" , kbc .Kubectl .Namespace ,
349
- "--image=curlimages/curl:7.78.0" ,
350
- "--" ,
351
- "/bin/sh" , "-c" , fmt .Sprintf ("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics" ,
352
- kbc .TestSuffix , kbc .Kubectl .Namespace ),
316
+ By ("validating that the curl pod is running as expected" )
317
+ verifyCurlUp := func () error {
318
+ status , err := kbc .Kubectl .Get (
319
+ true ,
320
+ "pods" , "curl" , "-o" , "jsonpath={.status.phase}" )
321
+ ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
322
+ if status != "Succeeded" {
323
+ return fmt .Errorf ("curl pod in %s status" , status )
353
324
}
354
- _ , err := kbc .Kubectl .CommandInNamespace (cmdOpts ... )
355
- ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
325
+ return nil
326
+ }
327
+ EventuallyWithOffset (2 , verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
356
328
357
- By ("validating that the curl pod fail as expected" )
358
- verifyCurlUp := func () error {
359
- status , err := kbc .Kubectl .Get (
360
- true ,
361
- "pods" , "curl" , "-o" , "jsonpath={.status.phase}" )
362
- ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
363
- if status != "Failed" {
364
- return fmt .Errorf (
365
- "curl pod in %s status when should fail with an error" , status )
366
- }
367
- return nil
368
- }
369
- EventuallyWithOffset (2 , verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
329
+ By ("validating that the metrics endpoint is serving as expected" )
330
+ getCurlLogs := func () string {
331
+ metricsOutput , err = kbc .Kubectl .Logs ("curl" )
332
+ ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
333
+ return metricsOutput
334
+ }
335
+ EventuallyWithOffset (2 , getCurlLogs , 10 * time .Second , time .Second ).Should (ContainSubstring ("< HTTP/1.1 200 OK" ))
336
+ removeCurlPod (kbc )
337
+ return metricsOutput
338
+ }
339
+
340
+ func metricsShouldBeUnavailable (kbc * utils.TestContext ) {
341
+ By ("creating a curl pod to access the metrics endpoint" )
342
+ cmdOpts := cmdOptsToCreateCurlPod (kbc )
343
+ _ , err := kbc .Kubectl .CommandInNamespace (cmdOpts ... )
344
+ ExpectWithOffset (2 , err ).NotTo (HaveOccurred ())
370
345
371
- By ("validating that the metrics endpoint is not working as expected" )
372
- getCurlLogs := func () string {
373
- metricsOutput , err := kbc .Kubectl .Logs ("curl" )
374
- ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
375
- return metricsOutput
346
+ By ("validating that the curl pod fail as expected" )
347
+ verifyCurlUp := func () error {
348
+ status , err := kbc .Kubectl .Get (
349
+ true ,
350
+ "pods" , "curl" , "-o" , "jsonpath={.status.phase}" )
351
+ ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
352
+ if status != "Failed" {
353
+ return fmt .Errorf (
354
+ "curl pod in %s status when should fail with an error" , status )
376
355
}
377
- EventuallyWithOffset (2 , getCurlLogs , 10 * time .Second , time .Second ).Should (ContainSubstring ("Could not resolve host" ))
356
+ return nil
357
+ }
358
+ EventuallyWithOffset (2 , verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
359
+
360
+ By ("validating that the metrics endpoint is not working as expected" )
361
+ getCurlLogs := func () string {
362
+ metricsOutput , err := kbc .Kubectl .Logs ("curl" )
363
+ ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
364
+ return metricsOutput
378
365
}
366
+ EventuallyWithOffset (2 , getCurlLogs , 10 * time .Second , time .Second ).Should (ContainSubstring ("Could not resolve host" ))
367
+ removeCurlPod (kbc )
368
+ }
369
+
370
+ func cmdOptsToCreateCurlPod (kbc * utils.TestContext ) []string {
371
+ // nolint:lll
372
+ cmdOpts := []string {
373
+ "run" , "curl" ,
374
+ "--restart=Never" ,
375
+ "--namespace" , kbc .Kubectl .Namespace ,
376
+ "--image=curlimages/curl:7.78.0" ,
377
+ "--" ,
378
+ "/bin/sh" , "-c" , fmt .Sprintf ("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics" ,
379
+ kbc .TestSuffix , kbc .Kubectl .Namespace ),
380
+ }
381
+ return cmdOpts
382
+ }
383
+
384
+ func removeCurlPod (kbc * utils.TestContext ) {
379
385
By ("cleaning up the curl pod" )
380
386
_ , err := kbc .Kubectl .Delete (true , "pods/curl" )
381
387
ExpectWithOffset (3 , err ).NotTo (HaveOccurred ())
382
-
383
- return metricsOutput
384
388
}
0 commit comments