@@ -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 }
@@ -255,7 +261,22 @@ func (s *initScaffolder) copyConfigFiles() error {
255261
256262 for _ , srcFile := range files {
257263 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 )
259280 if err != nil {
260281 return err
261282 }
@@ -267,7 +288,7 @@ func (s *initScaffolder) copyConfigFiles() error {
267288
268289// copyFileWithHelmLogic reads the source file, modifies the content for Helm, applies patches
269290// 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 {
271292 if _ , err := os .Stat (srcFile ); os .IsNotExist (err ) {
272293 log .Printf ("Source file does not exist: %s" , srcFile )
273294 return err
@@ -352,8 +373,40 @@ func copyFileWithHelmLogic(srcFile, destFile, subDir, projectName string) error
352373 // If patch content exists, inject it under spec.conversion with Helm conditional
353374 if patchExists {
354375 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+ }
357410 }
358411
359412 // Inject annotations after "annotations:" in a single block without extra spaces
@@ -490,3 +543,19 @@ func removeLabels(content string) string {
490543
491544 return re .ReplaceAllString (content , "" )
492545}
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