@@ -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 := hasWebhooksWith (s .config ) || (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 hasWebhooks {
115
+ buildScaffold = append (buildScaffold ,
110
116
& templateswebhooks.Service {},
111
117
)
112
118
}
@@ -255,7 +261,22 @@ func (s *initScaffolder) copyConfigFiles() error {
255
261
256
262
for _ , srcFile := range files {
257
263
destFile := filepath .Join (dir .DestDir , filepath .Base (srcFile ))
258
- err := copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config .GetProjectName ())
264
+
265
+ hasConvertionalWebhook := false
266
+ if hasWebhooksWith (s .config ) {
267
+ resources , err := s .config .GetResources ()
268
+ if err != nil {
269
+ break
270
+ }
271
+ for _ , res := range resources {
272
+ if res .HasConversionWebhook () {
273
+ hasConvertionalWebhook = true
274
+ break
275
+ }
276
+ }
277
+ }
278
+
279
+ err := copyFileWithHelmLogic (srcFile , destFile , dir .SubDir , s .config .GetProjectName (), hasConvertionalWebhook )
259
280
if err != nil {
260
281
return err
261
282
}
@@ -267,7 +288,7 @@ func (s *initScaffolder) copyConfigFiles() error {
267
288
268
289
// copyFileWithHelmLogic reads the source file, modifies the content for Helm, applies patches
269
290
// to spec.conversion if applicable, and writes it to the destination
270
- func copyFileWithHelmLogic (srcFile , destFile , subDir , projectName string ) error {
291
+ func copyFileWithHelmLogic (srcFile , destFile , subDir , projectName string , hasConvertionalWebhook bool ) error {
271
292
if _ , err := os .Stat (srcFile ); os .IsNotExist (err ) {
272
293
log .Printf ("Source file does not exist: %s" , srcFile )
273
294
return err
@@ -352,8 +373,40 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
352
373
// If patch content exists, inject it under spec.conversion with Helm conditional
353
374
if patchExists {
354
375
conversionSpec := extractConversionSpec (patchContent )
355
- contentStr = injectConversionSpecWithCondition (contentStr , conversionSpec )
356
- hasWebhookPatch = true
376
+ // Projects scaffolded with old Kubebuilder versions does not have the conversion
377
+ // webhook properly generated because before 4.4.0 this feature was not fully addressed.
378
+ // The patch was added by default when should not. See the related fixes:
379
+ //
380
+ // Issue fixed in release 4.3.1: (which will cause the injection of webhook conditionals for projects without
381
+ // conversion webhooks)
382
+ // (kustomize/v2, go/v4): Corrected the generation of manifests under config/crd/patches
383
+ // to ensure the /convert service patch is only created for webhooks configured with --conversion. (#4280)
384
+ //
385
+ // Conversion webhook fully fixed in release 4.4.0:
386
+ // (kustomize/v2, go/v4): Fixed CA injection for conversion webhooks. Previously, the CA injection
387
+ // was applied incorrectly to all CRDs instead of only conversion types. The issue dates back to release 3.5.0
388
+ // due to kustomize/v2-alpha changes. Now, conversion webhooks are properly generated. (#4254, #4282)
389
+ if len (conversionSpec ) > 0 && ! hasConvertionalWebhook {
390
+ log .Warn ("\n " +
391
+ "============================================================\n " +
392
+ "| [WARNING] Webhook Patch Issue Detected |\n " +
393
+ "============================================================\n " +
394
+ "Webhook patch found, but no conversion webhook is configured for this project.\n \n " +
395
+ "Note: Older scaffolds have an issue where the conversion webhook patch was \n " +
396
+ " scaffolded by default, and conversion webhook injection was not properly limited \n " +
397
+ " to specific CRDs.\n \n " +
398
+ "Recommended Action:\n " +
399
+ " - Upgrade your project to the latest available version.\n " +
400
+ " - Consider using the 'alpha generate' command.\n \n " +
401
+ "The cert-manager injection and webhook conversion patch found for CRDs will\n " +
402
+ "be skipped and NOT added to the Helm chart.\n " +
403
+ "============================================================" )
404
+
405
+ hasWebhookPatch = false
406
+ } else {
407
+ contentStr = injectConversionSpecWithCondition (contentStr , conversionSpec )
408
+ hasWebhookPatch = true
409
+ }
357
410
}
358
411
359
412
// Inject annotations after "annotations:" in a single block without extra spaces
@@ -490,3 +543,19 @@ func removeLabels(content string) string {
490
543
491
544
return re .ReplaceAllString (content , "" )
492
545
}
546
+
547
+ func hasWebhooksWith (c config.Config ) bool {
548
+ // Get the list of resources
549
+ resources , err := c .GetResources ()
550
+ if err != nil {
551
+ return false // If there's an error getting resources, assume no webhooks
552
+ }
553
+
554
+ for _ , res := range resources {
555
+ if res .HasDefaultingWebhook () || res .HasValidationWebhook () || res .HasConversionWebhook () {
556
+ return true
557
+ }
558
+ }
559
+
560
+ return false
561
+ }
0 commit comments