@@ -32,9 +32,8 @@ 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"
37- chart_templates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
36+ 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"
3938 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/manager"
4039 templatesmetrics "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/metrics"
@@ -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+ webhooks , err := s .extractWebhooksFromGeneratedFiles ()
7875 if err != nil {
7976 return fmt .Errorf ("failed to extract webhooks: %w" , err )
8077 }
@@ -88,12 +85,11 @@ func (s *initScaffolder) Scaffold() error {
8885 & templates.HelmChart {},
8986 & templates.HelmValues {
9087 HasWebhooks : len (webhooks ) > 0 ,
91- Webhooks : webhooks ,
9288 DeployImages : imagesEnvVars ,
9389 Force : s .force ,
9490 },
9591 & templates.HelmIgnore {},
96- & chart_templates .HelmHelpers {},
92+ & charttemplates .HelmHelpers {},
9793 & manager.ManagerDeployment {
9894 Force : s .force ,
9995 DeployImages : len (imagesEnvVars ) > 0 ,
@@ -105,8 +101,12 @@ func (s *initScaffolder) Scaffold() error {
105101 }
106102
107103 if len (webhooks ) > 0 {
108- buildScaffold = append (buildScaffold , & templateswebhooks.WebhookTemplate {})
109- buildScaffold = append (buildScaffold , & templateswebhooks.WebhookService {})
104+ buildScaffold = append (buildScaffold ,
105+ & templateswebhooks.WebhookTemplate {
106+ Webhooks : webhooks ,
107+ },
108+ & templateswebhooks.WebhookService {},
109+ )
110110 }
111111
112112 if err := scaffold .Execute (buildScaffold ... ); err != nil {
@@ -146,87 +146,73 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
146146 return deployImages
147147}
148148
149- // Extract webhooks from manifests.yaml file
150- func extractWebhooksFromGeneratedFiles () ([]helm. WebhookYAML , error ) {
151- var webhooks []helm. WebhookYAML
149+ // extractWebhooksFromGeneratedFiles parse the files and get the webhook data
150+ func ( s * initScaffolder ) extractWebhooksFromGeneratedFiles () ([]templateswebhooks. Webhook , error ) {
151+ var webhooks []templateswebhooks. Webhook
152152 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- }
158153
159- // Process the content to extract webhooks
160- webhooks = append (webhooks , extractWebhookYAML (content )... )
161- } else {
162- // Return empty if no webhooks were found
154+ if _ , err := os .Stat (manifestFile ); os .IsNotExist (err ) {
155+ log .Printf ("webhook manifests were not found at %s" , manifestFile )
163156 return webhooks , nil
164157 }
165158
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"`
159+ content , err := os .ReadFile (manifestFile )
160+ if err != nil {
161+ return nil , fmt .Errorf ("failed to read %s: %w" , manifestFile , err )
190162 }
191163
192- // Split the input into different documents (to handle multiple YAML docs in one file)
193164 docs := strings .Split (string (content ), "---" )
194-
195165 for _ , doc := range docs {
196- var webhookConfig WebhookConfig
166+ var webhookConfig struct {
167+ Kind string `yaml:"kind"`
168+ Webhooks []struct {
169+ Name string `yaml:"name"`
170+ ClientConfig struct {
171+ Service struct {
172+ Name string `yaml:"name"`
173+ Namespace string `yaml:"namespace"`
174+ Path string `yaml:"path"`
175+ } `yaml:"service"`
176+ } `yaml:"clientConfig"`
177+ Rules []templateswebhooks.WebhookRule `yaml:"rules"`
178+ FailurePolicy string `yaml:"failurePolicy"`
179+ SideEffects string `yaml:"sideEffects"`
180+ AdmissionReviewVersions []string `yaml:"admissionReviewVersions"`
181+ } `yaml:"webhooks"`
182+ }
183+
197184 if err := yaml .Unmarshal ([]byte (doc ), & webhookConfig ); err != nil {
198185 log .Errorf ("Error unmarshalling webhook YAML: %v" , err )
199186 continue
200187 }
201188
202- // Determine the webhook type (mutating or validating)
203189 webhookType := "unknown"
204190 if webhookConfig .Kind == "MutatingWebhookConfiguration" {
205191 webhookType = "mutating"
206192 } else if webhookConfig .Kind == "ValidatingWebhookConfiguration" {
207193 webhookType = "validating"
208194 }
209195
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 {"" }
196+ for _ , w := range webhookConfig .Webhooks {
197+ for i := range w .Rules {
198+ if len (w .Rules [i ].APIGroups ) == 0 {
199+ w .Rules [i ].APIGroups = []string {"" }
216200 }
217201 }
218- webhooks = append (webhooks , helm.WebhookYAML {
219- Name : webhook .Name ,
202+ webhooks = append (webhooks , templateswebhooks.Webhook {
203+ NameMetadata : fmt .Sprintf ("%s-%s-webhook-configuration" , s .config .GetProjectName (), webhookType ),
204+ Name : w .Name ,
220205 Type : webhookType ,
221- Path : webhook .ClientConfig .Service .Path ,
222- Rules : webhook .Rules ,
223- FailurePolicy : webhook .FailurePolicy ,
224- SideEffects : webhook .SideEffects ,
225- AdmissionReviewVersions : webhook .AdmissionReviewVersions ,
206+ ServiceName : fmt .Sprintf ("%s-webhook-service" , s .config .GetProjectName ()),
207+ Path : w .ClientConfig .Service .Path ,
208+ FailurePolicy : w .FailurePolicy ,
209+ SideEffects : w .SideEffects ,
210+ AdmissionReviewVersions : w .AdmissionReviewVersions ,
211+ Rules : w .Rules ,
226212 })
227213 }
228214 }
229- return webhooks
215+ return webhooks , nil
230216}
231217
232218// Helper function to copy files from config/ to dist/chart/templates/
0 commit comments