Skip to content

Commit 0e81141

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 5e331e7 commit 0e81141

File tree

230 files changed

+20709
-15948
lines changed

Some content is hidden

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

230 files changed

+20709
-15948
lines changed

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

Lines changed: 52 additions & 20 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@v6
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,39 +67,50 @@ 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
6180
helm repo update
6281
helm install prometheus-crds prometheus-community/prometheus-operator-crds
6382
64-
- name: Install cert-manager via Helm
83+
- name: Install cert-manager via Helm (wait for readiness)
6584
run: |
6685
helm repo add jetstack https://charts.jetstack.io
6786
helm repo update
68-
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set crds.enabled=true
69-
70-
- name: Wait for cert-manager to be ready
71-
run: |
72-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
73-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
74-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
87+
helm install cert-manager jetstack/cert-manager \
88+
--namespace cert-manager \
89+
--create-namespace \
90+
--set crds.enabled=true \
91+
--wait \
92+
--timeout 300s
7593
7694
- name: Render Helm chart for project-v4-with-plugins
7795
run: |
7896
helm template testdata/project-v4-with-plugins/dist/chart --namespace=project-v4-with-plugins-system
7997
8098
- name: Install Helm chart for project-v4-with-plugins
8199
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
100+
helm install my-release \
101+
testdata/project-v4-with-plugins/dist/chart \
102+
--namespace project-v4-with-plugins-system \
103+
--create-namespace \
104+
--set prometheus.enable=true
83105
84106
- name: Check Helm release status
85107
run: |
86108
helm status my-release --namespace project-v4-with-plugins-system
87109
88-
- name: Check Presence of ServiceMonitor
110+
- name: Delete kind cluster
111+
if: always()
89112
run: |
90-
kubectl wait --namespace project-v4-with-plugins-system --for=jsonpath='{.kind}'=ServiceMonitor servicemonitor/project-v4-with-plugins-controller-manager-metrics-monitor
113+
kind delete cluster || true
91114
92115
# Test scenario:
93116
# - scaffold project without creating webhooks,
@@ -97,7 +120,7 @@ jobs:
97120
# Command to use to scaffold project without creating webhooks and so no need to install cert manager:
98121
# - kubebuilder init
99122
# - kubebuilder create api --group example.com --version v1 --kind App --controller=true --resource=true
100-
# - kubebuilder edit --plugins=helm.kubebuilder.io/v1-alpha
123+
# - kubebuilder edit --plugins=helm.kubebuilder.io/v2-alpha
101124
test-helm-no-webhooks:
102125
runs-on: ubuntu-latest
103126
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
@@ -149,9 +172,18 @@ jobs:
149172

150173
- name: Deploy Helm chart without cert-manager
151174
working-directory: test-helm-no-webhooks
152-
run: helm install my-release ./dist/chart --create-namespace --namespace test-helm-no-webhooks-system
175+
run: |
176+
helm install my-release \
177+
./dist/chart \
178+
--create-namespace \
179+
--namespace test-helm-no-webhooks-system
153180
154181
- name: Verify deployment is working
155182
working-directory: test-helm-no-webhooks
156183
run: |
157184
helm status my-release --namespace test-helm-no-webhooks-system
185+
186+
- name: Delete kind cluster
187+
if: always()
188+
run: |
189+
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/.github/workflows/test-chart.yml

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,24 @@ jobs:
4646
run: |
4747
helm lint ./dist/chart
4848
49-
# TODO: Uncomment if cert-manager is enabled
50-
- name: Install cert-manager via Helm
49+
50+
- name: Install cert-manager via Helm (wait for readiness)
5151
run: |
5252
helm repo add jetstack https://charts.jetstack.io
5353
helm repo update
54-
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set crds.enabled=true
55-
56-
- name: Wait for cert-manager to be ready
57-
run: |
58-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
59-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
60-
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
54+
helm install cert-manager jetstack/cert-manager \
55+
--namespace cert-manager \
56+
--create-namespace \
57+
--set crds.enabled=true \
58+
--wait \
59+
--timeout 300s
6160
6261
# TODO: Uncomment if Prometheus is enabled
6362
# - name: Install Prometheus Operator CRDs
6463
# run: |
6564
# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
6665
# helm repo update
6766
# helm install prometheus-crds prometheus-community/prometheus-operator-crds
68-
#
69-
# - name: Install Prometheus via Helm
70-
# run: |
71-
# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
72-
# helm repo update
73-
# helm install prometheus prometheus-community/prometheus --namespace monitoring --create-namespace
74-
#
75-
# - name: Wait for Prometheus to be ready
76-
# run: |
77-
# kubectl wait --namespace monitoring --for=condition=available --timeout=300s deployment/prometheus-server
7867

7968
- name: Install Helm chart for project
8069
run: |
@@ -83,8 +72,3 @@ jobs:
8372
- name: Check Helm release status
8473
run: |
8574
helm status my-release --namespace project-system
86-
87-
# TODO: Uncomment if prometheus.enabled is set to true to confirm that the ServiceMonitor gets created
88-
# - name: Check Presence of ServiceMonitor
89-
# run: |
90-
# kubectl wait --namespace project-system --for=jsonpath='{.kind}'=ServiceMonitor servicemonitor/project-controller-manager-metrics-monitor

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: 49 additions & 21 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+
40+
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 }}
1554

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,11 @@ 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 }}
33-
34-
35-
{{- define "chart.hasMutatingWebhooks" -}}
36-
{{- $hasMutating := false }}
37-
{{- range . }}
38-
{{- if eq .type "mutating" }}
39-
$hasMutating = true }}{{- end }}
40-
{{- end }}
41-
{{ $hasMutating }}}}{{- end }}
42-
43-
44-
{{- define "chart.hasValidatingWebhooks" -}}
45-
{{- $hasValidating := false }}
46-
{{- range . }}
47-
{{- if eq .type "validating" }}
48-
$hasValidating = true }}{{- end }}
49-
{{- 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: {{ .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 }}

0 commit comments

Comments
 (0)