Skip to content

Commit 2d60e80

Browse files
(kustomize/v2, go/v4): Fix ca injection for conversion webhooks
The CA injection patch has **not** worked for `go/v4` and `kustomize/v2` (release `3.5.0`) due to the need to replace `vars` with `replacements`, as `vars` are no longer supported in the latest major versions of Kustomize. However, since webhook `--conversion` was an incomplete feature until the upcoming Kubebuilder future release `v4.4.0` (where [PR #4254](#4254) is expected to be merged), users likely didn’t encounter this issue or addressed it manually by fixing the scaffold. **Note:** This change only affects projects that require a **conversion webhook**.
1 parent 0df6220 commit 2d60e80

File tree

49 files changed

+1328
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1328
-218
lines changed

.github/workflows/test-e2e-samples.yml

+11-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ jobs:
4141
run: |
4242
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
4343
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
44-
sed -i '55,182s/^#//' $KUSTOMIZATION_FILE_PATH
44+
# Uncomment all cert-manager injections
45+
sed -i '55,177s/^#//' $KUSTOMIZATION_FILE_PATH
46+
sed -i '179,203s/^#//' $KUSTOMIZATION_FILE_PATH
4547
cd testdata/project-v4/
4648
go mod tidy
4749
@@ -81,9 +83,12 @@ jobs:
8183
KUSTOMIZATION_FILE_PATH="testdata/project-v4-with-plugins/config/default/kustomization.yaml"
8284
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
8385
# Uncomment only ValidatingWebhookConfiguration
84-
# from cert-manager replaces
86+
# from cert-manager replaces; we are leaving defaulting uncommented
87+
# since this sample has no defaulting webhooks
8588
sed -i '55,121s/^#//' $KUSTOMIZATION_FILE_PATH
86-
sed -i '153,182s/^#//' $KUSTOMIZATION_FILE_PATH
89+
# Uncomment only --conversion webhooks CA injection
90+
sed -i '153,168s/^#//' $KUSTOMIZATION_FILE_PATH
91+
sed -i '170,185s/^#//' $KUSTOMIZATION_FILE_PATH
8792
cd testdata/project-v4-with-plugins/
8893
go mod tidy
8994
@@ -122,7 +127,9 @@ jobs:
122127
run: |
123128
KUSTOMIZATION_FILE_PATH="testdata/project-v4-multigroup/config/default/kustomization.yaml"
124129
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
125-
sed -i '55,182s/^#//' $KUSTOMIZATION_FILE_PATH
130+
# Uncomment all cert-manager injections
131+
sed -i '55,168s/^#//' $KUSTOMIZATION_FILE_PATH
132+
sed -i '170,185s/^#//' $KUSTOMIZATION_FILE_PATH
126133
cd testdata/project-v4-multigroup
127134
go mod tidy
128135

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

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ patches:
1010
# patches here are for enabling the conversion webhook for each CRD
1111
# +kubebuilder:scaffold:crdkustomizewebhookpatch
1212

13-
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
14-
# patches here are for enabling the CA injection for each CRD
15-
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
16-
1713
# [WEBHOOK] To enable webhook, uncomment the following section
1814
# the following config is for teaching kustomize how to do kustomization for CRDs.
1915
#configurations:

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

+4-18
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,13 @@ replacements:
156156
# version: v1
157157
# name: serving-cert # This name should match the one in certificate.yaml
158158
# fieldPath: .metadata.namespace # Namespace of the certificate CR
159-
# targets:
160-
# - select:
161-
# kind: CustomResourceDefinition
162-
# fieldPaths:
163-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
164-
# options:
165-
# delimiter: '/'
166-
# index: 0
167-
# create: true
159+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
160+
# +kubebuilder:scaffold:crdkustomizecainjectionns
168161
# - source:
169162
# kind: Certificate
170163
# group: cert-manager.io
171164
# version: v1
172165
# name: serving-cert # This name should match the one in certificate.yaml
173166
# fieldPath: .metadata.name
174-
# targets:
175-
# - select:
176-
# kind: CustomResourceDefinition
177-
# fieldPaths:
178-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
179-
# options:
180-
# delimiter: '/'
181-
# index: 1
182-
# create: true
167+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
168+
# +kubebuilder:scaffold:crdkustomizecainjectionname

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

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ patches:
1010
# patches here are for enabling the conversion webhook for each CRD
1111
# +kubebuilder:scaffold:crdkustomizewebhookpatch
1212

13-
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
14-
# patches here are for enabling the CA injection for each CRD
15-
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
16-
1713
# [WEBHOOK] To enable webhook, uncomment the following section
1814
# the following config is for teaching kustomize how to do kustomization for CRDs.
1915
#configurations:

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

+4-18
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,13 @@ patches:
156156
# version: v1
157157
# name: serving-cert # This name should match the one in certificate.yaml
158158
# fieldPath: .metadata.namespace # Namespace of the certificate CR
159-
# targets:
160-
# - select:
161-
# kind: CustomResourceDefinition
162-
# fieldPaths:
163-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
164-
# options:
165-
# delimiter: '/'
166-
# index: 0
167-
# create: true
159+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
160+
# +kubebuilder:scaffold:crdkustomizecainjectionns
168161
# - source:
169162
# kind: Certificate
170163
# group: cert-manager.io
171164
# version: v1
172165
# name: serving-cert # This name should match the one in certificate.yaml
173166
# fieldPath: .metadata.name
174-
# targets:
175-
# - select:
176-
# kind: CustomResourceDefinition
177-
# fieldPaths:
178-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
179-
# options:
180-
# delimiter: '/'
181-
# index: 1
182-
# create: true
167+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
168+
# +kubebuilder:scaffold:crdkustomizecainjectionname

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

-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ patches:
1111
- path: patches/webhook_in_cronjobs.yaml
1212
# +kubebuilder:scaffold:crdkustomizewebhookpatch
1313

14-
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
15-
# patches here are for enabling the CA injection for each CRD
16-
#- path: patches/cainjection_in_cronjobs.yaml
17-
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
18-
1914
# [WEBHOOK] To enable webhook, uncomment the following section
2015
# the following config is for teaching kustomize how to do kustomization for CRDs.
2116
configurations:

docs/book/src/multiversion-tutorial/testdata/project/config/crd/patches/cainjection_in_cronjobs.yaml

-7
This file was deleted.

docs/book/src/multiversion-tutorial/testdata/project/config/default/kustomization.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,31 @@ replacements:
156156
version: v1
157157
name: serving-cert # This name should match the one in certificate.yaml
158158
fieldPath: .metadata.namespace # Namespace of the certificate CR
159-
targets:
159+
targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
160160
- select:
161161
kind: CustomResourceDefinition
162+
name: cronjobs.batch.tutorial.kubebuilder.io
162163
fieldPaths:
163164
- .metadata.annotations.[cert-manager.io/inject-ca-from]
164165
options:
165166
delimiter: '/'
166167
index: 0
167168
create: true
169+
# +kubebuilder:scaffold:crdkustomizecainjectionns
168170
- source:
169171
kind: Certificate
170172
group: cert-manager.io
171173
version: v1
172174
name: serving-cert # This name should match the one in certificate.yaml
173175
fieldPath: .metadata.name
174-
targets:
176+
targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
175177
- select:
176178
kind: CustomResourceDefinition
179+
name: cronjobs.batch.tutorial.kubebuilder.io
177180
fieldPaths:
178181
- .metadata.annotations.[cert-manager.io/inject-ca-from]
179182
options:
180183
delimiter: '/'
181184
index: 1
182185
create: true
186+
# +kubebuilder:scaffold:crdkustomizecainjectionname

docs/book/src/reference/markers/scaffold.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,67 @@ properly registered with the manager, so that the controller can reconcile the r
103103
| `+kubebuilder:scaffold:webhook` | `webhooks suite tests` files | Marks where webhook setup functions are added. |
104104
| `+kubebuilder:scaffold:crdkustomizeresource`| `config/crd` | Marks where CRD custom resource patches are added. |
105105
| `+kubebuilder:scaffold:crdkustomizewebhookpatch` | `config/crd` | Marks where CRD webhook patches are added. |
106-
| `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhook. |
106+
| `+kubebuilder:scaffold:crdkustomizecainjectionns` | `config/default` | Marks where CA injection patches are added for the conversion webhooks. |
107+
| `+kubebuilder:scaffold:crdkustomizecainjectioname` | `config/default` | Marks where CA injection patches are added for the conversion webhooks. |
108+
| **(No longer supported)** `+kubebuilder:scaffold:crdkustomizecainjectionpatch` | `config/crd` | Marks where CA injection patches are added for the webhooks. Replaced by `+kubebuilder:scaffold:crdkustomizecainjectionns` and `+kubebuilder:scaffold:crdkustomizecainjectioname` |
107109
| `+kubebuilder:scaffold:manifestskustomizesamples` | `config/samples` | Marks where Kustomize sample manifests are injected. |
108110
| `+kubebuilder:scaffold:e2e-webhooks-checks` | `test/e2e` | Adds e2e checks for webhooks depending on the types of webhooks scaffolded. |
109111

112+
<aside class="note warning">
113+
<h1> **(No longer supported)** `+kubebuilder:scaffold:crdkustomizecainjectionpatch` </h1>
114+
115+
If you find this marker in your code please:
116+
117+
1. **Remove the CERTMANAGER Section from `config/crd/kustomization.yaml`:**
118+
119+
Delete the `CERTMANAGER` section to prevent unintended CA injection patches for CRDs. Ensure the following lines are removed or commented out:
120+
121+
```yaml
122+
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
123+
# patches here are for enabling the CA injection for each CRD
124+
#- path: patches/cainjection_in_firstmates.yaml
125+
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
126+
```
127+
128+
2. **Ensure CA Injection Configuration in `config/default/kustomization.yaml`:**
129+
130+
Under the `[CERTMANAGER]` replacement in `config/default/kustomization.yaml`, add the following code for proper CA injection generation:
131+
132+
**NOTE:** You must ensure that the code contains the following target markers:
133+
- `+kubebuilder:scaffold:crdkustomizecainjectionns`
134+
- `+kubebuilder:scaffold:crdkustomizecainjectioname`
135+
136+
```yaml
137+
# - source: # Uncomment the following block if you have a ConversionWebhook (--conversion)
138+
# kind: Certificate
139+
# group: cert-manager.io
140+
# version: v1
141+
# name: serving-cert # This name should match the one in certificate.yaml
142+
# fieldPath: .metadata.namespace # Namespace of the certificate CR
143+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
144+
# +kubebuilder:scaffold:crdkustomizecainjectionns
145+
# - source:
146+
# kind: Certificate
147+
# group: cert-manager.io
148+
# version: v1
149+
# name: serving-cert # This name should match the one in certificate.yaml
150+
# fieldPath: .metadata.name
151+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
152+
# +kubebuilder:scaffold:crdkustomizecainjectioname
153+
```
154+
155+
3. **Ensure Only Conversion Webhook Patches in `config/crd/patches`:**
156+
157+
The `config/crd/patches` directory and the corresponding entries in `config/crd/kustomization.yaml` should only contain files for conversion webhooks. Previously, a bug caused the patch file to be generated for any webhook, but only patches for webhooks scaffolded with the `--conversion` option should be included.
158+
159+
For further guidance, you can refer to examples in the `testdata/` directory in the Kubebuilder repository.
160+
161+
> **Alternatively**: You can use the [`alpha generate`](./../rescaffold.md) command to re-generate the project from scratch
162+
> using the latest release available. Afterward, you can re-add only your code implementation on top to ensure your project
163+
> includes all the latest bug fixes and enhancements.
164+
165+
</aside>
166+
110167
<aside class="note">
111168
<h1>Creating Your Own Markers</h1>
112169

hack/docs/internal/multiversion-tutorial/generate_multiversion.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ func (sp *Sample) updateDefaultKustomize() {
8787
// Enable CA for Conversion Webhook
8888
err := pluginutil.UncommentCode(
8989
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
90-
caConversionCRDDefaultKustomize, `#`)
90+
caInjectionNamespace, `#`)
91+
hackutils.CheckError("fixing default/kustomization", err)
92+
93+
// Enable CA for Conversion Webhook
94+
err = pluginutil.UncommentCode(
95+
filepath.Join(sp.ctx.Dir, "config/default/kustomization.yaml"),
96+
caInjectionCert, `#`)
9197
hackutils.CheckError("fixing default/kustomization", err)
9298
}
9399

