Skip to content

Commit

Permalink
fix: uncomment kustomize files when webhooks are generate
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Sep 17, 2023
1 parent e769e76 commit f3233cf
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 0 additions & 22 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`, `#`)
Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions pkg/plugins/common/kustomize/v2/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scaffolds

import (
"fmt"
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -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{},
Expand Down
13 changes: 0 additions & 13 deletions test/e2e/v4/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions testdata/project-v4-multigroup/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v4/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v4/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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.
Expand Down

0 comments on commit f3233cf

Please sign in to comment.