@@ -57,12 +57,18 @@ var _ = Describe("kubebuilder", func() {
5757 })
5858
5959 AfterEach (func () {
60+ By ("By removing pod create for the metrics" )
61+ removeCurlPod (kbc )
62+
6063 By ("By removing restricted namespace label" )
6164 _ = kbc .RemoveNamespaceLabelToEnforceRestricted ()
6265
6366 By ("clean up API objects created during the test" )
6467 _ = kbc .Make ("undeploy" )
6568
69+ By ("clean up API objects created during the test" )
70+ _ = kbc .Make ("uninstall" )
71+
6672 By ("removing controller image and working dir" )
6773 kbc .Destroy ()
6874 })
@@ -88,25 +94,19 @@ var _ = Describe("kubebuilder", func() {
8894 GenerateV4WithoutMetrics (kbc )
8995 Run (kbc , true , false , false , false , false )
9096 })
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+ })
98101 It ("should generate a runnable project with webhooks and metrics protected by network policies" , func () {
99102 GenerateV4WithNetworkPolicies (kbc )
100103 Run (kbc , true , false , false , true , true )
101104 })
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+ })
110110 })
111111})
112112
@@ -453,7 +453,6 @@ func getControllerName(kbc *utils.TestContext) string {
453453 controllerPodName = podNames [0 ]
454454 g .Expect (controllerPodName ).Should (ContainSubstring ("controller-manager" ))
455455
456- // Validate pod status
457456 status , err := kbc .Kubectl .Get (
458457 true ,
459458 "pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" )
@@ -464,9 +463,14 @@ func getControllerName(kbc *utils.TestContext) string {
464463 return nil
465464 }
466465 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+ }
470474 }()
471475 Eventually (verifyControllerUp , 5 * time .Minute , time .Second ).Should (Succeed ())
472476 return controllerPodName
@@ -501,6 +505,13 @@ func getMetricsOutput(kbc *utils.TestContext) string {
501505 )
502506 Expect (err ).NotTo (HaveOccurred (), "Controller-manager service should exist" )
503507
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+
504515 By ("ensuring the service endpoint is ready" )
505516 checkServiceEndpoint := func (g Gomega ) {
506517 var output string
@@ -515,30 +526,58 @@ func getMetricsOutput(kbc *utils.TestContext) string {
515526 Eventually (checkServiceEndpoint , 2 * time .Minute , time .Second ).Should (Succeed (),
516527 "Service endpoint should be ready" )
517528
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+ }
522537
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 ) )
524539 verifyCurlUp := func (g Gomega ) {
525540 var status string
526541 status , err = kbc .Kubectl .Get (
527542 true ,
528- "pods " , "curl" , "-o" , "jsonpath={.status.phase}" )
543+ "pod " , podName , "-o" , "jsonpath={.status.phase}" )
529544 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 ))
531546 }
547+ defer func () {
548+ status , errCurl := kbc .Kubectl .Get (
549+ true ,
550+ "pod" , 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+ "pod" , podName , "-o" , "jsonpath={.status.phase}" , "--namespace" , kbc .Kubectl .Namespace )
558+ Expect (err ).NotTo (HaveOccurred ())
559+ _ , _ = fmt .Fprintln (GinkgoWriter , out )
560+ }
561+ }()
532562 Eventually (verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
533563
564+ By ("validating that the correct ServiceAccount is being used" )
565+ saName := kbc .Kubectl .ServiceAccount
566+ currentSAOutput , err := kbc .Kubectl .Get (
567+ true ,
568+ "serviceaccount" , saName ,
569+ "-o" , "jsonpath={.metadata.name}" ,
570+ )
571+ Expect (err ).NotTo (HaveOccurred (), "Failed to fetch the service account" )
572+ Expect (currentSAOutput ).To (Equal (saName ), "The ServiceAccount in use does not match the expected one" )
573+
534574 By ("validating that the metrics endpoint is serving as expected" )
535575 getCurlLogs := func (g Gomega ) {
536- metricsOutput , err = kbc .Kubectl .Logs ("curl" )
576+ metricsOutput , err = kbc .Kubectl .Logs ("pod" , podName )
537577 g .Expect (err ).NotTo (HaveOccurred ())
538578 g .Expect (metricsOutput ).Should (ContainSubstring ("< HTTP/1.1 200 OK" ))
539579 }
540580 Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
541- removeCurlPod (kbc )
542581 return metricsOutput
543582}
544583
@@ -553,46 +592,40 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
553592 Expect (err ).NotTo (HaveOccurred ())
554593 Expect (token ).NotTo (BeEmpty ())
555594
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 ())
595+ By ("checking if curl pod to access the metrics endpoint exist" )
596+ podName := fmt .Sprintf ("%s-curl" , kbc .TestSuffix )
597+ if _ , errCurl := kbc .Kubectl .Get (true , "pod" , podName ); errCurl != nil &&
598+ strings .Contains (errCurl .Error (), "NotFound" ) {
599+ By ("creating a curl pod to access the metrics endpoint" )
600+ cmdOpts := cmdOptsToCreateCurlPod (kbc , podName , token )
601+ _ , err = kbc .Kubectl .CommandInNamespace (cmdOpts ... )
602+ Expect (err ).NotTo (HaveOccurred ())
603+ }
560604
561605 By ("validating that the curl pod fail as expected" )
562606 verifyCurlUp := func (g Gomega ) {
563607 status , errCurl := kbc .Kubectl .Get (
564608 true ,
565- "pods " , "curl" , "-o" , "jsonpath={.status.phase}" )
609+ "pod " , podName , "-o" , "jsonpath={.status.phase}" )
566610 g .Expect (errCurl ).NotTo (HaveOccurred ())
567611 g .Expect (status ).NotTo (Equal ("Failed" ),
568612 fmt .Sprintf ("curl pod in %s status when should fail with an error" , status ))
569613 }
570614 Eventually (verifyCurlUp , 240 * time .Second , time .Second ).Should (Succeed ())
571615
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-
582616 By ("validating that the metrics endpoint is not working as expected" )
583617 getCurlLogs := func (g Gomega ) {
584- metricsOutput , err := kbc .Kubectl .Logs ("curl" )
618+ metricsOutput , err := kbc .Kubectl .Logs ("pod" , podName )
585619 g .Expect (err ).NotTo (HaveOccurred ())
586620 g .Expect (metricsOutput ).Should (ContainSubstring ("Could not resolve host" ))
587621 }
588622 Eventually (getCurlLogs , 10 * time .Second , time .Second ).Should (Succeed ())
589- removeCurlPod (kbc )
590623}
591624
592- func cmdOptsToCreateCurlPod (kbc * utils.TestContext , token string ) []string {
625+ func cmdOptsToCreateCurlPod (kbc * utils.TestContext , podName , token string ) []string {
593626 //nolint:lll
594627 cmdOpts := []string {
595- "run" , "curl" ,
628+ "run" , podName ,
596629 "--restart=Never" ,
597630 "--namespace" , kbc .Kubectl .Namespace ,
598631 "--image=curlimages/curl:latest" ,
@@ -625,9 +658,23 @@ func cmdOptsToCreateCurlPod(kbc *utils.TestContext, token string) []string {
625658}
626659
627660func 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+ }
631678}
632679
633680// serviceAccountToken provides a helper function that can provide you with a service account
0 commit comments