@@ -71,16 +71,17 @@ func (s *initScaffolder) Scaffold() error {
71
71
72
72
imagesEnvVars := s .getDeployImagesEnvVars ()
73
73
74
+ scaffold := machinery .NewScaffold (s .fs ,
75
+ machinery .WithConfig (s .config ),
76
+ )
77
+
78
+ // Found webhooks by looking at the config our scaffolds files
74
79
mutatingWebhooks , validatingWebhooks , err := s .extractWebhooksFromGeneratedFiles ()
75
80
if err != nil {
76
81
return fmt .Errorf ("failed to extract webhooks: %w" , err )
77
82
}
83
+ hasWebhooks := s .config .HasWebhooks () || (len (mutatingWebhooks ) > 0 && len (validatingWebhooks ) > 0 )
78
84
79
- scaffold := machinery .NewScaffold (s .fs ,
80
- machinery .WithConfig (s .config ),
81
- )
82
-
83
- hasWebhooks := len (mutatingWebhooks ) > 0 || len (validatingWebhooks ) > 0
84
85
buildScaffold := []machinery.Builder {
85
86
& github.HelmChartCI {},
86
87
& templates.HelmChart {},
@@ -96,7 +97,7 @@ func (s *initScaffolder) Scaffold() error {
96
97
DeployImages : len (imagesEnvVars ) > 0 ,
97
98
HasWebhooks : hasWebhooks ,
98
99
},
99
- & templatescertmanager.Certificate {},
100
+ & templatescertmanager.Certificate {HasWebhooks : hasWebhooks },
100
101
& templatesmetrics.Service {},
101
102
& prometheus.Monitor {},
102
103
}
@@ -107,6 +108,11 @@ func (s *initScaffolder) Scaffold() error {
107
108
MutatingWebhooks : mutatingWebhooks ,
108
109
ValidatingWebhooks : validatingWebhooks ,
109
110
},
111
+ )
112
+ }
113
+
114
+ if s .config .HasWebhooks () {
115
+ buildScaffold = append (buildScaffold ,
110
116
& templateswebhooks.Service {},
111
117
)
112
118
}
@@ -243,7 +249,22 @@ func (s *initScaffolder) copyConfigFiles() error {
243
249
244
250
for _ , srcFile := range files {
245
251
destFile := filepath .Join (dir .DestDir , filepath .Base (srcFile ))
246
- err := copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config .GetProjectName ())
252
+
253
+ hasConvertionalWebhook := false
254
+ if s .config .HasWebhooks () {
255
+ resources , err := s .config .GetResources ()
256
+ if err != nil {
257
+ break
258
+ }
259
+ for _ , res := range resources {
260
+ if res .HasConversionWebhook () {
261
+ hasConvertionalWebhook = true
262
+ break
263
+ }
264
+ }
265
+ }
266
+
267
+ err := copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config .GetProjectName (), hasConvertionalWebhook )
247
268
if err != nil {
248
269
return err
249
270
}
@@ -255,7 +276,7 @@ func (s *initScaffolder) copyConfigFiles() error {
255
276
256
277
// copyFileWithHelmLogic reads the source file, modifies the content for Helm, applies patches
257
278
// to spec.conversion if applicable, and writes it to the destination
258
- func copyFileWithHelmLogic (srcFile , destFile , subDir , projectName string ) error {
279
+ func copyFileWithHelmLogic (srcFile , destFile , subDir , projectName string , hasConvertionalWebhook bool ) error {
259
280
if _ , err := os .Stat (srcFile ); os .IsNotExist (err ) {
260
281
log .Printf ("Source file does not exist: %s" , srcFile )
261
282
return err
@@ -340,8 +361,40 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
340
361
// If patch content exists, inject it under spec.conversion with Helm conditional
341
362
if patchExists {
342
363
conversionSpec := extractConversionSpec (patchContent )
343
- contentStr = injectConversionSpecWithCondition (contentStr , conversionSpec )
344
- hasWebhookPatch = true
364
+ // Projects scaffolded with old Kubebuilder versions does not have the conversion
365
+ // webhook properly generated because before 4.4.0 this feature was not fully addressed.
366
+ // The patch was added by default when should not. See the related fixes:
367
+ //
368
+ // Issue fixed in release 4.3.1: (which will cause the injection of webhook conditionals for projects without
369
+ // conversion webhooks)
370
+ // (kustomize/v2, go/v4): Corrected the generation of manifests under config/crd/patches
371
+ // to ensure the /convert service patch is only created for webhooks configured with --conversion. (#4280)
372
+ //
373
+ // Conversion webhook fully fixed in release 4.4.0:
374
+ // (kustomize/v2, go/v4): Fixed CA injection for conversion webhooks. Previously, the CA injection
375
+ // was applied incorrectly to all CRDs instead of only conversion types. The issue dates back to release 3.5.0
376
+ // due to kustomize/v2-alpha changes. Now, conversion webhooks are properly generated. (#4254, #4282)
377
+ if len (conversionSpec ) > 0 && ! hasConvertionalWebhook {
378
+ log .Warn ("\n " +
379
+ "============================================================\n " +
380
+ "| [WARNING] Webhook Patch Issue Detected |\n " +
381
+ "============================================================\n " +
382
+ "Webhook patch found, but no conversion webhook is configured for this project.\n \n " +
383
+ "Note: Older scaffolds have an issue where the conversion webhook patch was \n " +
384
+ " scaffolded by default, and conversion webhook injection was not properly limited \n " +
385
+ " to specific CRDs.\n \n " +
386
+ "Recommended Action:\n " +
387
+ " - Upgrade your project to the latest available version.\n " +
388
+ " - Consider using the 'alpha generate' command.\n \n " +
389
+ "The cert-manager injection and webhook conversion patch found for CRDs will\n " +
390
+ "be skipped and NOT added to the Helm chart.\n " +
391
+ "============================================================" )
392
+
393
+ hasWebhookPatch = false
394
+ } else {
395
+ contentStr = injectConversionSpecWithCondition (contentStr , conversionSpec )
396
+ hasWebhookPatch = true
397
+ }
345
398
}
346
399
347
400
// Inject annotations after "annotations:" in a single block without extra spaces
0 commit comments