Skip to content

Commit e004665

Browse files
(ci) - fix cleanup to allow tests broken for 1.33 work
1 parent 1d073d9 commit e004665

File tree

1 file changed

+84
-37
lines changed

1 file changed

+84
-37
lines changed

test/e2e/v4/plugin_cluster_test.go

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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,20 +526,39 @@ 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+
"pods", 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+
"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+
}()
532562
Eventually(verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
533563

534564
By("validating that the metrics endpoint is serving as expected")
@@ -538,7 +568,6 @@ func getMetricsOutput(kbc *utils.TestContext) string {
538568
g.Expect(metricsOutput).Should(ContainSubstring("< HTTP/1.1 200 OK"))
539569
}
540570
Eventually(getCurlLogs, 10*time.Second, time.Second).Should(Succeed())
541-
removeCurlPod(kbc)
542571
return metricsOutput
543572
}
544573

@@ -553,10 +582,15 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
553582
Expect(err).NotTo(HaveOccurred())
554583
Expect(token).NotTo(BeEmpty())
555584

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+
}
560594

561595
By("validating that the curl pod fail as expected")
562596
verifyCurlUp := func(g Gomega) {
@@ -586,13 +620,12 @@ func metricsShouldBeUnavailable(kbc *utils.TestContext) {
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", fmt.Sprintf(podName, kbc.TestSuffix),
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

627660
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+
}
631678
}
632679

633680
// serviceAccountToken provides a helper function that can provide you with a service account

0 commit comments

Comments
 (0)