Skip to content

Commit 688b258

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: OpenAI
1 parent dfadc63 commit 688b258

File tree

227 files changed

+20611
-15910
lines changed

Some content is hidden

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

227 files changed

+20611
-15910
lines changed

.github/workflows/test-helm-samples.yml

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,30 @@ jobs:
2020
- name: Checkout repository
2121
uses: actions/checkout@v5
2222

23+
- name: Enable Prometheus in kustomize (testdata sample)
24+
run: |
25+
sed -i 's/^#- \.\.\/prometheus/- ..\/prometheus/' testdata/project-v4-with-plugins/config/default/kustomization.yaml
26+
27+
- name: Build kubebuilder CLI
28+
run: make build
29+
2330
- name: Setup Go
2431
uses: actions/setup-go@v5
2532
with:
2633
go-version-file: go.mod
2734

35+
- name: Prepare project-v4-with-plugins
36+
run: |
37+
cd testdata/project-v4-with-plugins/
38+
go mod tidy
39+
make all
40+
41+
- name: Rebuild installer and regenerate Helm chart (v2-alpha)
42+
working-directory: testdata/project-v4-with-plugins
43+
run: |
44+
make build-installer
45+
../../bin/kubebuilder edit --plugins=helm/v2-alpha --force
46+
2847
- name: Install the latest version of kind
2948
run: |
3049
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
@@ -37,13 +56,6 @@ jobs:
3756
- name: Create kind cluster
3857
run: kind create cluster
3958

40-
- name: Prepare project-v4-with-plugins
41-
run: |
42-
cd testdata/project-v4-with-plugins/
43-
go mod tidy
44-
make docker-build IMG=project-v4-with-plugins:v0.1.0
45-
kind load docker-image project-v4-with-plugins:v0.1.0
46-
4759
- name: Install Helm
4860
run: |
4961
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
@@ -55,6 +67,13 @@ jobs:
5567
run: |
5668
helm lint testdata/project-v4-with-plugins/dist/chart
5769
70+
- name: Build project-v4-with-plugins
71+
run: |
72+
cd testdata/project-v4-with-plugins/
73+
go mod tidy
74+
make docker-build IMG=project-v4-with-plugins:v0.1.0
75+
kind load docker-image project-v4-with-plugins:v0.1.0
76+
5877
- name: Install Prometheus Operator CRDs
5978
run: |
6079
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
@@ -79,7 +98,11 @@ jobs:
7998
8099
- name: Install Helm chart for project-v4-with-plugins
81100
run: |
82-
helm install my-release testdata/project-v4-with-plugins/dist/chart --create-namespace --namespace project-v4-with-plugins-system --set prometheus.enable=true
101+
helm install my-release \
102+
testdata/project-v4-with-plugins/dist/chart \
103+
--namespace project-v4-with-plugins-system \
104+
--create-namespace \
105+
--set prometheus.enable=true
83106
84107
- name: Check Helm release status
85108
run: |
@@ -89,6 +112,11 @@ jobs:
89112
run: |
90113
kubectl wait --namespace project-v4-with-plugins-system --for=jsonpath='{.kind}'=ServiceMonitor servicemonitor/project-v4-with-plugins-controller-manager-metrics-monitor
91114
115+
- name: Delete kind cluster
116+
if: always()
117+
run: |
118+
kind delete cluster || true
119+
92120
# Test scenario:
93121
# - scaffold project without creating webhooks,
94122
# - deploy helm chart without installing cert manager;
@@ -97,7 +125,7 @@ jobs:
97125
# Command to use to scaffold project without creating webhooks and so no need to install cert manager:
98126
# - kubebuilder init
99127
# - kubebuilder create api --group example.com --version v1 --kind App --controller=true --resource=true
100-
# - kubebuilder edit --plugins=helm.kubebuilder.io/v1-alpha
128+
# - kubebuilder edit --plugins=helm.kubebuilder.io/v2-alpha
101129
test-helm-no-webhooks:
102130
runs-on: ubuntu-latest
103131
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
@@ -135,7 +163,7 @@ jobs:
135163
go mod init test-helm-no-webhooks
136164
kubebuilder init
137165
kubebuilder create api --group example.com --version v1 --kind App --controller=true --resource=true
138-
kubebuilder edit --plugins=helm.kubebuilder.io/v1-alpha
166+
kubebuilder edit --plugins=helm.kubebuilder.io/v2-alpha
139167
140168
- name: Build and load Docker image
141169
working-directory: test-helm-no-webhooks
@@ -149,9 +177,20 @@ jobs:
149177

150178
- name: Deploy Helm chart without cert-manager
151179
working-directory: test-helm-no-webhooks
152-
run: helm install my-release ./dist/chart --create-namespace --namespace test-helm-no-webhooks-system
180+
run: |
181+
helm install my-release \
182+
./dist/chart \
183+
--create-namespace \
184+
--namespace test-helm-no-webhooks-system \
185+
--set metrics.enable=false \
186+
--atomic
153187
154188
- name: Verify deployment is working
155189
working-directory: test-helm-no-webhooks
156190
run: |
157191
helm status my-release --namespace test-helm-no-webhooks-system
192+
193+
- name: Delete kind cluster
194+
if: always()
195+
run: |
196+
kind delete cluster || true

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: 54 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,48 @@
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 for generated references.
33+
Always uses the Helm release namespace.
34+
*/}}
35+
{{- define "chart.namespaceName" -}}
36+
{{ .Release.Namespace }}
37+
{{- end }}
38+
39+
1540

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

28-
71+
{{/*
72+
Selector labels for matching pods and services.
73+
Only includes name and instance for consistent selection.
74+
*/}}
2975
{{- define "chart.selectorLabels" -}}
3076
app.kubernetes.io/name: {{ include "chart.name" . }}
3177
app.kubernetes.io/instance: {{ .Release.Name }}
3278
{{- end }}
3379

3480

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

44-
{{- define "chart.hasValidatingWebhooks" -}}
45-
{{- $hasValidating := false }}
46-
{{- range . }}
47-
{{- if eq .type "validating" }}
48-
$hasValidating = true }}{{- end }}
49-
{{- end }}
50-
{{ $hasValidating }}}}{{- end }}
82+
{{/*
83+
Note: Extra labels and annotations functionality removed for simplicity.
84+
The chart focuses on converting kustomize output to Helm templates.
85+
Users can still add labels/annotations directly in their kustomize manifests.
86+
*/}}
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 }}
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 }}

0 commit comments

Comments
 (0)