@@ -57,12 +57,18 @@ var _ = Describe("kubebuilder", func() {
57
57
})
58
58
59
59
AfterEach (func () {
60
+ By ("By removing pod create for the metrics" )
61
+ removeCurlPod (kbc )
62
+
60
63
By ("By removing restricted namespace label" )
61
64
_ = kbc .RemoveNamespaceLabelToEnforceRestricted ()
62
65
63
66
By ("clean up API objects created during the test" )
64
67
_ = kbc .Make ("undeploy" )
65
68
69
+ By ("clean up API objects created during the test" )
70
+ _ = kbc .Make ("uninstall" )
71
+
66
72
By ("removing controller image and working dir" )
67
73
kbc .Destroy ()
68
74
})
@@ -88,25 +94,19 @@ var _ = Describe("kubebuilder", func() {
88
94
GenerateV4WithoutMetrics (kbc )
89
95
Run (kbc , true , false , false , false , false )
90
96
})
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
- // })
97
+ It ("should generate a runnable project with metrics protected by network policies" , func () {
98
+ GenerateV4WithNetworkPoliciesWithoutWebhooks (kbc )
99
+ Run (kbc , false , false , false , true , true )
100
+ })
98
101
It ("should generate a runnable project with webhooks and metrics protected by network policies" , func () {
99
102
GenerateV4WithNetworkPolicies (kbc )
100
103
Run (kbc , true , false , false , true , true )
101
104
})
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
- // })
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
+ })
110
110
})
111
111
})
112
112
@@ -453,7 +453,6 @@ func getControllerName(kbc *utils.TestContext) string {
453
453
controllerPodName = podNames [0 ]
454
454
g .Expect (controllerPodName ).Should (ContainSubstring ("controller-manager" ))
455
455
456
- // Validate pod status
457
456
status , err := kbc .Kubectl .Get (
458
457
true ,
459
458
"pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
@@ -464,9 +463,14 @@ func getControllerName(kbc *utils.TestContext) string {
464
463
return nil
465
464
}
466
465
defer func () {
467
- out , err := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
468
- Expect (err ).NotTo (HaveOccurred ())
469
- _ , _ = fmt .Fprintln (GinkgoWriter , out )
466
+ status , err := kbc .Kubectl .Get (
467
+ true ,
468
+ "pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
469
+ if status != "Running" || err != nil {
470
+ out , err := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
471
+ Expect (err ).NotTo (HaveOccurred ())
472
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
473
+ }
470
474
}()
471
475
Eventually (verifyControllerUp , 5 * time .Minute , time .Second ).Should (Succeed ())
472
476
return controllerPodName
@@ -501,6 +505,13 @@ func getMetricsOutput(kbc *utils.TestContext) string {
501
505
)
502
506
Expect (err ).NotTo (HaveOccurred (), "Controller-manager service should exist" )
503
507
508
+ By ("checking controller-manager logs to verify metrics server is up" )
509
+ controllerPodName := getControllerName (kbc )
510
+ logs , err := kbc .Kubectl .Logs (controllerPodName )
511
+ Expect (err ).NotTo (HaveOccurred (), "failed to get controller-manager logs" )
512
+ Expect (logs ).To (ContainSubstring ("Serving metrics server" ),
513
+ "controller logs should show that metrics server is up" )
514
+
504
515
By ("ensuring the service endpoint is ready" )
505
516
checkServiceEndpoint := func (g Gomega ) {
506
517
var output string
@@ -515,20 +526,39 @@ func getMetricsOutput(kbc *utils.TestContext) string {
515
526
Eventually (checkServiceEndpoint , 2 * time .Minute , time .Second ).Should (Succeed (),
516
527
"Service endpoint should be ready" )
517
528
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 ())
529
+ By ("checking if curl pod to access the metrics endpoint exist" )
530
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
531
+ if _ , err = kbc .Kubectl .Get (true , "pod" , podName ); err != nil && strings .Contains (err .Error (), "NotFound" ) {
532
+ By ("creating a curl pod to access the metrics endpoint" )
533
+ cmdOpts := cmdOptsToCreateCurlPod (kbc , podName , token )
534
+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
535
+ Expect (err ).NotTo (HaveOccurred ())
536
+ }
522
537
523
- By ("validating that the curl pod is running as expected" )
538
+ By (fmt . Sprintf ( "validating that the pod %s is running as expected" , podName ) )
524
539
verifyCurlUp := func (g Gomega ) {
525
540
var status string
526
541
status , err = kbc .Kubectl .Get (
527
542
true ,
528
- "pods" , "curl" , "-o" , "jsonpath={.status.phase}" )
543
+ "pods" , podName , "-o" , "jsonpath={.status.phase}" )
529
544
g .Expect (err ).NotTo (HaveOccurred ())
530
- g .Expect (status ).To (Equal ("Succeeded" ), fmt .Sprintf ("curl pod in %s status" , status ))
545
+ g .Expect (status ).To (Equal ("Succeeded" ), fmt .Sprintf ("%s pod in %s status" , podName , status ))
531
546
}
547
+ defer func () {
548
+ status , errCurl := kbc .Kubectl .Get (
549
+ true ,
550
+ "pods" , podName , "-o" , "jsonpath={.status.phase}" )
551
+ if status != "Succeeded" || errCurl != nil {
552
+ out , errDescribe := kbc .Kubectl .CommandInNamespace ("describe" , "all" )
553
+ Expect (errDescribe ).NotTo (HaveOccurred ())
554
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
555
+
556
+ out , err = kbc .Kubectl .Command (
557
+ "pods" , podName , "-o" , "jsonpath={.status.phase}" , "--namespace" , kbc .Kubectl .Namespace )
558
+ Expect (err ).NotTo (HaveOccurred ())
559
+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
560
+ }
561
+ }()
532
562
Eventually (verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
533
563
534
564
By ("validating that the metrics endpoint is serving as expected" )
@@ -538,7 +568,6 @@ func getMetricsOutput(kbc *utils.TestContext) string {
538
568
g .Expect (metricsOutput ).Should (ContainSubstring ("< HTTP/1.1 200 OK" ))
539
569
}
540
570
Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
541
- removeCurlPod (kbc )
542
571
return metricsOutput
543
572
}
544
573
@@ -553,10 +582,15 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
553
582
Expect (err ).NotTo (HaveOccurred ())
554
583
Expect (token ).NotTo (BeEmpty ())
555
584
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 ())
585
+ By ("checking if curl pod to access the metrics endpoint exist" )
586
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
587
+ if _ , errCurl := kbc .Kubectl .Get (true , "pod" , podName ); errCurl != nil &&
588
+ strings .Contains (errCurl .Error (), "NotFound" ) {
589
+ By ("creating a curl pod to access the metrics endpoint" )
590
+ cmdOpts := cmdOptsToCreateCurlPod (kbc , podName , token )
591
+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
592
+ Expect (err ).NotTo (HaveOccurred ())
593
+ }
560
594
561
595
By ("validating that the curl pod fail as expected" )
562
596
verifyCurlUp := func (g Gomega ) {
@@ -586,13 +620,12 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
586
620
g .Expect (metricsOutput ).Should (ContainSubstring ("Could not resolve host" ))
587
621
}
588
622
Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
589
- removeCurlPod (kbc )
590
623
}
591
624
592
- func cmdOptsToCreateCurlPod (kbc * utils.TestContext , token string ) []string {
625
+ func cmdOptsToCreateCurlPod (kbc * utils.TestContext , podName , token string ) []string {
593
626
//nolint:lll
594
627
cmdOpts := []string {
595
- "run" , "curl" ,
628
+ "run" , fmt . Sprintf ( podName , kbc . TestSuffix ) ,
596
629
"--restart=Never" ,
597
630
"--namespace" , kbc .Kubectl .Namespace ,
598
631
"--image=curlimages/curl:latest" ,
@@ -625,9 +658,23 @@ func cmdOptsToCreateCurlPod(kbc *utils.TestContext, token string) []string {
625
658
}
626
659
627
660
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 ())
661
+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
662
+
663
+ By (fmt .Sprintf ("checking if pod %q exists before attempting cleanup" , podName ))
664
+ if _ , err := kbc .Kubectl .Get (true , "pod" , podName ); err != nil {
665
+ By (fmt .Sprintf ("pod %q does not exist, skipping cleanup" , podName ))
666
+ return
667
+ }
668
+
669
+ By ("waiting for the pod to be deleted" )
670
+ if _ , err := kbc .Kubectl .Wait (true , "pod/" + podName , "--for=delete" , "--timeout=2m" ); err != nil {
671
+ By ("force deleting the curl pod with 0s grace period" )
672
+ _ , err := kbc .Kubectl .Delete (true , "pod" , podName , "--grace-period=0" , "--force" )
673
+ Expect (err ).NotTo (HaveOccurred ())
674
+
675
+ _ , err = kbc .Kubectl .Wait (true , "pod/" + podName , "--for=delete" , "--timeout=30s" )
676
+ Expect (err ).NotTo (HaveOccurred ())
677
+ }
631
678
}
632
679
633
680
// serviceAccountToken provides a helper function that can provide you with a service account
0 commit comments