Skip to content

Commit a910425

Browse files
fix: uncomment kustomize files when webhooks are generate
1 parent e769e76 commit a910425

File tree

9 files changed

+46
-46
lines changed

9 files changed

+46
-46
lines changed

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -542,22 +542,11 @@ var _ = AfterSuite(func() {
542542

543543
func updateKustomization(sp *Sample) {
544544
var err error
545-
// uncomment default/kustomization
546-
err = pluginutil.UncommentCode(
547-
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
548-
`#- ../webhook`, `#`)
549-
CheckError("fixing default/kustomization", err)
550-
551545
err = pluginutil.UncommentCode(
552546
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
553547
`#- ../certmanager`, `#`)
554548
CheckError("fixing default/kustomization", err)
555549

556-
err = pluginutil.UncommentCode(
557-
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
558-
`#- manager_webhook_patch.yaml`, `#`)
559-
CheckError("fixing default/kustomization", err)
560-
561550
err = pluginutil.UncommentCode(
562551
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
563552
`#- webhookcainjection`, `#`)
@@ -573,12 +562,6 @@ func updateKustomization(sp *Sample) {
573562
DefaultKustomization, `#`)
574563
CheckError("fixing default/kustomization", err)
575564

576-
// uncomment crd/kustomization
577-
err = pluginutil.UncommentCode(
578-
filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"),
579-
`#- path: patches/webhook_in_cronjobs.yaml`, `#`)
580-
CheckError("fixing crd/kustomization", err)
581-
582565
err = pluginutil.UncommentCode(
583566
filepath.Join(sp.ctx.Dir, "config/crd/kustomization.yaml"),
584567
`#- path: patches/cainjection_in_cronjobs.yaml`, `#`)

pkg/plugins/common/kustomize/v2/scaffolds/webhook.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scaffolds
1818

1919
import (
2020
"fmt"
21+
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
2122

2223
log "github.com/sirupsen/logrus"
2324

@@ -69,6 +70,35 @@ func (s *webhookScaffolder) Scaffold() error {
6970
return fmt.Errorf("error updating resource: %w", err)
7071
}
7172

73+
kustomizeFilePath := "config/default/kustomization.yaml"
74+
err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../webhook", `#`)
75+
if err != nil {
76+
hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../webhook")
77+
if !hasWebHookUncommented || err != nil {
78+
log.Errorf("Unable to find the target #- ../webhook to uncomment in the file "+
79+
"%s.", kustomizeFilePath)
80+
}
81+
}
82+
83+
err = pluginutil.UncommentCode(kustomizeFilePath, "#- manager_webhook_patch.yaml", `#`)
84+
if err != nil {
85+
hasWebHookUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- manager_webhook_patch.yaml")
86+
if !hasWebHookUncommented || err != nil {
87+
log.Errorf("Unable to find the target #- manager_webhook_patch.yaml to uncomment in the file "+
88+
"%s.", kustomizeFilePath)
89+
}
90+
}
91+
92+
crdKustomizationsFilePath := "config/crd/kustomization.yaml"
93+
err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#- path: patches/webhook", `#`)
94+
if err != nil {
95+
hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- path: patches/webhook")
96+
if !hasWebHookUncommented || err != nil {
97+
log.Errorf("Unable to find the target(s) #- path: patches/webhook/* to uncomment in the file "+
98+
"%s.", crdKustomizationsFilePath)
99+
}
100+
}
101+
72102
if err := scaffold.Execute(
73103
&kdefault.WebhookCAInjectionPatch{},
74104
&kdefault.ManagerWebhookPatch{},

test/e2e/v4/generate_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,12 @@ Count int `+"`"+`json:"count,omitempty"`+"`"+`
8282
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
8383
ExpectWithOffset(1, err).NotTo(HaveOccurred())
8484

85-
By("uncomment kustomization.yaml to enable webhook and ca injection")
86-
ExpectWithOffset(1, pluginutil.UncommentCode(
87-
filepath.Join(kbc.Dir, "config", "crd", "kustomization.yaml"),
88-
fmt.Sprintf("#- path: patches/webhook_in_%s.yaml", kbc.Resources), "#")).To(Succeed())
89-
ExpectWithOffset(1, pluginutil.UncommentCode(
90-
filepath.Join(kbc.Dir, "config", "crd", "kustomization.yaml"),
91-
fmt.Sprintf("#- path: patches/cainjection_in_%s.yaml", kbc.Resources), "#")).To(Succeed())
92-
ExpectWithOffset(1, pluginutil.UncommentCode(
93-
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
94-
"#- ../webhook", "#")).To(Succeed())
9585
ExpectWithOffset(1, pluginutil.UncommentCode(
9686
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
9787
"#- ../certmanager", "#")).To(Succeed())
9888
ExpectWithOffset(1, pluginutil.UncommentCode(
9989
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
10090
"#- ../prometheus", "#")).To(Succeed())
101-
ExpectWithOffset(1, pluginutil.UncommentCode(
102-
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
103-
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
10491
ExpectWithOffset(1, pluginutil.UncommentCode(
10592
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
10693
"#- webhookcainjection_patch.yaml", "#")).To(Succeed())

testdata/project-v4-multigroup/config/crd/kustomization.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ resources:
1919
patches:
2020
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
2121
# patches here are for enabling the conversion webhook for each CRD
22-
#- path: patches/webhook_in_crew_captains.yaml
23-
#- path: patches/webhook_in_ship_frigates.yaml
24-
#- path: patches/webhook_in_ship_destroyers.yaml
25-
#- path: patches/webhook_in_ship_cruisers.yaml
26-
#- path: patches/webhook_in_sea-creatures_krakens.yaml
27-
#- path: patches/webhook_in_sea-creatures_leviathans.yaml
22+
- path: patches/webhook_in_crew_captains.yaml
23+
- path: patches/webhook_in_ship_frigates.yaml
24+
- path: patches/webhook_in_ship_destroyers.yaml
25+
- path: patches/webhook_in_ship_cruisers.yaml
26+
- path: patches/webhook_in_sea-creatures_krakens.yaml
27+
- path: patches/webhook_in_sea-creatures_leviathans.yaml
2828
#- path: patches/webhook_in_foo.policy_healthcheckpolicies.yaml
2929
#- path: patches/webhook_in_foo_bars.yaml
3030
#- path: patches/webhook_in_fiz_bars.yaml

testdata/project-v4-multigroup/config/default/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resources:
2020
- ../manager
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
23-
#- ../webhook
23+
- ../webhook
2424
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
2525
#- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
@@ -36,7 +36,7 @@ patchesStrategicMerge:
3636

3737
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3838
# crd/kustomization.yaml
39-
#- manager_webhook_patch.yaml
39+
- manager_webhook_patch.yaml
4040

4141
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
4242
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.

testdata/project-v4-with-deploy-image/config/crd/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resources:
99
patches:
1010
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1111
# patches here are for enabling the conversion webhook for each CRD
12-
#- path: patches/webhook_in_memcacheds.yaml
12+
- path: patches/webhook_in_memcacheds.yaml
1313
#- path: patches/webhook_in_busyboxes.yaml
1414
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1515

testdata/project-v4-with-deploy-image/config/default/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resources:
2020
- ../manager
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
23-
#- ../webhook
23+
- ../webhook
2424
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
2525
#- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
@@ -36,7 +36,7 @@ patchesStrategicMerge:
3636

3737
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3838
# crd/kustomization.yaml
39-
#- manager_webhook_patch.yaml
39+
- manager_webhook_patch.yaml
4040

4141
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
4242
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.

testdata/project-v4/config/crd/kustomization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ resources:
1010
patches:
1111
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1212
# patches here are for enabling the conversion webhook for each CRD
13-
#- path: patches/webhook_in_captains.yaml
14-
#- path: patches/webhook_in_firstmates.yaml
15-
#- path: patches/webhook_in_admirales.yaml
13+
- path: patches/webhook_in_captains.yaml
14+
- path: patches/webhook_in_firstmates.yaml
15+
- path: patches/webhook_in_admirales.yaml
1616
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1717

1818
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.

testdata/project-v4/config/default/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resources:
2020
- ../manager
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
23-
#- ../webhook
23+
- ../webhook
2424
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
2525
#- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
@@ -36,7 +36,7 @@ patchesStrategicMerge:
3636

3737
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3838
# crd/kustomization.yaml
39-
#- manager_webhook_patch.yaml
39+
- manager_webhook_patch.yaml
4040

4141
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
4242
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.

0 commit comments

Comments
 (0)