@@ -32,7 +32,6 @@ import (
3232 "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3333 "sigs.k8s.io/kubebuilder/v4/pkg/plugins"
3434 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
35- "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
3635 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates"
3736 charttemplates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
3837 templatescertmanager "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/cert-manager"
@@ -70,11 +69,9 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
7069func (s * initScaffolder ) Scaffold () error {
7170 log .Println ("Generating Helm Chart to distribute project" )
7271
73- // Extract Images scaffolded with DeployImage to add ENVVAR to the values
7472 imagesEnvVars := s .getDeployImagesEnvVars ()
7573
76- // Extract webhooks from generated YAML files (generated by controller-gen)
77- webhooks , err := extractWebhooksFromGeneratedFiles ()
74+ mutatingWebhooks , validatingWebhooks , err := s .extractWebhooksFromGeneratedFiles ()
7875 if err != nil {
7976 return fmt .Errorf ("failed to extract webhooks: %w" , err )
8077 }
@@ -83,12 +80,12 @@ func (s *initScaffolder) Scaffold() error {
8380 machinery .WithConfig (s .config ),
8481 )
8582
83+ hasWebhooks := len (mutatingWebhooks ) > 0 || len (validatingWebhooks ) > 0
8684 buildScaffold := []machinery.Builder {
8785 & github.HelmChartCI {},
8886 & templates.HelmChart {},
8987 & templates.HelmValues {
90- HasWebhooks : len (webhooks ) > 0 ,
91- Webhooks : webhooks ,
88+ HasWebhooks : hasWebhooks ,
9289 DeployImages : imagesEnvVars ,
9390 Force : s .force ,
9491 },
@@ -97,16 +94,21 @@ func (s *initScaffolder) Scaffold() error {
9794 & manager.Deployment {
9895 Force : s .force ,
9996 DeployImages : len (imagesEnvVars ) > 0 ,
100- HasWebhooks : len ( webhooks ) > 0 ,
97+ HasWebhooks : hasWebhooks ,
10198 },
10299 & templatescertmanager.Certificate {},
103100 & templatesmetrics.Service {},
104101 & prometheus.Monitor {},
105102 }
106103
107- if len (webhooks ) > 0 {
108- buildScaffold = append (buildScaffold , & templateswebhooks.Template {})
109- buildScaffold = append (buildScaffold , & templateswebhooks.Service {})
104+ if len (mutatingWebhooks ) > 0 || len (validatingWebhooks ) > 0 {
105+ buildScaffold = append (buildScaffold ,
106+ & templateswebhooks.Template {
107+ MutatingWebhooks : mutatingWebhooks ,
108+ ValidatingWebhooks : validatingWebhooks ,
109+ },
110+ & templateswebhooks.Service {},
111+ )
110112 }
111113
112114 if err := scaffold .Execute (buildScaffold ... ); err != nil {
@@ -146,87 +148,74 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
146148 return deployImages
147149}
148150
149- // Extract webhooks from manifests.yaml file
150- func extractWebhooksFromGeneratedFiles () ([]helm.WebhookYAML , error ) {
151- var webhooks []helm.WebhookYAML
151+ // extractWebhooksFromGeneratedFiles parses the files generated by controller-gen under
152+ // config/webhooks and created Mutating and Validating helper structures to
153+ // generate the webhook manifest for the helm-chart
154+ func (s * initScaffolder ) extractWebhooksFromGeneratedFiles () (mutatingWebhooks []templateswebhooks.DataWebhook ,
155+ validatingWebhooks []templateswebhooks.DataWebhook , err error ) {
152156 manifestFile := "config/webhook/manifests.yaml"
153- if _ , err := os .Stat (manifestFile ); err == nil {
154- content , err := os .ReadFile (manifestFile )
155- if err != nil {
156- return nil , fmt .Errorf ("failed to read manifests.yaml: %w" , err )
157- }
158157
159- // Process the content to extract webhooks
160- webhooks = append (webhooks , extractWebhookYAML (content )... )
161- } else {
162- // Return empty if no webhooks were found
163- return webhooks , nil
158+ if _ , err := os .Stat (manifestFile ); os .IsNotExist (err ) {
159+ log .Printf ("webhook manifests were not found at %s" , manifestFile )
160+ return nil , nil , nil
164161 }
165162
166- return webhooks , nil
167- }
168-
169- // extractWebhookYAML parses the webhook YAML content and returns a list of WebhookYAML
170- func extractWebhookYAML (content []byte ) []helm.WebhookYAML {
171- var webhooks []helm.WebhookYAML
172-
173- type WebhookConfig struct {
174- Kind string `yaml:"kind"`
175- Webhooks []struct {
176- Name string `yaml:"name"`
177- ClientConfig struct {
178- Service struct {
179- Name string `yaml:"name"`
180- Namespace string `yaml:"namespace"`
181- Path string `yaml:"path"`
182- } `yaml:"service"`
183- CABundle string `yaml:"caBundle"`
184- } `yaml:"clientConfig"`
185- Rules []helm.WebhookRule `yaml:"rules"`
186- FailurePolicy string `yaml:"failurePolicy"`
187- SideEffects string `yaml:"sideEffects"`
188- AdmissionReviewVersions []string `yaml:"admissionReviewVersions"`
189- } `yaml:"webhooks"`
163+ content , err := os .ReadFile (manifestFile )
164+ if err != nil {
165+ return nil , nil ,
166+ fmt .Errorf ("failed to read %s: %w" , manifestFile , err )
190167 }
191168
192- // Split the input into different documents (to handle multiple YAML docs in one file)
193169 docs := strings .Split (string (content ), "---" )
194-
195170 for _ , doc := range docs {
196- var webhookConfig WebhookConfig
197- if err := yaml .Unmarshal ([]byte (doc ), & webhookConfig ); err != nil {
198- log .Errorf ("Error unmarshalling webhook YAML: %v" , err )
199- continue
171+ var webhookConfig struct {
172+ Kind string `yaml:"kind"`
173+ Webhooks []struct {
174+ Name string `yaml:"name"`
175+ ClientConfig struct {
176+ Service struct {
177+ Name string `yaml:"name"`
178+ Namespace string `yaml:"namespace"`
179+ Path string `yaml:"path"`
180+ } `yaml:"service"`
181+ } `yaml:"clientConfig"`
182+ Rules []templateswebhooks.DataWebhookRule `yaml:"rules"`
183+ FailurePolicy string `yaml:"failurePolicy"`
184+ SideEffects string `yaml:"sideEffects"`
185+ AdmissionReviewVersions []string `yaml:"admissionReviewVersions"`
186+ } `yaml:"webhooks"`
200187 }
201188
202- // Determine the webhook type (mutating or validating)
203- webhookType := "unknown"
204- if webhookConfig .Kind == "MutatingWebhookConfiguration" {
205- webhookType = "mutating"
206- } else if webhookConfig .Kind == "ValidatingWebhookConfiguration" {
207- webhookType = "validating"
189+ if err := yaml .Unmarshal ([]byte (doc ), & webhookConfig ); err != nil {
190+ log .Errorf ("fail to unmarshalling webhook YAML: %v" , err )
191+ continue
208192 }
209193
210- // Parse each webhook and append it to the result
211- for _ , webhook := range webhookConfig .Webhooks {
212- for i := range webhook .Rules {
213- // If apiGroups is empty, set it to [""] to ensure proper YAML output
214- if len (webhook .Rules [i ].APIGroups ) == 0 {
215- webhook .Rules [i ].APIGroups = []string {"" }
194+ for _ , w := range webhookConfig .Webhooks {
195+ for i := range w .Rules {
196+ if len (w .Rules [i ].APIGroups ) == 0 {
197+ w .Rules [i ].APIGroups = []string {"" }
216198 }
217199 }
218- webhooks = append (webhooks , helm.WebhookYAML {
219- Name : webhook .Name ,
220- Type : webhookType ,
221- Path : webhook .ClientConfig .Service .Path ,
222- Rules : webhook .Rules ,
223- FailurePolicy : webhook .FailurePolicy ,
224- SideEffects : webhook .SideEffects ,
225- AdmissionReviewVersions : webhook .AdmissionReviewVersions ,
226- })
200+ webhook := templateswebhooks.DataWebhook {
201+ Name : w .Name ,
202+ ServiceName : fmt .Sprintf ("%s-webhook-service" , s .config .GetProjectName ()),
203+ Path : w .ClientConfig .Service .Path ,
204+ FailurePolicy : w .FailurePolicy ,
205+ SideEffects : w .SideEffects ,
206+ AdmissionReviewVersions : w .AdmissionReviewVersions ,
207+ Rules : w .Rules ,
208+ }
209+
210+ if webhookConfig .Kind == "MutatingWebhookConfiguration" {
211+ mutatingWebhooks = append (mutatingWebhooks , webhook )
212+ } else if webhookConfig .Kind == "ValidatingWebhookConfiguration" {
213+ validatingWebhooks = append (validatingWebhooks , webhook )
214+ }
227215 }
228216 }
229- return webhooks
217+
218+ return mutatingWebhooks , validatingWebhooks , nil
230219}
231220
232221// Helper function to copy files from config/ to dist/chart/templates/
0 commit comments