@@ -63,6 +63,9 @@ var _ = Describe("kubebuilder", func() {
63
63
By ("clean up API objects created during the test" )
64
64
_ = kbc .Make ("undeploy" )
65
65
66
+ By ("clean up API objects created during the test" )
67
+ _ = kbc .Make ("uninstall" )
68
+
66
69
By ("removing controller image and working dir" )
67
70
kbc .Destroy ()
68
71
})
@@ -88,25 +91,19 @@ var _ = Describe("kubebuilder", func() {
88
91
GenerateV4WithoutMetrics (kbc )
89
92
Run (kbc , true , false , false , false , false )
90
93
})
91
- // FIXME: This test is currently disabled because it requires to be fixed:
92
- // https://github.com/kubernetes-sigs/kubebuilder/issues/4853
93
- // It is not working for k8s 1.33
94
- // It("should generate a runnable project with metrics protected by network policies", func() {
95
- // GenerateV4WithNetworkPoliciesWithoutWebhooks(kbc)
96
- // Run(kbc, false, false, false, true, true)
97
- // })
94
+ It ("should generate a runnable project with metrics protected by network policies" , func () {
95
+ GenerateV4WithNetworkPoliciesWithoutWebhooks (kbc )
96
+ Run (kbc , false , false , false , true , true )
97
+ })
98
98
It ("should generate a runnable project with webhooks and metrics protected by network policies" , func () {
99
99
GenerateV4WithNetworkPolicies (kbc )
100
100
Run (kbc , true , false , false , true , true )
101
101
})
102
- // FIXME: This test is currently disabled because it requires to be fixed:
103
- // https://github.com/kubernetes-sigs/kubebuilder/issues/4853
104
- // It is not working for k8s 1.33
105
- // It("should generate a runnable project with the manager running "+
106
- // "as restricted and without webhooks", func() {
107
- // GenerateV4WithoutWebhooks(kbc)
108
- // Run(kbc, false, false, false, true, false)
109
- // })
102
+ It ("should generate a runnable project with the manager running " +
103
+ "as restricted and without webhooks" , func () {
104
+ GenerateV4WithoutWebhooks (kbc )
105
+ Run (kbc , false , false , false , true , false )
106
+ })
110
107
})
111
108
})
112
109
@@ -453,7 +450,6 @@ func getControllerName(kbc *utils.TestContext) string {
453
450
controllerPodName = podNames [0 ]
454
451
g .Expect (controllerPodName ).Should (ContainSubstring ("controller-manager" ))
455
452
456
- // Validate pod status
457
453
status , err := kbc .Kubectl .Get (
458
454
true ,
459
455
"pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
@@ -464,9 +460,14 @@ func getControllerName(kbc *utils.TestContext) string {
464
460
return nil
465
461
}
466
462
defer func () {
467
- out , err := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
468
- Expect (err ).NotTo (HaveOccurred ())
469
- _ , _ = fmt .Fprintln (GinkgoWriter , out )
463
+ status , err := kbc .Kubectl .Get (
464
+ true ,
465
+ "pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
466
+ if status != "Running" || err != nil {
467
+ out , err := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
468
+ Expect (err ).NotTo (HaveOccurred ())
469
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
470
+ }
470
471
}()
471
472
Eventually (verifyControllerUp , 5 * time .Minute , time .Second ).Should (Succeed ())
472
473
return controllerPodName
@@ -501,6 +502,17 @@ func getMetricsOutput(kbc *utils.TestContext) string {
501
502
)
502
503
Expect (err ).NotTo (HaveOccurred (), "Controller-manager service should exist" )
503
504
505
+ By ("checking controller-manager logs to verify metrics server is up" )
506
+ controllerPodName := getControllerName (kbc )
507
+ checkLogs := func (g Gomega ) {
508
+ logs , curlLogsError := kbc .Kubectl .Logs (controllerPodName )
509
+ g .Expect (curlLogsError ).NotTo (HaveOccurred (), "failed to get controller-manager logs" )
510
+ g .Expect (logs ).To (ContainSubstring ("Serving metrics server" ),
511
+ "controller logs should show that metrics server is up" )
512
+ }
513
+ Eventually (checkLogs , 2 * time .Minute , 5 * time .Second ).Should (Succeed (),
514
+ "controller logs should show metrics server startup within 2 minutes" )
515
+
504
516
By ("ensuring the service endpoint is ready" )
505
517
checkServiceEndpoint := func (g Gomega ) {
506
518
var output string
@@ -515,30 +527,62 @@ func getMetricsOutput(kbc *utils.TestContext) string {
515
527
Eventually (checkServiceEndpoint , 2 * time .Minute , time .Second ).Should (Succeed (),
516
528
"Service endpoint should be ready" )
517
529
518
- By ("creating a curl pod to access the metrics endpoint" )
519
- cmdOpts := cmdOptsToCreateCurlPod (kbc , token )
520
- _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
521
- Expect (err ).NotTo (HaveOccurred ())
530
+ By ("checking if curl pod to access the metrics endpoint exist" )
531
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
532
+ if _ , err = kbc .Kubectl .Get (true , "pod" , podName ); err != nil && strings .Contains (err .Error (), "NotFound" ) {
533
+ By ("creating a curl pod to access the metrics endpoint" )
534
+ cmdOpts := cmdOptsToCreateCurlPod (kbc , podName , token )
535
+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
536
+ Expect (err ).NotTo (HaveOccurred ())
537
+ }
522
538
523
- By ("validating that the curl pod is running as expected" )
539
+ By (fmt . Sprintf ( "validating that the pod %s is running as expected" , podName ) )
524
540
verifyCurlUp := func (g Gomega ) {
525
541
var status string
526
542
status , err = kbc .Kubectl .Get (
527
543
true ,
528
- "pods " , "curl" , "-o" , "jsonpath={.status.phase}" )
544
+ "pod " , podName , "-o" , "jsonpath={.status.phase}" )
529
545
g .Expect (err ).NotTo (HaveOccurred ())
530
- g .Expect (status ).To (Equal ("Succeeded" ), fmt .Sprintf ("curl pod in %s status" , status ))
546
+ g .Expect (status ).To (Equal ("Succeeded" ), fmt .Sprintf ("%s pod in %s status" , podName , status ))
531
547
}
548
+ defer func () {
549
+ status , errCurl := kbc .Kubectl .Get (
550
+ true ,
551
+ "pod" , podName , "-o" , "jsonpath={.status.phase}" )
552
+ if status != "Succeeded" || errCurl != nil {
553
+ out , errDescribe := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
554
+ Expect (errDescribe ).NotTo (HaveOccurred ())
555
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
556
+
557
+ out , err = kbc .Kubectl .Command (
558
+ "pod" , podName , "-o" , "jsonpath={.status.phase}" , "--namespace" , kbc .Kubectl .Namespace )
559
+ Expect (err ).NotTo (HaveOccurred ())
560
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
561
+ }
562
+ }()
532
563
Eventually (verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
533
564
565
+ By ("validating that the correct ServiceAccount is being used" )
566
+ saName := kbc .Kubectl .ServiceAccount
567
+ currentSAOutput , err := kbc .Kubectl .Get (
568
+ true ,
569
+ "serviceaccount" , saName ,
570
+ "-o" , "jsonpath={.metadata.name}" ,
571
+ )
572
+ Expect (err ).NotTo (HaveOccurred (), "Failed to fetch the service account" )
573
+ Expect (currentSAOutput ).To (Equal (saName ), "The ServiceAccount in use does not match the expected one" )
574
+
534
575
By ("validating that the metrics endpoint is serving as expected" )
535
576
getCurlLogs := func (g Gomega ) {
536
- metricsOutput , err = kbc .Kubectl .Logs ("curl" )
577
+ metricsOutput , err = kbc .Kubectl .Logs ("pod/" + podName )
537
578
g .Expect (err ).NotTo (HaveOccurred ())
538
579
g .Expect (metricsOutput ).Should (ContainSubstring ("< HTTP/1.1 200 OK" ))
539
580
}
540
581
Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
582
+
583
+ By ("By removing pod create for the metrics" )
541
584
removeCurlPod (kbc )
585
+
542
586
return metricsOutput
543
587
}
544
588
@@ -553,46 +597,43 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
553
597
Expect (err ).NotTo (HaveOccurred ())
554
598
Expect (token ).NotTo (BeEmpty ())
555
599
556
- By ("creating a curl pod to access the metrics endpoint" )
557
- cmdOpts := cmdOptsToCreateCurlPod (kbc , token )
558
- _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
559
- Expect (err ).NotTo (HaveOccurred ())
600
+ By ("checking if curl pod to access the metrics endpoint exist" )
601
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
602
+ if _ , errCurl := kbc .Kubectl .Get (true , "pod" , podName ); errCurl != nil &&
603
+ strings .Contains (errCurl .Error (), "NotFound" ) {
604
+ By ("creating a curl pod to access the metrics endpoint" )
605
+ cmdOpts := cmdOptsToCreateCurlPod (kbc , podName , token )
606
+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
607
+ Expect (err ).NotTo (HaveOccurred ())
608
+ }
560
609
561
610
By ("validating that the curl pod fail as expected" )
562
611
verifyCurlUp := func (g Gomega ) {
563
612
status , errCurl := kbc .Kubectl .Get (
564
613
true ,
565
- "pods " , "curl" , "-o" , "jsonpath={.status.phase}" )
614
+ "pod " , podName , "-o" , "jsonpath={.status.phase}" )
566
615
g .Expect (errCurl ).NotTo (HaveOccurred ())
567
616
g .Expect (status ).NotTo (Equal ("Failed" ),
568
617
fmt .Sprintf ("curl pod in %s status when should fail with an error" , status ))
569
618
}
570
619
Eventually (verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
571
620
572
- By ("validating that the correct ServiceAccount is being used" )
573
- saName := kbc .Kubectl .ServiceAccount
574
- currentSAOutput , err := kbc .Kubectl .Get (
575
- true ,
576
- "serviceaccount" , saName ,
577
- "-o" , "jsonpath={.metadata.name}" ,
578
- )
579
- Expect (err ).NotTo (HaveOccurred (), "Failed to fetch the service account" )
580
- Expect (currentSAOutput ).To (Equal (saName ), "The ServiceAccount in use does not match the expected one" )
581
-
582
621
By ("validating that the metrics endpoint is not working as expected" )
583
622
getCurlLogs := func (g Gomega ) {
584
- metricsOutput , err := kbc .Kubectl .Logs ("curl" )
623
+ metricsOutput , err := kbc .Kubectl .Logs ("pod/" + podName )
585
624
g .Expect (err ).NotTo (HaveOccurred ())
586
625
g .Expect (metricsOutput ).Should (ContainSubstring ("Could not resolve host" ))
587
626
}
588
627
Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
628
+
629
+ By ("By removing pod create for the metrics" )
589
630
removeCurlPod (kbc )
590
631
}
591
632
592
- func cmdOptsToCreateCurlPod (kbc * utils.TestContext , token string ) []string {
633
+ func cmdOptsToCreateCurlPod (kbc * utils.TestContext , podName , token string ) []string {
593
634
//nolint:lll
594
635
cmdOpts := []string {
595
- "run" , "curl" ,
636
+ "run" , podName ,
596
637
"--restart=Never" ,
597
638
"--namespace" , kbc .Kubectl .Namespace ,
598
639
"--image=curlimages/curl:latest" ,
@@ -625,9 +666,23 @@ func cmdOptsToCreateCurlPod(kbc *utils.TestContext, token string) []string {
625
666
}
626
667
627
668
func removeCurlPod (kbc * utils.TestContext ) {
628
- By ("cleaning up the curl pod" )
629
- _ , err := kbc .Kubectl .Delete (true , "pods/curl" )
630
- Expect (err ).NotTo (HaveOccurred ())
669
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
670
+
671
+ By (fmt .Sprintf ("checking if pod %q exists before attempting cleanup" , podName ))
672
+ if _ , err := kbc .Kubectl .Get (true , "pod" , podName ); err != nil {
673
+ By (fmt .Sprintf ("pod %q does not exist, skipping cleanup" , podName ))
674
+ return
675
+ }
676
+
677
+ By ("waiting for the pod to be deleted" )
678
+ if _ , err := kbc .Kubectl .Wait (true , "pod/" + podName , "--for=delete" , "--timeout=40s" ); err != nil {
679
+ By ("force deleting the curl pod with 0s grace period" )
680
+ _ , err := kbc .Kubectl .Delete (true , "pod" , podName , "--grace-period=0" , "--force" )
681
+ Expect (err ).NotTo (HaveOccurred ())
682
+
683
+ _ , err = kbc .Kubectl .Wait (true , "pod/" + podName , "--for=delete" , "--timeout=30s" )
684
+ Expect (err ).NotTo (HaveOccurred ())
685
+ }
631
686
}
632
687
633
688
// serviceAccountToken provides a helper function that can provide you with a service account
0 commit comments