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