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