hack/docs/internal/multiversion-tutorial/kustomize.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,34 @@ limitations under the License.
1616

1717
package multiversion
1818

19-
const caConversionCRDDefaultKustomize = `#
19+
const caInjectionNamespace = `#
2020
# - source: # Uncomment the following block if you have a ConversionWebhook (--conversion)
2121
# kind: Certificate
2222
# group: cert-manager.io
2323
# version: v1
2424
# name: serving-cert # This name should match the one in certificate.yaml
2525
# fieldPath: .metadata.namespace # Namespace of the certificate CR
26-
# targets:
26+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
2727
# - select:
2828
# kind: CustomResourceDefinition
29+
# name: cronjobs.batch.tutorial.kubebuilder.io
2930
# fieldPaths:
3031
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
3132
# options:
3233
# delimiter: '/'
3334
# index: 0
34-
# create: true
35-
# - source:
35+
# create: true`
36+
37+
const caInjectionCert = `# - source:
3638
# kind: Certificate
3739
# group: cert-manager.io
3840
# version: v1
3941
# name: serving-cert # This name should match the one in certificate.yaml
4042
# fieldPath: .metadata.name
41-
# targets:
43+
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
4244
# - select:
4345
# kind: CustomResourceDefinition
46+
# name: cronjobs.batch.tutorial.kubebuilder.io
4447
# fieldPaths:
4548
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
4649
# options:

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

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (s *apiScaffolder) Scaffold() error {
9191
}
9292
}
9393

94+
// nolint:goconst
9495
kustomizeFilePath := "config/default/kustomization.yaml"
9596
err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../crd", `#`)
9697
if err != nil {

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

+3-22
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,35 @@ func (f *Kustomization) SetTemplateDefaults() error {
4545
f.TemplateBody = fmt.Sprintf(kustomizationTemplate,
4646
machinery.NewMarkerFor(f.Path, resourceMarker),
4747
machinery.NewMarkerFor(f.Path, webhookPatchMarker),
48-
machinery.NewMarkerFor(f.Path, caInjectionPatchMarker),
4948
)
5049

5150
return nil
5251
}
5352

5453
//nolint:gosec to ignore false complain G101: Potential hardcoded credentials (gosec)
5554
const (
56-
resourceMarker = "crdkustomizeresource"
57-
webhookPatchMarker = "crdkustomizewebhookpatch"
58-
caInjectionPatchMarker = "crdkustomizecainjectionpatch"
55+
resourceMarker = "crdkustomizeresource"
56+
webhookPatchMarker = "crdkustomizewebhookpatch"
5957
)
6058

6159
// GetMarkers implements file.Inserter
6260
func (f *Kustomization) GetMarkers() []machinery.Marker {
6361
return []machinery.Marker{
6462
machinery.NewMarkerFor(f.Path, resourceMarker),
6563
machinery.NewMarkerFor(f.Path, webhookPatchMarker),
66-
machinery.NewMarkerFor(f.Path, caInjectionPatchMarker),
6764
}
6865
}
6966

7067
const (
7168
resourceCodeFragment = `- bases/%s_%s.yaml
7269
`
7370
webhookPatchCodeFragment = `- path: patches/webhook_in_%s.yaml
74-
`
75-
caInjectionPatchCodeFragment = `#- path: patches/cainjection_in_%s.yaml
7671
`
7772
)
7873

7974
// GetCodeFragments implements file.Inserter
8075
func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
81-
fragments := make(machinery.CodeFragmentsMap, 3)
76+
fragments := make(machinery.CodeFragmentsMap, 2)
8277

8378
// Generate resource code fragments
8479
res := make([]string, 0)
@@ -98,21 +93,11 @@ func (f *Kustomization) GetCodeFragments() machinery.CodeFragmentsMap {
9893
}
9994
}
10095

101-
// Generate resource code fragments
102-
caInjectionPatch := make([]string, 0)
103-
if !f.Resource.Webhooks.IsEmpty() && f.Resource.Webhooks.Conversion {
104-
caInjectionPatch = append(caInjectionPatch, fmt.Sprintf(caInjectionPatchCodeFragment, suffix))
105-
}
106-
10796
// Only store code fragments in the map if the slices are non-empty
10897
if len(res) != 0 {
10998
fragments[machinery.NewMarkerFor(f.Path, resourceMarker)] = res
11099
}
111100

112-
if len(caInjectionPatch) != 0 {
113-
fragments[machinery.NewMarkerFor(f.Path, caInjectionPatchMarker)] = caInjectionPatch
114-
}
115-
116101
return fragments
117102
}
118103

@@ -127,10 +112,6 @@ patches:
127112
# patches here are for enabling the conversion webhook for each CRD
128113
%s
129114
130-
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
131-
# patches here are for enabling the CA injection for each CRD
132-
%s
133-
134115
# [WEBHOOK] To enable webhook, uncomment the following section
135116
# the following config is for teaching kustomize how to do kustomization for CRDs.
136117
#configurations:

0 commit comments

Comments
 (0)