Skip to content

Commit 45546a9

Browse files
authored
Merge pull request #3647 from camilamacedo86/add-test-without-webhook
🐛 stop to generate crd webhooks patches and cainjetions for any CRD/API and projects without webhooks
2 parents 6fcae44 + a4ad443 commit 45546a9

File tree

38 files changed

+162
-404
lines changed

38 files changed

+162
-404
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ patches:
1616
#- path: patches/cainjection_in_projectconfigs.yaml
1717
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1818

19+
# [WEBHOOK] To enable webhook, uncomment the following section
1920
# the following config is for teaching kustomize how to do kustomization for CRDs.
20-
configurations:
21-
- kustomizeconfig.yaml
21+
22+
#configurations:
23+
#- kustomizeconfig.yaml

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ patches:
1616
- path: patches/cainjection_in_cronjobs.yaml
1717
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1818

19+
# [WEBHOOK] To enable webhook, uncomment the following section
1920
# the following config is for teaching kustomize how to do kustomization for CRDs.
21+
2022
configurations:
2123
- kustomizeconfig.yaml

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ import (
2020
"fmt"
2121

2222
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
23+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
2324

2425
log "github.com/sirupsen/logrus"
25-
2626
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2727
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2828
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2929
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
30-
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
31-
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/patches"
3230
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/rbac"
3331
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/samples"
3432
)
@@ -78,8 +76,6 @@ func (s *apiScaffolder) Scaffold() error {
7876
&samples.CRDSample{Force: s.force},
7977
&rbac.CRDEditorRole{},
8078
&rbac.CRDViewerRole{},
81-
&patches.EnableWebhookPatch{},
82-
&patches.EnableCAInjectionPatch{},
8379
&crd.Kustomization{},
8480
&crd.KustomizeConfig{},
8581
); err != nil {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ patches:
125125
# patches here are for enabling the CA injection for each CRD
126126
%s
127127
128+
# [WEBHOOK] To enable webhook, uncomment the following section
128129
# the following config is for teaching kustomize how to do kustomization for CRDs.
129-
configurations:
130-
- kustomizeconfig.yaml
130+
131+
#configurations:
132+
#- kustomizeconfig.yaml
131133
`

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,15 @@ nameReference:
4848
version: v1
4949
fieldSpecs:
5050
- kind: CustomResourceDefinition
51-
version: {{ .Resource.API.CRDVersion }}
51+
version: v1
5252
group: apiextensions.k8s.io
53-
{{- if ne .Resource.API.CRDVersion "v1" }}
54-
path: spec/conversion/webhookClientConfig/service/name
55-
{{- else }}
5653
path: spec/conversion/webhook/clientConfig/service/name
57-
{{- end }}
5854
5955
namespace:
6056
- kind: CustomResourceDefinition
61-
version: {{ .Resource.API.CRDVersion }}
57+
version: v1
6258
group: apiextensions.k8s.io
63-
{{- if ne .Resource.API.CRDVersion "v1" }}
64-
path: spec/conversion/webhookClientConfig/service/namespace
65-
{{- else }}
6659
path: spec/conversion/webhook/clientConfig/service/namespace
67-
{{- end }}
6860
create: false
6961
7062
varReference:

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ func (f *EnableCAInjectionPatch) SetTemplateDefaults() error {
4949

5050
//nolint:lll
5151
const enableCAInjectionPatchTemplate = `# The following patch adds a directive for certmanager to inject CA into the CRD
52-
{{- if ne .Resource.API.CRDVersion "v1" }}
53-
# CRD conversion requires k8s 1.13 or later.
54-
{{- end }}
55-
apiVersion: apiextensions.k8s.io/{{ .Resource.API.CRDVersion }}
52+
apiVersion: apiextensions.k8s.io/v1
5653
kind: CustomResourceDefinition
5754
metadata:
5855
annotations:

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,19 @@ func (f *EnableWebhookPatch) SetTemplateDefaults() error {
4949
}
5050

5151
const enableWebhookPatchTemplate = `# The following patch enables a conversion webhook for the CRD
52-
{{- if ne .Resource.API.CRDVersion "v1" }}
53-
# CRD conversion requires k8s 1.13 or later.
54-
{{- end }}
55-
apiVersion: apiextensions.k8s.io/{{ .Resource.API.CRDVersion }}
52+
apiVersion: apiextensions.k8s.io/v1
5653
kind: CustomResourceDefinition
5754
metadata:
5855
name: {{ .Resource.Plural }}.{{ .Resource.QualifiedGroup }}
5956
spec:
6057
conversion:
6158
strategy: Webhook
62-
{{- if ne .Resource.API.CRDVersion "v1" }}
63-
webhookClientConfig:
64-
service:
65-
namespace: system
66-
name: webhook-service
67-
path: /convert
68-
{{- else }}
6959
webhook:
7060
clientConfig:
7161
service:
7262
namespace: system
7363
name: webhook-service
7464
path: /convert
7565
conversionReviewVersions:
76-
- {{ .Resource.API.CRDVersion }}
77-
{{- end }}
66+
- v1
7867
`

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package scaffolds
1919
import (
2020
"fmt"
2121

22-
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
23-
2422
log "github.com/sirupsen/logrus"
23+
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/patches"
2525

2626
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2727
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
@@ -71,6 +71,21 @@ func (s *webhookScaffolder) Scaffold() error {
7171
return fmt.Errorf("error updating resource: %w", err)
7272
}
7373

74+
if err := scaffold.Execute(
75+
&kdefault.WebhookCAInjectionPatch{},
76+
&kdefault.ManagerWebhookPatch{},
77+
&webhook.Kustomization{Force: s.force},
78+
&webhook.KustomizeConfig{},
79+
&webhook.Service{},
80+
&certmanager.Certificate{},
81+
&certmanager.Kustomization{},
82+
&certmanager.KustomizeConfig{},
83+
&patches.EnableWebhookPatch{},
84+
&patches.EnableCAInjectionPatch{},
85+
); err != nil {
86+
return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
87+
}
88+
7489
kustomizeFilePath := "config/default/kustomization.yaml"
7590
err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../webhook", `#`)
7691
if err != nil {
@@ -100,17 +115,13 @@ func (s *webhookScaffolder) Scaffold() error {
100115
}
101116
}
102117

103-
if err := scaffold.Execute(
104-
&kdefault.WebhookCAInjectionPatch{},
105-
&kdefault.ManagerWebhookPatch{},
106-
&webhook.Kustomization{Force: s.force},
107-
&webhook.KustomizeConfig{},
108-
&webhook.Service{},
109-
&certmanager.Certificate{},
110-
&certmanager.Kustomization{},
111-
&certmanager.KustomizeConfig{},
112-
); err != nil {
113-
return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
118+
err = pluginutil.UncommentCode(crdKustomizationsFilePath, "#configurations:\n#- kustomizeconfig.yaml", `#`)
119+
if err != nil {
120+
hasWebHookUncommented, err := pluginutil.HasFragment(crdKustomizationsFilePath, "- kustomizeconfig.yaml")
121+
if !hasWebHookUncommented || err != nil {
122+
log.Errorf("Unable to find the target(s) #configurations:\n#- kustomizeconfig.yaml to uncomment in the file "+
123+
"%s.", crdKustomizationsFilePath)
124+
}
114125
}
115126

116127
return nil

0 commit comments

Comments
 (0)