Skip to content

Commit 16b3fca

Browse files
This PR introduces a completely rewritten Helm plugin (helm/v2-alpha) that dynamically generates Helm charts based on the actual kustomize output from make build-installer, replacing the previous hardcoded template approach in helm/v1-alpha.
The existing `helm/v1-alpha` plugin used static templates that didn't reflect user customizations (environment variables, labels, annotations, security contexts, etc.) made in their kustomize configuration. This led to inconsistencies between `kubectl apply -f dist/install.yaml` and `helm install`. - Deprecated Helm v1-alpha in favour of v2 - Add docs and tests for Helm v2 - Update all samples - Address all feedbacks raised so far Assisted-by: Cursor
1 parent dfadc63 commit 16b3fca

File tree

226 files changed

+19709
-15867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+19709
-15867
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ generate-charts: build ## Re-generate the helm chart testdata and docs samples
103103
rm -rf docs/book/src/cronjob-tutorial/testdata/project/dist/chart
104104
rm -rf docs/book/src/multiversion-tutorial/testdata/project/dist/chart
105105

106-
(cd testdata/project-v4-with-plugins && ../../bin/kubebuilder edit --plugins=helm/v1-alpha)
107-
(cd docs/book/src/getting-started/testdata/project && ../../../../../../bin/kubebuilder edit --plugins=helm/v1-alpha)
108-
(cd docs/book/src/cronjob-tutorial/testdata/project && ../../../../../../bin/kubebuilder edit --plugins=helm/v1-alpha)
109-
(cd docs/book/src/multiversion-tutorial/testdata/project && ../../../../../../bin/kubebuilder edit --plugins=helm/v1-alpha)
106+
# Generate helm charts from kustomize manifests using v2-alpha plugin
107+
(cd testdata/project-v4-with-plugins && make build-installer && ../../bin/kubebuilder edit --plugins=helm/v2-alpha)
108+
(cd docs/book/src/getting-started/testdata/project && make build-installer && ../../../../../../bin/kubebuilder edit --plugins=helm/v2-alpha)
109+
(cd docs/book/src/cronjob-tutorial/testdata/project && make build-installer && ../../../../../../bin/kubebuilder edit --plugins=helm/v2-alpha)
110+
(cd docs/book/src/multiversion-tutorial/testdata/project && make build-installer && ../../../../../../bin/kubebuilder edit --plugins=helm/v2-alpha)
110111

111112
.PHONY: check-docs
112113
check-docs: ## Run the script to ensure that the docs are updated

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
autoupdatev1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha"
3535
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
3636
helmv1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
37+
helmv2alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha"
3738
)
3839

3940
// Run bootstraps & runs the CLI
@@ -72,6 +73,7 @@ func Run() {
7273
&deployimagev1alpha1.Plugin{},
7374
&grafanav1alpha1.Plugin{},
7475
&helmv1alpha1.Plugin{},
76+
&helmv2alpha1.Plugin{},
7577
&autoupdatev1alpha1.Plugin{},
7678
),
7779
cli.WithPlugins(externalPlugins...),

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
- [go/v4](./plugins/available/go-v4-plugin.md)
126126
- [grafana/v1-alpha](./plugins/available/grafana-v1-alpha.md)
127127
- [helm/v1-alpha](./plugins/available/helm-v1-alpha.md)
128+
- [helm/v2-alpha](./plugins/available/helm-v2-alpha.md)
128129
- [kustomize/v2](./plugins/available/kustomize-v2.md)
129130
- [Extending](./plugins/extending.md)
130131
- [CLI and Plugins](./plugins/extending/extending_cli_features_and_plugins.md)

docs/book/src/cronjob-tutorial/testdata/project/PROJECT

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ domain: tutorial.kubebuilder.io
77
layout:
88
- go.kubebuilder.io/v4
99
plugins:
10-
helm.kubebuilder.io/v1-alpha: {}
10+
helm.kubebuilder.io/v2-alpha:
11+
manifests: dist/install.yaml
12+
output: dist
1113
projectName: project
1214
repo: tutorial.kubebuilder.io/project
1315
resources:

docs/book/src/cronjob-tutorial/testdata/project/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>
9797
1. Build the chart using the optional helm plugin
9898

9999
```sh
100-
kubebuilder edit --plugins=helm/v1-alpha
100+
kubebuilder edit --plugins=helm/v2-alpha
101101
```
102102

103103
2. See that a chart was generated under 'dist/chart', and users
Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
{{/*
2+
Chart name based on project name.
3+
Truncated to 63 characters for Kubernetes compatibility.
4+
*/}}
15
{{- define "chart.name" -}}
26
{{- if .Chart }}
37
{{- if .Chart.Name }}
48
{{- .Chart.Name | trunc 63 | trimSuffix "-" }}
5-
{{- else if .Values.nameOverride }}
6-
{{ .Values.nameOverride | trunc 63 | trimSuffix "-" }}
79
{{- else }}
810
project
911
{{- end }}
@@ -12,7 +14,52 @@
1214
{{- end }}
1315
{{- end }}
1416

