Skip to content

Commit 4e206d8

Browse files
authored
Merge pull request #3838 from camilamacedo86/fix-dist-g
🐛 Fix the CRD kustomization path logic to ensure webhook patches are generated exclusively for resources that are configured with webhooks
2 parents 0218ad7 + 8372976 commit 4e206d8

File tree

12 files changed

+2663
-27
lines changed

12 files changed

+2663
-27
lines changed

docs/book/src/component-config-tutorial/testdata/project/config/crd/kustomization.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ resources:
88
patches:
99
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1010
# patches here are for enabling the conversion webhook for each CRD
11-
#- path: patches/webhook_in_projectconfigs.yaml
1211
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1312

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

docs/book/src/getting-started/testdata/project/config/crd/kustomization.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ resources:
88
patches:
99
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1010
# patches here are for enabling the conversion webhook for each CRD
11-
#- path: patches/webhook_in_memcacheds.yaml
1211
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1312

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

pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/kustomization.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
8888
if f.MultiGroup && f.Resource.Group != "" {
8989
suffix = f.Resource.Group + "_" + f.Resource.Plural
9090
}
91-
// Generate resource code fragments
92-
webhookPatch := make([]string, 0)
93-
webhookPatch = append(webhookPatch, fmt.Sprintf(webhookPatchCodeFragment, suffix))
91+
92+
if !f.Resource.Webhooks.IsEmpty() {
93+
webhookPatch := fmt.Sprintf(webhookPatchCodeFragment, suffix)
94+
fragments[machinery.NewMarkerFor(f.Path, webhookPatchMarker)] = []string{webhookPatch}
95+
}
9496

9597
// Generate resource code fragments
9698
caInjectionPatch := make([]string, 0)
@@ -100,9 +102,7 @@ func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
100102
if len(res) != 0 {
101103
fragments[machinery.NewMarkerFor(f.Path, resourceMarker)] = res
102104
}
103-
if len(webhookPatch) != 0 {
104-
fragments[machinery.NewMarkerFor(f.Path, webhookPatchMarker)] = webhookPatch
105-
}
105+
106106
if len(caInjectionPatch) != 0 {
107107
fragments[machinery.NewMarkerFor(f.Path, caInjectionPatchMarker)] = caInjectionPatch
108108
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
log "github.com/sirupsen/logrus"
2323
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
24+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
2425
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/patches"
2526

2627
"sigs.k8s.io/kubebuilder/v3/pkg/config"
@@ -82,6 +83,7 @@ func (s *webhookScaffolder) Scaffold() error {
8283
&certmanager.KustomizeConfig{},
8384
&patches.EnableWebhookPatch{},
8485
&patches.EnableCAInjectionPatch{},
86+
&crd.Kustomization{},
8587
); err != nil {
8688
return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
8789
}

test/testdata/generate.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,7 @@ function scaffold_test_project {
116116
fi
117117

118118
make generate manifests
119-
# TODO fix the error with multigroup layout and allow it be generated
120-
# with this one.
121-
# Error: trouble configuring builtin PatchTransformer with config: `
122-
# path: patches/webhook_in_sea-creatures_krakens.yaml
123-
# `: failed to get the patch file from path(patches/webhook_in_sea-creatures_krakens.yaml): evalsymlink failure on '/Users/camiladeomacedo/go/src/sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/config/crd/patches/webhook_in_sea-creatures_krakens.yaml' : lstat go/src/sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/config/crd/patches/webhook_in_sea-creatures_krakens.yaml: no such file or directory
124-
if [[ $project =~ v4 && ! $project =~ multigroup ]]; then
119+
if [[ $project =~ v4 ]]; then
125120
make build-installer
126121
fi
127122

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ patches:
2121
- path: patches/webhook_in_ship_frigates.yaml
2222
- path: patches/webhook_in_ship_destroyers.yaml
2323
- path: patches/webhook_in_ship_cruisers.yaml
24-
- path: patches/webhook_in_sea-creatures_krakens.yaml
25-
#- path: patches/webhook_in_sea-creatures_leviathans.yaml
26-
#- path: patches/webhook_in_foo.policy_healthcheckpolicies.yaml
27-
#- path: patches/webhook_in_foo_bars.yaml
28-
#- path: patches/webhook_in_fiz_bars.yaml
29-
#- path: patches/webhook_in_lakers.yaml
24+
- path: patches/webhook_in_lakers.yaml
3025
#+kubebuilder:scaffold:crdkustomizewebhookpatch
3126

3227
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
resources:
22
- manager.yaml
3+
apiVersion: kustomize.config.k8s.io/v1beta1
4+
kind: Kustomization
5+
images:
6+
- name: controller
7+
newName: controller
8+
newTag: latest

0 commit comments

Comments
 (0)