@@ -32,7 +32,6 @@ import (
32
32
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
33
33
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
34
34
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
35
-
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
36
35
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates"
37
36
charttemplates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
38
37
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) {
70
69
func (s *initScaffolder) Scaffold() error {
71
70
log.Println("Generating Helm Chart to distribute project")
72
71
73
-
// Extract Images scaffolded with DeployImage to add ENVVAR to the values
74
72
imagesEnvVars := s.getDeployImagesEnvVars()
75
73
76
-
// Extract webhooks from generated YAML files (generated by controller-gen)
77
-
webhooks, err := extractWebhooksFromGeneratedFiles()
74
+
mutatingWebhooks, validatingWebhooks, err := s.extractWebhooksFromGeneratedFiles()
78
75
if err != nil {
79
76
return fmt.Errorf("failed to extract webhooks: %w", err)
80
77
}
@@ -83,12 +80,12 @@ func (s *initScaffolder) Scaffold() error {
83
80
machinery.WithConfig(s.config),
84
81
)
85
82
83
+
hasWebhooks := len(mutatingWebhooks) > 0 || len(validatingWebhooks) > 0
86
84
buildScaffold := []machinery.Builder{
87
85
&github.HelmChartCI{},
88
86
&templates.HelmChart{},
89
87
&templates.HelmValues{
90
-
HasWebhooks: len(webhooks) > 0,
91
-
Webhooks: webhooks,
88
+
HasWebhooks: hasWebhooks,
92
89
DeployImages: imagesEnvVars,
93
90
Force: s.force,
94
91
},
@@ -97,16 +94,21 @@ func (s *initScaffolder) Scaffold() error {
97
94
&manager.Deployment{
98
95
Force: s.force,
99
96
DeployImages: len(imagesEnvVars) > 0,
100
-
HasWebhooks: len(webhooks) > 0,
97
+
HasWebhooks: hasWebhooks,
101
98
},
102
99
&templatescertmanager.Certificate{},
103
100
&templatesmetrics.Service{},
104
101
&prometheus.Monitor{},
105
102
}
106
103
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
+
)
110
112
}
111
113
112
114
if err := scaffold.Execute(buildScaffold...); err != nil {
@@ -146,87 +148,74 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
146
148
return deployImages
147
149
}
148
150
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) {
152
156
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
-
}
158
157
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
164
161
}
165
162
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)
190
167
}
191
168
192
-
// Split the input into different documents (to handle multiple YAML docs in one file)
193
169
docs := strings.Split(string(content), "---")
194
-
195
170
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"`
200
187
}
201
188
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
208
192
}
209
193
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{""}
216
198
}
217
199
}
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
+
}
227
215
}
228
216
}
229
-
return webhooks
217
+
218
+
return mutatingWebhooks, validatingWebhooks, nil
230
219
}
231
220
232
221
// Helper function to copy files from config/ to dist/chart/templates/
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4