17+
{{/*
18+
Full name of the chart (with release name prefix).
19+
Combines release name with chart name.
20+
Truncated to 63 characters for Kubernetes compatibility.
21+
*/}}
22+
{{- define "chart.fullname" -}}
23+
{{- $name := include "chart.name" . }}
24+
{{- if contains $name .Release.Name }}
25+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
26+
{{- else }}
27+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
28+
{{- end }}
29+
{{- end }}
30+
31+
{{/*
32+
Namespace name for the chart.
33+
Uses .Values.namespace.name if provided, otherwise defaults to "<fullname>-system".
34+
*/}}
35+
{{- define "chart.namespaceName" -}}
36+
{{- if .Values.namespace.name }}
37+
{{- .Values.namespace.name }}
38+
{{- else }}
39+
{{- printf "%s-system" (include "chart.fullname" .) }}
40+
{{- end }}
41+
{{- end }}
42+
1543

44+
45+
{{/*
46+
Service name with proper truncation for Kubernetes 63-character limit.
47+
Takes a context with .suffix for the service type (e.g., "webhook-service").
48+
If fullname + suffix exceeds 63 chars, truncates fullname to 45 chars.
49+
*/}}
50+
{{- define "chart.serviceName" -}}
51+
{{- $fullname := include "chart.fullname" .context -}}
52+
{{- if gt (len $fullname) 45 -}}
53+
{{- printf "%s-%s" (trunc 45 $fullname | trimSuffix "-") .suffix | trunc 63 | trimSuffix "-" -}}
54+
{{- else -}}
55+
{{- printf "%s-%s" $fullname .suffix | trunc 63 | trimSuffix "-" -}}
56+
{{- end -}}
57+
{{- end }}
58+
59+
{{/*
60+
Common labels for Helm charts.
61+
Includes app version, chart version, app name, instance, and managed-by labels.
62+
*/}}
1663
{{- define "chart.labels" -}}
1764
{{- if .Chart.AppVersion -}}
1865
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
@@ -25,26 +72,19 @@ app.kubernetes.io/instance: {{ .Release.Name }}
2572
app.kubernetes.io/managed-by: {{ .Release.Service }}
2673
{{- end }}
2774

28-
75+
{{/*
76+
Selector labels for matching pods and services.
77+
Only includes name and instance for consistent selection.
78+
*/}}
2979
{{- define "chart.selectorLabels" -}}
3080
app.kubernetes.io/name: {{ include "chart.name" . }}
3181
app.kubernetes.io/instance: {{ .Release.Name }}
3282
{{- end }}
3383

3484

35-
{{- define "chart.hasMutatingWebhooks" -}}
36-
{{- $hasMutating := false }}
37-
{{- range . }}
38-
{{- if eq .type "mutating" }}
39-
$hasMutating = true }}{{- end }}
40-
{{- end }}
41-
{{ $hasMutating }}}}{{- end }}
42-
4385

44-
{{- define "chart.hasValidatingWebhooks" -}}
45-
{{- $hasValidating := false }}
46-
{{- range . }}
47-
{{- if eq .type "validating" }}
48-
$hasValidating = true }}{{- end }}
49-
{{- end }}
50-
{{ $hasValidating }}}}{{- end }}
86+
{{/*
87+
Note: Extra labels and annotations functionality removed for simplicity.
88+
The chart focuses on converting kustomize output to Helm templates.
89+
Users can still add labels/annotations directly in their kustomize manifests.
90+
*/}}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if and .Values.certManager.enable .Values.metrics.enable }}
2+
apiVersion: cert-manager.io/v1
3+
kind: Certificate
4+
metadata:
5+
labels:
6+
app.kubernetes.io/managed-by: {{ .Release.Service }}
7+
app.kubernetes.io/name: project
8+
name: project-metrics-certs
9+
namespace: {{ .Release.Namespace }}
10+
spec:
11+
dnsNames:
12+
- {{ include "chart.serviceName" (dict "suffix" "controller-manager-metrics-service" "context" .) }}.{{ .Release.Namespace }}.svc
13+
- {{ include "chart.serviceName" (dict "suffix" "controller-manager-metrics-service" "context" .) }}.{{ .Release.Namespace }}.svc.cluster.local
14+
issuerRef:
15+
kind: Issuer
16+
name: {{ include "chart.name" . }}-selfsigned-issuer
17+
secretName: metrics-server-cert
18+
{{- end }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{- if .Values.certManager.enable }}
2+
apiVersion: cert-manager.io/v1
3+
kind: Issuer
4+
metadata:
5+
labels:
6+
app.kubernetes.io/managed-by: {{ .Release.Service }}
7+
app.kubernetes.io/name: project
8+
name: project-selfsigned-issuer
9+
namespace: {{ .Release.Namespace }}
10+
spec:
11+
selfSigned: {}
12+
{{- end }}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if .Values.certManager.enable }}
2+
apiVersion: cert-manager.io/v1
3+
kind: Certificate
4+
metadata:
5+
labels:
6+
app.kubernetes.io/managed-by: {{ .Release.Service }}
7+
app.kubernetes.io/name: project
8+
name: project-serving-cert
9+
namespace: {{ .Release.Namespace }}
10+
spec:
11+
dnsNames:
12+
- project-webhook-service.{{ .Release.Namespace }}.svc
13+
- project-webhook-service.{{ .Release.Namespace }}.svc.cluster.local
14+
issuerRef:
15+
kind: Issuer
16+
name: {{ include "chart.name" . }}-selfsigned-issuer
17+
secretName: webhook-server-cert
18+
{{- end }}

docs/book/src/cronjob-tutorial/testdata/project/dist/chart/templates/certmanager/certificate.yaml

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)