diff --git a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml index 726852ee68b..e14663ce582 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml +++ b/docs/book/src/cronjob-tutorial/testdata/project/config/crd/kustomization.yaml @@ -13,7 +13,7 @@ patches: # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD -- path: patches/cainjection_in_cronjobs.yaml +#- path: patches/cainjection_in_cronjobs.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go index f64a28ea3d5..08cf799b62a 100644 --- a/hack/docs/internal/cronjob-tutorial/generate_cronjob.go +++ b/hack/docs/internal/cronjob-tutorial/generate_cronjob.go @@ -542,22 +542,11 @@ var _ = AfterSuite(func() { func updateKustomization(sp *Sample) { var err error - // uncomment default/kustomization - err = pluginutil.UncommentCode( - filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), - `#- ../webhook`, `#`) - CheckError("fixing default/kustomization", err) - err = pluginutil.UncommentCode( filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), `#- ../certmanager`, `#`) CheckError("fixing default/kustomization", err) - err = pluginutil.UncommentCode( - filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), - `#- manager_webhook_patch.yaml`, `#`) - CheckError("fixing default/kustomization", err) - err = pluginutil.UncommentCode( filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), `#- webhookcainjection`, `#`) @@ -571,18 +560,7 @@ func updateKustomization(sp *Sample) { err = pluginutil.UncommentCode( filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"), DefaultKustomization, `#`) - CheckError("fixing default/kustomization", err) - // uncomment crd/kustomization - err = pluginutil.UncommentCode( - filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"), - `#- path: patches/webhook_in_cronjobs.yaml`, `#`) - CheckError("fixing crd/kustomization", err) - - err = pluginutil.UncommentCode( - filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"), - `#- path: patches/cainjection_in_cronjobs.yaml`, `#`) - CheckError("fixing crd/kustomization", err) } func updateExample(sp *Sample) { diff --git a/pkg/plugins/common/kustomize/v2/scaffolds/webhook.go b/pkg/plugins/common/kustomize/v2/scaffolds/webhook.go index 2dedaa52e22..6324e028c15 100644 --- a/pkg/plugins/common/kustomize/v2/scaffolds/webhook.go +++ b/pkg/plugins/common/kustomize/v2/scaffolds/webhook.go @@ -18,6 +18,7 @@ package scaffolds import ( "fmt" + pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" log "github.com/sirupsen/logrus" @@ -69,6 +70,35 @@ func (s *webhookScaffolder) Scaffold() error { return fmt.Errorf("error updating resource: %w", err) } + kustomizeFilePath := "config/default/kustomization.yaml" + err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../webhook", `#`) + if err != nil { + hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../webhook") + if !hasWebHookUncommented || err != nil { + log.Errorf("Unable to find the target #- ../webhook to uncomment in the file "+ + "%s.", kustomizeFilePath) + } + } + + err = pluginutil.UncommentCode(kustomizeFilePath, "#- manager_webhook_patch.yaml", `#`) + if err != nil { + hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- manager_webhook_patch.yaml") + if !hasWebHookUncommented || err != nil { + log.Errorf("Unable to find the target #- manager_webhook_patch.yaml to uncomment in the file "+ + "%s.", kustomizeFilePath) + } + } + + crdKustomizationsFilePath := "config/crd/kustomization.yaml" + err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#- path: patches/webhook", `#`) + if err != nil { + hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- path: patches/webhook") + if !hasWebHookUncommented || err != nil { + log.Errorf("Unable to find the target(s) #- path: patches/webhook/* to uncomment in the file "+ + "%s.", crdKustomizationsFilePath) + } + } + if err := scaffold.Execute( &kdefault.WebhookCAInjectionPatch{}, &kdefault.ManagerWebhookPatch{}, diff --git a/test/e2e/v4/generate_test.go b/test/e2e/v4/generate_test.go index d4fc718bc25..e7056394d34 100644 --- a/test/e2e/v4/generate_test.go +++ b/test/e2e/v4/generate_test.go @@ -82,25 +82,12 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+` fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))) ExpectWithOffset(1, err).NotTo(HaveOccurred()) - By("uncomment kustomization.yaml to enable webhook and ca injection") - ExpectWithOffset(1, pluginutil.UncommentCode( - filepath.Join(kbc.Dir, "config", "crd", "kustomization.yaml"), - fmt.Sprintf("#- path: patches/webhook_in_%s.yaml", kbc.Resources), "#")).To(Succeed()) - ExpectWithOffset(1, pluginutil.UncommentCode( - filepath.Join(kbc.Dir, "config", "crd", "kustomization.yaml"), - fmt.Sprintf("#- path: patches/cainjection_in_%s.yaml", kbc.Resources), "#")).To(Succeed()) - ExpectWithOffset(1, pluginutil.UncommentCode( - filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), - "#- ../webhook", "#")).To(Succeed()) ExpectWithOffset(1, pluginutil.UncommentCode( filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), "#- ../certmanager", "#")).To(Succeed()) ExpectWithOffset(1, pluginutil.UncommentCode( filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), "#- ../prometheus", "#")).To(Succeed()) - ExpectWithOffset(1, pluginutil.UncommentCode( - filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), - "#- manager_webhook_patch.yaml", "#")).To(Succeed()) ExpectWithOffset(1, pluginutil.UncommentCode( filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), "#- webhookcainjection_patch.yaml", "#")).To(Succeed()) diff --git a/testdata/project-v4-multigroup/config/crd/kustomization.yaml b/testdata/project-v4-multigroup/config/crd/kustomization.yaml index e3729c3f731..edb4eaf7c9b 100644 --- a/testdata/project-v4-multigroup/config/crd/kustomization.yaml +++ b/testdata/project-v4-multigroup/config/crd/kustomization.yaml @@ -19,12 +19,12 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- path: patches/webhook_in_crew_captains.yaml -#- path: patches/webhook_in_ship_frigates.yaml -#- path: patches/webhook_in_ship_destroyers.yaml -#- path: patches/webhook_in_ship_cruisers.yaml -#- path: patches/webhook_in_sea-creatures_krakens.yaml -#- path: patches/webhook_in_sea-creatures_leviathans.yaml +- path: patches/webhook_in_crew_captains.yaml +- path: patches/webhook_in_ship_frigates.yaml +- path: patches/webhook_in_ship_destroyers.yaml +- path: patches/webhook_in_ship_cruisers.yaml +- path: patches/webhook_in_sea-creatures_krakens.yaml +- path: patches/webhook_in_sea-creatures_leviathans.yaml #- path: patches/webhook_in_foo.policy_healthcheckpolicies.yaml #- path: patches/webhook_in_foo_bars.yaml #- path: patches/webhook_in_fiz_bars.yaml diff --git a/testdata/project-v4-multigroup/config/default/kustomization.yaml b/testdata/project-v4-multigroup/config/default/kustomization.yaml index baea92fc167..ce2920a1301 100644 --- a/testdata/project-v4-multigroup/config/default/kustomization.yaml +++ b/testdata/project-v4-multigroup/config/default/kustomization.yaml @@ -20,7 +20,7 @@ resources: - ../manager # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- ../webhook +- ../webhook # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. #- ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. @@ -36,7 +36,7 @@ patchesStrategicMerge: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- manager_webhook_patch.yaml +- manager_webhook_patch.yaml # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks. diff --git a/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml b/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml index 1385006e680..c4fb2e1b99b 100644 --- a/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml +++ b/testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml @@ -9,7 +9,7 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- path: patches/webhook_in_memcacheds.yaml +- path: patches/webhook_in_memcacheds.yaml #- path: patches/webhook_in_busyboxes.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch diff --git a/testdata/project-v4-with-deploy-image/config/default/kustomization.yaml b/testdata/project-v4-with-deploy-image/config/default/kustomization.yaml index faf2235f91f..9a8d7b34515 100644 --- a/testdata/project-v4-with-deploy-image/config/default/kustomization.yaml +++ b/testdata/project-v4-with-deploy-image/config/default/kustomization.yaml @@ -20,7 +20,7 @@ resources: - ../manager # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- ../webhook +- ../webhook # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. #- ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. @@ -36,7 +36,7 @@ patchesStrategicMerge: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- manager_webhook_patch.yaml +- manager_webhook_patch.yaml # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks. diff --git a/testdata/project-v4/config/crd/kustomization.yaml b/testdata/project-v4/config/crd/kustomization.yaml index a67f99e5ea3..55a06bf09c9 100644 --- a/testdata/project-v4/config/crd/kustomization.yaml +++ b/testdata/project-v4/config/crd/kustomization.yaml @@ -10,9 +10,9 @@ resources: patches: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. # patches here are for enabling the conversion webhook for each CRD -#- path: patches/webhook_in_captains.yaml -#- path: patches/webhook_in_firstmates.yaml -#- path: patches/webhook_in_admirales.yaml +- path: patches/webhook_in_captains.yaml +- path: patches/webhook_in_firstmates.yaml +- path: patches/webhook_in_admirales.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. diff --git a/testdata/project-v4/config/default/kustomization.yaml b/testdata/project-v4/config/default/kustomization.yaml index 1516d32747f..c96e5df1142 100644 --- a/testdata/project-v4/config/default/kustomization.yaml +++ b/testdata/project-v4/config/default/kustomization.yaml @@ -20,7 +20,7 @@ resources: - ../manager # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- ../webhook +- ../webhook # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. #- ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. @@ -36,7 +36,7 @@ patchesStrategicMerge: # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in # crd/kustomization.yaml -#- manager_webhook_patch.yaml +- manager_webhook_patch.yaml # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.