@@ -71,16 +71,17 @@ func (s *initScaffolder) Scaffold() error {
7171
7272 imagesEnvVars := s .getDeployImagesEnvVars ()
7373
74+ scaffold := machinery .NewScaffold (s .fs ,
75+ machinery .WithConfig (s .config ),
76+ )
77+
78+ // Found webhooks by looking at the config our scaffolds files
7479 mutatingWebhooks , validatingWebhooks , err := s .extractWebhooksFromGeneratedFiles ()
7580 if err != nil {
7681 return fmt .Errorf ("failed to extract webhooks: %w" , err )
7782 }
83+ hasWebhooks := hasWebhooksWith (s .config ) || (len (mutatingWebhooks ) > 0 && len (validatingWebhooks ) > 0 )
7884
79- scaffold := machinery .NewScaffold (s .fs ,
80- machinery .WithConfig (s .config ),
81- )
82-
83- hasWebhooks := len (mutatingWebhooks ) > 0 || len (validatingWebhooks ) > 0
8485 buildScaffold := []machinery.Builder {
8586 & github.HelmChartCI {},
8687 & templates.HelmChart {},
@@ -96,7 +97,7 @@ func (s *initScaffolder) Scaffold() error {
9697 DeployImages : len (imagesEnvVars ) > 0 ,
9798 HasWebhooks : hasWebhooks ,
9899 },
99- & templatescertmanager.Certificate {},
100+ & templatescertmanager.Certificate {HasWebhooks : hasWebhooks },
100101 & templatesmetrics.Service {},
101102 & prometheus.Monitor {},
102103 }
@@ -107,6 +108,11 @@ func (s *initScaffolder) Scaffold() error {
107108 MutatingWebhooks : mutatingWebhooks ,
108109 ValidatingWebhooks : validatingWebhooks ,
109110 },
111+ )
112+ }
113+
114+ if hasWebhooks {
115+ buildScaffold = append (buildScaffold ,
110116 & templateswebhooks.Service {},
111117 )
112118 }
@@ -254,7 +260,22 @@ func (s *initScaffolder) copyConfigFiles() error {
254260
255261 for _ , srcFile := range files {
256262 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 )
258279 if err != nil {
259280 return err
260281 }
@@ -266,7 +287,7 @@ func (s *initScaffolder) copyConfigFiles() error {
266287
267288// copyFileWithHelmLogic reads the source file, modifies the content for Helm, applies patches
268289// 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 {
270291 if _ , err := os .Stat (srcFile ); os .IsNotExist (err ) {
271292 log .Printf ("Source file does not exist: %s" , srcFile )
272293 return err
@@ -351,8 +372,40 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
351372 // If patch content exists, inject it under spec.conversion with Helm conditional
352373 if patchExists {
353374 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+ }
356409 }
357410
358411 // Inject annotations after "annotations:" in a single block without extra spaces
@@ -489,3 +542,19 @@ func removeLabels(content string) string {
489542
490543 return re .ReplaceAllString (content , "" )
491544}
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