@@ -111,6 +111,7 @@ func (sp *Sample) UpdateTutorial() {
111111 sp .updateConversionFiles ()
112112 sp .updateSampleV2 ()
113113 sp .updateMain ()
114+ sp .updateE2EWebhookConversion ()
114115}
115116
116117func (sp * Sample ) updateCronjobV1DueForce () {
@@ -790,3 +791,74 @@ func (sp *Sample) CodeGen() {
790791 err = sp .ctx .EditHelmPlugin ()
791792 hackutils .CheckError ("Failed to enable helm plugin" , err )
792793}
794+
795+ const webhookConversionE2ETest = `
796+ It("Should successfully convert between v1 and v2 versions", func() {
797+ By("Creating a v1 CronJob with sample data")
798+ cmd := exec.Command("kubectl", "apply", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace)
799+ _, err := utils.Run(cmd)
800+ Expect(err).NotTo(HaveOccurred(), "Failed to create v1 CronJob")
801+
802+ By("Verifying the v1 CronJob was created")
803+ cmd = exec.Command("kubectl", "get", "cronjobs.v1.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
804+ v1Name, err := utils.Run(cmd)
805+ Expect(err).NotTo(HaveOccurred(), "Failed to get v1 CronJob")
806+ Expect(strings.TrimSpace(v1Name)).NotTo(BeEmpty(), "v1 CronJob name should not be empty")
807+
808+ By("Creating a v2 CronJob with sample data")
809+ cmd = exec.Command("kubectl", "apply", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace)
810+ _, err = utils.Run(cmd)
811+ Expect(err).NotTo(HaveOccurred(), "Failed to create v2 CronJob")
812+
813+ By("Verifying the v2 CronJob was created")
814+ cmd = exec.Command("kubectl", "get", "cronjobs.v2.batch.tutorial.kubebuilder.io", "-n", namespace, "-o", "jsonpath={.items[0].metadata.name}")
815+ v2Name, err := utils.Run(cmd)
816+ Expect(err).NotTo(HaveOccurred(), "Failed to get v2 CronJob")
817+ Expect(strings.TrimSpace(v2Name)).NotTo(BeEmpty(), "v2 CronJob name should not be empty")
818+
819+ By("Verifying conversion webhook is active by checking controller logs")
820+ cmd = exec.Command("kubectl", "logs", "-l", "control-plane=controller-manager", "-n", namespace, "--tail=50")
821+ logs, err := utils.Run(cmd)
822+ Expect(err).NotTo(HaveOccurred(), "Failed to get controller logs")
823+ Expect(logs).To(ContainSubstring("cronjob"), "Controller logs should contain cronjob references")
824+ })`
825+
826+ func (sp * Sample ) updateE2EWebhookConversion () {
827+ cronjobE2ETest := filepath .Join (sp .ctx .Dir , "test" , "e2e" , "e2e_test.go" )
828+
829+ // Add strings import if not already present
830+ err := pluginutil .InsertCodeIfNotExist (cronjobE2ETest ,
831+ ` "os/exec"
832+ "path/filepath"
833+ "time"` ,
834+ `
835+ "strings"` )
836+ hackutils .CheckError ("adding strings import for e2e test" , err )
837+
838+ // Add CronJob cleanup to the AfterEach block
839+ err = pluginutil .InsertCode (cronjobE2ETest ,
840+ ` // After each test, check for failures and collect logs, events,
841+ // and pod descriptions for debugging.
842+ AfterEach(func() {` ,
843+ ` By("Cleaning up test CronJob resources")
844+ cmd := exec.Command("kubectl", "delete", "-f", "config/samples/batch_v1_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
845+ _, _ = utils.Run(cmd)
846+ cmd = exec.Command("kubectl", "delete", "-f", "config/samples/batch_v2_cronjob.yaml", "-n", namespace, "--ignore-not-found=true")
847+ _, _ = utils.Run(cmd)
848+
849+ ` )
850+ hackutils .CheckError ("adding CronJob cleanup to AfterEach" , err )
851+
852+ // Add webhook conversion test after the existing TODO comment
853+ err = pluginutil .InsertCode (cronjobE2ETest ,
854+ ` // TODO: Customize the e2e test suite with scenarios specific to your project.
855+ // Consider applying sample/CR(s) and check their status and/or verifying
856+ // the reconciliation by using the metrics, i.e.:
857+ // metricsOutput := getMetricsOutput()
858+ // Expect(metricsOutput).To(ContainSubstring(
859+ // fmt.Sprintf(` + "`" + `controller_runtime_reconcile_total{controller="%s",result="success"} 1` + "`" + `,
860+ // strings.ToLower(<Kind>),
861+ // ))` ,
862+ webhookConversionE2ETest )
863+ hackutils .CheckError ("adding webhook conversion e2e test" , err )
864+ }
0 commit comments