Skip to content

Commit 2c9f1e3

Browse files
(helm/v1alpha1) - fix webhook generation by removing data from helm chart values
This commit changes the code implementation to not generate the webhook values in the helm chart values. Instead only expose on the values to enable or not webhooks
1 parent 6fb374f commit 2c9f1e3

File tree

5 files changed

+126
-233
lines changed

5 files changed

+126
-233
lines changed

pkg/plugins/optional/helm/v1alpha/scaffolds/init.go

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
3535
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
3636
"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"
37+
chartTemplates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
3838
templatescertmanager "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/cert-manager"
3939
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/manager"
4040
templatesmetrics "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/metrics"
@@ -70,11 +70,9 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
7070
func (s *initScaffolder) Scaffold() error {
7171
log.Println("Generating Helm Chart to distribute project")
7272

73-
// Extract Images scaffolded with DeployImage to add ENVVAR to the values
7473
imagesEnvVars := s.getDeployImagesEnvVars()
7574

76-
// Extract webhooks from generated YAML files (generated by controller-gen)
77-
webhooks, err := extractWebhooksFromGeneratedFiles()
75+
webhooks, err := s.extractWebhooks()
7876
if err != nil {
7977
return fmt.Errorf("failed to extract webhooks: %w", err)
8078
}
@@ -88,12 +86,11 @@ func (s *initScaffolder) Scaffold() error {
8886
&templates.HelmChart{},
8987
&templates.HelmValues{
9088
HasWebhooks: len(webhooks) > 0,
91-
Webhooks: webhooks,
9289
DeployImages: imagesEnvVars,
9390
Force: s.force,
9491
},
9592
&templates.HelmIgnore{},
96-
&chart_templates.HelmHelpers{},
93+
&chartTemplates.HelmHelpers{},
9794
&manager.ManagerDeployment{
9895
Force: s.force,
9996
DeployImages: len(imagesEnvVars) > 0,
@@ -105,8 +102,12 @@ func (s *initScaffolder) Scaffold() error {
105102
}
106103

107104
if len(webhooks) > 0 {
108-
buildScaffold = append(buildScaffold, &templateswebhooks.WebhookTemplate{})
109-
buildScaffold = append(buildScaffold, &templateswebhooks.WebhookService{})
105+
buildScaffold = append(buildScaffold,
106+
&templateswebhooks.WebhookTemplate{
107+
Webhooks: webhooks,
108+
},
109+
&templateswebhooks.WebhookService{},
110+
)
110111
}
111112

112113
if err := scaffold.Execute(buildScaffold...); err != nil {
@@ -146,23 +147,64 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
146147
return deployImages
147148
}
148149

149-
// Extract webhooks from manifests.yaml file
150-
func extractWebhooksFromGeneratedFiles() ([]helm.WebhookYAML, error) {
151-
var webhooks []helm.WebhookYAML
150+
// extractWebhooks reads webhook YAML configurations and converts them to Webhook structs.
151+
func (s *initScaffolder) extractWebhooks() ([]templateswebhooks.Webhook, error) {
152+
var webhooks []templateswebhooks.Webhook
152153
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-
}
158154

159-
// Process the content to extract webhooks
160-
webhooks = append(webhooks, extractWebhookYAML(content)...)
161-
} else {
162-
// Return empty if no webhooks were found
155+
if _, err := os.Stat(manifestFile); os.IsNotExist(err) {
156+
log.Printf("No webhook manifests found at %s", manifestFile)
163157
return webhooks, nil
164158
}
165159

160+
content, err := os.ReadFile(manifestFile)
161+
if err != nil {
162+
return nil, fmt.Errorf("failed to read %s: %w", manifestFile, err)
163+
}
164+
165+
docs := strings.Split(string(content), "---")
166+
for _, doc := range docs {
167+
var webhookConfig struct {
168+
Kind string `yaml:"kind"`
169+
Webhooks []struct {
170+
Name string `yaml:"name"`
171+
ClientConfig struct {
172+
Service struct {
173+
Name string `yaml:"name"`
174+
Namespace string `yaml:"namespace"`
175+
Path string `yaml:"path"`
176+
} `yaml:"service"`
177+
} `yaml:"clientConfig"`
178+
Rules []templateswebhooks.WebhookRule `yaml:"rules"`
179+
FailurePolicy string `yaml:"failurePolicy"`
180+
SideEffects string `yaml:"sideEffects"`
181+
AdmissionReviewVersions []string `yaml:"admissionReviewVersions"`
182+
} `yaml:"webhooks"`
183+
}
184+
185+
if err := yaml.Unmarshal([]byte(doc), &webhookConfig); err != nil {
186+
log.Errorf("Error parsing webhook YAML: %v", err)
187+
continue
188+
}
189+
190+
webhookType := "validating"
191+
if webhookConfig.Kind == "MutatingWebhookConfiguration" {
192+
webhookType = "mutating"
193+
}
194+
195+
for _, w := range webhookConfig.Webhooks {
196+
webhooks = append(webhooks, templateswebhooks.Webhook{
197+
Name: w.Name,
198+
Type: webhookType,
199+
ServiceName: w.ClientConfig.Service.Name,
200+
Path: w.ClientConfig.Service.Path,
201+
FailurePolicy: w.FailurePolicy,
202+
SideEffects: w.SideEffects,
203+
AdmissionReviewVersions: w.AdmissionReviewVersions,
204+
Rules: w.Rules,
205+
})
206+
}
207+
}
166208
return webhooks, nil
167209
}
168210

pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates/webhook/webhook.go

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var _ machinery.Template = &WebhookTemplate{}
2727
type WebhookTemplate struct {
2828
machinery.TemplateMixin
2929
machinery.ProjectNameMixin
30+
31+
Webhooks []Webhook
3032
}
3133

3234
// SetTemplateDefaults sets default configuration for the webhook template
@@ -42,12 +44,32 @@ func (f *WebhookTemplate) SetTemplateDefaults() error {
4244
return nil
4345
}
4446

45-
const webhookTemplate = `{{` + "`" + `{{- if .Values.webhook.enable }}` + "`" + `}}
47+
// Webhook helps generate the bollerplate with the Webhook values
48+
type Webhook struct {
49+
ServiceName string
50+
Name string
51+
Path string
52+
Type string
53+
FailurePolicy string
54+
SideEffects string
55+
AdmissionReviewVersions []string
56+
Rules []WebhookRule
57+
}
4658

59+
// WebhookRule to help map the rules
60+
type WebhookRule struct {
61+
Operations []string
62+
APIGroups []string
63+
APIVersions []string
64+
Resources []string
65+
}
66+
67+
const webhookTemplate = `{{` + "`" + `{{- if .Values.webhook.enable }}` + "`" + `}}
68+
{{- range .Webhooks }}
4769
apiVersion: admissionregistration.k8s.io/v1
48-
kind: MutatingWebhookConfiguration
70+
kind: {{ if eq .Type "mutating" }}MutatingWebhookConfiguration{{ else }}ValidatingWebhookConfiguration{{ end }}
4971
metadata:
50-
name: {{ .ProjectName }}-mutating-webhook-configuration
72+
name: {{ .Name }}
5173
namespace: {{ "{{ .Release.Namespace }}" }}
5274
annotations:
5375
{{` + "`" + `{{- if .Values.certmanager.enable }}` + "`" + `}}
@@ -56,87 +78,38 @@ metadata:
5678
labels:
5779
{{ "{{- include \"chart.labels\" . | nindent 4 }}" }}
5880
webhooks:
59-
{{` + "`" + `{{- range .Values.webhook.services }}` + "`" + `}}
60-
{{` + "`" + `{{- if eq .type "mutating" }}` + "`" + `}}
61-
- name: {{` + "`" + `{{ .name }}` + "`" + `}}
62-
clientConfig:
63-
service:
64-
name: {{ .ProjectName }}-webhook-service
65-
namespace: {{` + "`" + `{{ $.Release.Namespace }}` + "`" + `}}
66-
path: {{` + "`" + `{{ .path }}` + "`" + `}}
67-
failurePolicy: {{` + "`" + `{{ .failurePolicy }}` + "`" + `}}
68-
sideEffects: {{` + "`" + `{{ .sideEffects }}` + "`" + `}}
69-
admissionReviewVersions:
70-
{{` + "`" + `{{- range .admissionReviewVersions }}` + "`" + `}}
71-
- {{` + "`" + `{{ . }}` + "`" + `}}
72-
{{` + "`" + `{{- end }}` + "`" + `}}
73-
rules:
74-
{{` + "`" + `{{- range .rules }}` + "`" + `}}
75-
- operations:
76-
{{` + "`" + `{{- range .operations }}` + "`" + `}}
77-
- {{` + "`" + `{{ . }}` + "`" + `}}
78-
{{` + "`" + `{{- end }}` + "`" + `}}
79-
apiGroups:
80-
{{` + "`" + `{{- range .apiGroups }}` + "`" + `}}
81-
- {{` + "`" + `{{ . }}` + "`" + `}}
82-
{{` + "`" + `{{- end }}` + "`" + `}}
83-
apiVersions:
84-
{{` + "`" + `{{- range .apiVersions }}` + "`" + `}}
85-
- {{` + "`" + `{{ . }}` + "`" + `}}
86-
{{` + "`" + `{{- end }}` + "`" + `}}
87-
resources:
88-
{{` + "`" + `{{- range .resources }}` + "`" + `}}
89-
- {{` + "`" + `{{ . }}` + "`" + `}}
90-
{{` + "`" + `{{- end }}` + "`" + `}}
91-
{{` + "`" + `{{- end }}` + "`" + `}}
92-
{{` + "`" + `{{- end }}` + "`" + `}}
93-
{{` + "`" + `{{- end }}` + "`" + `}}
94-
---
95-
apiVersion: admissionregistration.k8s.io/v1
96-
kind: ValidatingWebhookConfiguration
97-
metadata:
98-
name: {{ .ProjectName }}-validating-webhook-configuration
99-
namespace: {{ "{{ .Release.Namespace }}" }}
100-
annotations:
101-
{{` + "`" + `{{- if .Values.certmanager.enable }}` + "`" + `}}
102-
cert-manager.io/inject-ca-from: "{{` + "`" + `{{ $.Release.Namespace }}` + "`" + `}}/serving-cert"
103-
{{` + "`" + `{{- end }}` + "`" + `}}
104-
webhooks:
105-
{{` + "`" + `{{- range .Values.webhook.services }}` + "`" + `}}
106-
{{` + "`" + `{{- if eq .type "validating" }}` + "`" + `}}
107-
- name: {{` + "`" + `{{ .name }}` + "`" + `}}
81+
- name: {{ .Name }}
10882
clientConfig:
10983
service:
110-
name: {{ .ProjectName }}-webhook-service
111-
namespace: {{` + "`" + `{{ $.Release.Namespace }}` + "`" + `}}
112-
path: {{` + "`" + `{{ .path }}` + "`" + `}}
113-
failurePolicy: {{` + "`" + `{{ .failurePolicy }}` + "`" + `}}
114-
sideEffects: {{` + "`" + `{{ .sideEffects }}` + "`" + `}}
84+
name: {{ .ServiceName }}
85+
namespace: {{ "{{ .Release.Namespace }}" }}
86+
path: {{ .Path }}
87+
failurePolicy: {{ .FailurePolicy }}
88+
sideEffects: {{ .SideEffects }}
11589
admissionReviewVersions:
116-
{{` + "`" + `{{- range .admissionReviewVersions }}` + "`" + `}}
117-
- {{` + "`" + `{{ . }}` + "`" + `}}
118-
{{` + "`" + `{{- end }}` + "`" + `}}
90+
{{- range .AdmissionReviewVersions }}
91+
- {{ . }}
92+
{{- end }}
11993
rules:
120-
{{` + "`" + `{{- range .rules }}` + "`" + `}}
94+
{{- range .Rules }}
12195
- operations:
122-
{{` + "`" + `{{- range .operations }}` + "`" + `}}
123-
- {{` + "`" + `{{ . }}` + "`" + `}}
124-
{{` + "`" + `{{- end }}` + "`" + `}}
96+
{{- range .Operations }}
97+
- {{ . }}
98+
{{- end }}
12599
apiGroups:
126-
{{` + "`" + `{{- range .apiGroups }}` + "`" + `}}
127-
- {{` + "`" + `{{ . }}` + "`" + `}}
128-
{{` + "`" + `{{- end }}` + "`" + `}}
100+
{{- range .APIGroups }}
101+
- {{ . }}
102+
{{- end }}
129103
apiVersions:
130-
{{` + "`" + `{{- range .apiVersions }}` + "`" + `}}
131-
- {{` + "`" + `{{ . }}` + "`" + `}}
132-
{{` + "`" + `{{- end }}` + "`" + `}}
104+
{{- range .APIVersions }}
105+
- {{ . }}
106+
{{- end }}
133107
resources:
134-
{{` + "`" + `{{- range .resources }}` + "`" + `}}
135-
- {{` + "`" + `{{ . }}` + "`" + `}}
136-
{{` + "`" + `{{- end }}` + "`" + `}}
137-
{{` + "`" + `{{- end }}` + "`" + `}}
138-
{{` + "`" + `{{- end }}` + "`" + `}}
139-
{{` + "`" + `{{- end }}` + "`" + `}}
108+
{{- range .Resources }}
109+
- {{ . }}
110+
{{- end }}
111+
{{- end }}
140112
---
113+
{{- end }}
141114
{{` + "`" + `{{- end }}` + "`" + `}}
142115
`

pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/values.go

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"path/filepath"
2020

2121
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
22-
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
2322
)
2423

2524
var _ machinery.Template = &HelmValues{}
@@ -33,8 +32,6 @@ type HelmValues struct {
3332
DeployImages map[string]string
3433
// Force if true allows overwriting the scaffolded file
3534
Force bool
36-
// Webhooks stores the webhook configurations
37-
Webhooks []helm.WebhookYAML
3835
// HasWebhooks is true when webhooks were found in the config
3936
HasWebhooks bool
4037
}
@@ -125,52 +122,13 @@ crd:
125122
# ControllerManager argument "--metrics-bind-address=:8443" is removed.
126123
metrics:
127124
enable: true
128-
{{ if .Webhooks }}
125+
{{ if .HasWebhooks }}
129126
# [WEBHOOKS]: Webhooks configuration
130127
# The following configuration is automatically generated from the manifests
131128
# generated by controller-gen. To update run 'make manifests' and
132129
# the edit command with the '--force' flag
133130
webhook:
134131
enable: true
135-
services:
136-
{{- range .Webhooks }}
137-
- name: {{ .Name }}
138-
type: {{ .Type }}
139-
path: {{ .Path }}
140-
failurePolicy: {{ .FailurePolicy }}
141-
sideEffects: {{ .SideEffects }}
142-
admissionReviewVersions:
143-
{{- range .AdmissionReviewVersions }}
144-
- {{ . }}
145-
{{- end }}
146-
rules:
147-
{{- range .Rules }}
148-
- operations:
149-
{{- range .Operations }}
150-
- {{ . }}
151-
{{- end }}
152-
apiGroups:
153-
{{- if .APIGroups }}
154-
{{- range .APIGroups }}
155-
{{- if eq . "" }}
156-
- ""
157-
{{- else }}
158-
- {{ . }}
159-
{{- end }}
160-
{{- end }}
161-
{{- else }}
162-
- ""
163-
{{- end }}
164-
apiVersions:
165-
{{- range .APIVersions }}
166-
- {{ . }}
167-
{{- end }}
168-
resources:
169-
{{- range .Resources }}
170-
- {{ . }}
171-
{{- end }}
172-
{{- end }}
173-
{{- end }}
174132
{{ end }}
175133
# [PROMETHEUS]: To enable a ServiceMonitor to export metrics to Prometheus set true
176134
prometheus:

0 commit comments

Comments
 (0)