Skip to content

Commit d9d172d

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 784f0e8 commit d9d172d

File tree

226 files changed

+18884
-15268
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

+18884
-15268
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

docs/book/src/cronjob-tutorial/testdata/project/dist/chart/templates/_helpers.tpl

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
{{/*
2+
Chart name with fallback to nameOverride or project name.
3+
Truncated to 63 characters for Kubernetes compatibility.
4+
*/}}
15
{{- define "chart.name" -}}
26
{{- if .Chart }}
37
{{- if .Chart.Name }}
@@ -12,7 +16,56 @@
1216
{{- end }}
1317
{{- end }}
1418

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

49+
50+
51+
{{/*
52+
Service name with proper truncation for Kubernetes 63-character limit.
53+
Takes a context with .suffix for the service type (e.g., "webhook-service").
54+
If fullname + suffix exceeds 63 chars, truncates fullname to 45 chars.
55+
*/}}
56+
{{- define "chart.serviceName" -}}
57+
{{- $fullname := include "chart.fullname" .context -}}
58+
{{- if gt (len $fullname) 45 -}}
59+
{{- printf "%s-%s" (trunc 45 $fullname | trimSuffix "-") .suffix | trunc 63 | trimSuffix "-" -}}
60+
{{- else -}}
61+
{{- printf "%s-%s" $fullname .suffix | trunc 63 | trimSuffix "-" -}}
62+
{{- end -}}
63+
{{- end }}
64+
65+
{{/*
66+
Common labels for Helm charts.
67+
Includes app version, chart version, app name, instance, and managed-by labels.
68+
*/}}
1669
{{- define "chart.labels" -}}
1770
{{- if .Chart.AppVersion -}}
1871
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
@@ -25,26 +78,36 @@ app.kubernetes.io/instance: {{ .Release.Name }}
2578
app.kubernetes.io/managed-by: {{ .Release.Service }}
2679
{{- end }}
2780

28-
81+
{{/*
82+
Selector labels for matching pods and services.
83+
Only includes name and instance for consistent selection.
84+
*/}}
2985
{{- define "chart.selectorLabels" -}}
3086
app.kubernetes.io/name: {{ include "chart.name" . }}
3187
app.kubernetes.io/instance: {{ .Release.Name }}
3288
{{- end }}
3389

3490

35-
{{- define "chart.hasMutatingWebhooks" -}}
36-
{{- $hasMutating := false }}
37-
{{- range . }}
38-
{{- if eq .type "mutating" }}
39-
$hasMutating = true }}{{- end }}
91+
92+
{{/*
93+
Common annotations for resources.
94+
Applies user-defined commonAnnotations from values.yaml.
95+
*/}}
96+
{{- define "chart.annotations" -}}
97+
{{- with .Values.commonAnnotations }}
98+
{{- toYaml . }}
99+
{{- end }}
40100
{{- end }}
41-
{{ $hasMutating }}}}{{- end }}
42101

43102

44-
{{- define "chart.hasValidatingWebhooks" -}}
45-
{{- $hasValidating := false }}
46-
{{- range . }}
47-
{{- if eq .type "validating" }}
48-
$hasValidating = true }}{{- end }}
103+
104+
{{/*
105+
Extended labels including both common labels and user-defined commonLabels.
106+
Combines chart.labels with any custom labels from values.yaml.
107+
*/}}
108+
{{- define "chart.extendedLabels" -}}
109+
{{- include "chart.labels" . }}
110+
{{- with .Values.commonLabels }}
111+
{{ toYaml . }}
112+
{{- end }}
49113
{{- end }}
50-
{{ $hasValidating }}}}{{- end }}
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: kustomize
7+
app.kubernetes.io/name: project
8+
name: {{ include "chart.fullname" . }}-metrics-certs
9+
namespace: {{ include "chart.namespaceName" . }}
10+
spec:
11+
dnsNames:
12+
- {{ include "chart.serviceName" (dict "suffix" "controller-manager-metrics-service" "context" .) }}.{{ include "chart.namespaceName" . }}.svc
13+
- {{ include "chart.serviceName" (dict "suffix" "controller-manager-metrics-service" "context" .) }}.{{ include "chart.namespaceName" . }}.svc.cluster.local
14+
issuerRef:
15+
kind: Issuer
16+
name: {{ include "chart.fullname" . }}-selfsigned-issuer
17+
secretName: metrics-server-cert
18+
{{- end }}
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: kustomize
7+
app.kubernetes.io/name: project
8+
name: {{ include "chart.fullname" . }}-selfsigned-issuer
9+
namespace: {{ include "chart.namespaceName" . }}
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: kustomize
7+
app.kubernetes.io/name: project
8+
name: {{ include "chart.fullname" . }}-serving-cert
9+
namespace: {{ include "chart.namespaceName" . }}
10+
spec:
11+
dnsNames:
12+
- {{ include "chart.serviceName" (dict "suffix" "webhook-service" "context" .) }}.{{ include "chart.namespaceName" . }}.svc
13+
- {{ include "chart.serviceName" (dict "suffix" "webhook-service" "context" .) }}.{{ include "chart.namespaceName" . }}.svc.cluster.local
14+
issuerRef:
15+
kind: Issuer
16+
name: {{ include "chart.fullname" . }}-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)