Skip to content

Conversation

camilamacedo86
Copy link
Member

@camilamacedo86 camilamacedo86 commented Aug 27, 2025

Introduce helm/v2-alpha Plugin: Dynamic Helm Chart Generation

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.

Key Features

  • Dynamic generation: Charts generated from actual kustomize output (dist/install.yaml)
  • Smart organization: Resources split into logical directories (metrics/, webhook/, cert-manager/, rbac/, crd/)
  • Conditional logic: Only includes resources that actually exist (certManager, webhooks, metrics)
  • Individual files: CRDs, RBAC, and certificates as separate files for better maintainability
  • Enhanced values.yaml: Dynamic configuration based on detected project features

Changes

  • Deprecated Helm v1-alpha in favour of v2
  • Add docs and tests for Helm v2
  • Update all samples
  • Address all feedback raised so far

Documentation

📖 Preview docs: https://deploy-preview-5058--kubebuilder.netlify.app/plugins/available/helm-v2-alpha

How to Review

  • Documentation: Check the docs link above for complete feature overview
  • Core implementation: Focus on pkg/plugins/optional/helm/v2alpha/ for the actual code changes
  • File changes context: Most changes under testdata/ and docs/ are sample updates with helm/v2-alpha
  • Test coverage: Comprehensive unit tests added with GitHub workflow integration for e2e testing via Prow
  • Play with in your solutions 🙏

Usage

# Generate Helm chart from kustomize output
kubebuilder edit --plugins=helm.kubebuilder.io/v2-alpha

# Custom output directory  
kubebuilder edit --plugins=helm.kubebuilder.io/v2-alpha --output=charts

Closes:

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 27, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 27, 2025
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Aug 27, 2025
@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 7 times, most recently from cb1ee39 to 1acfef2 Compare August 28, 2025 09:58
@camilamacedo86 camilamacedo86 changed the title WIP Add helm/v2-alpha and deprecated helm/v1-alpha ✨ Add helm/v2-alpha addressing all feedbacks and lessons learned. Deprecated helm/v1-alpha in favor of helm/v2-alpha Aug 28, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 28, 2025
@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 2 times, most recently from d9d172d to 2a5c5be Compare August 28, 2025 10:48
@camilamacedo86 camilamacedo86 changed the title ✨ Add helm/v2-alpha addressing all feedbacks and lessons learned. Deprecated helm/v1-alpha in favor of helm/v2-alpha WIP ✨ Add helm/v2-alpha addressing all feedbacks and lessons learned. Deprecated helm/v1-alpha in favor of helm/v2-alpha Aug 28, 2025
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 28, 2025
@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 4 times, most recently from 8203eed to 1cb0ec1 Compare August 29, 2025 18:18
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 29, 2025
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 29, 2025
@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 3 times, most recently from 16b3fca to 1863d2e Compare August 31, 2025 00:57
@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 2 times, most recently from ca52223 to 5610db1 Compare September 3, 2025 04:47
fs.BoolVar(&p.force, "force", false, "if true, regenerates all the files")
fs.StringVar(&p.manifestsFile, "manifests", DefaultManifestsFile,
"path to the YAML file containing Kubernetes manifests from kustomize output")
fs.StringVar(&p.outputDir, "output", DefaultOutputDir, "output directory for the generated Helm chart")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the flag called output-dir?
as you suggested in my initial PR here: #4891 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@bavarianbidi
Copy link
Contributor

bavarianbidi commented Sep 4, 2025

returned from PTO this week, will do a deeper review, starting next week, if wanted.

@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 4 times, most recently from 688b258 to 070f1d9 Compare September 5, 2025 06:52
@mkarlheim
Copy link

@camilamacedo86 many thanks for the PR. I've tested this version and it already looks very promising to my prior issues.

I just found some things to improve:

  1. The CRDs should be placed in the helm v3 suggested way: https://helm.sh/docs/chart_best_practices/custom_resource_definitions/. Just move the templates/crd folder to the root of the chart and name it "crds" and remove the flow control crd.enable.

This a special directory introduced especially for crds in helm. By doing this, helm will install these crds if not exists by default and you can skip this with --skip-diff

  1. Missing labels in manager.yaml
    The deployment is missing the include "chart.labels" in labels section and the include "chart.selectorLabels" in matchLabels section. The podTemplate is also missing "chart.labels".

  2. Missing labels in RBAC ClusterRoles (editor, viewer, admin);
    In the previous version there was this label section in editor-, viewer-, and admin-roles:
    labels: {{- include "chart.labels" . | nindent 4 }}
    This has now been replaced by fixed labels:
    labels: app.kubernetes.io/managed-by: {{ .Release.Service }} app.kubernetes.io/name: xxx
    and leads to differences in helm diff

I think using chart.labels defined in _helpers.tpl would be the preferred way.

  1. The following resources are missing labels section completely:
  • manager-role.yaml
  • metrics-auth-role.yaml
  • metrics-auth-rolebinding.yaml
  • metrics-reader.yaml

Besides these small findings I think the new plugin version helm/v2-alpha is a huge improvement!

Thank you

@camilamacedo86
Copy link
Member Author

camilamacedo86 commented Sep 6, 2025

Hi @mkarlheim

The chart. labels were added 👍 to allow adding extra labels.
However, note that these are the labels defined in Kustomize, and we should retain them.
The goal of Helm charts is to package the solution, so the labels should be identical.

Regards the CRDs, see:

Although Helm best practices recommend placing CRDs under a top-level crds/ directory, the Kubebuilder Helm plugin intentionally places them under templates/crd.**

This was a conscious design decision aafter lengthydiscussions in the design proposal and community threads (Slack discussion link).

The reason: CRDs in crds/ are not upgraded by Helm. Keeping them under templates/ ensures CRDs are correctly managed and updated together with releases, avoiding drift. While this means you cannot bundle custom resources directly in the chart (a current Helm limitation), it provides a more straightforward and more reliable workflow for projects generated by Kubebuilder. See; #3632 (comment)

Alternatively, here we could create a chart only for the CRDs, such as a two-chart flow (API first, then operator/app), with a simple readiness/wait check. However, if we change it, it would be better in a follow-up discussion where we can address it. Let's keep the cards under the templates, which is the most adopted approach.

@camilamacedo86 camilamacedo86 force-pushed the helm-change-manager branch 11 times, most recently from 73643da to bcfad58 Compare September 7, 2025 07:28
…`) 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
@camilamacedo86 camilamacedo86 changed the title WIP ✨ Add helm/v2-alpha addressing all feedbacks and aiming maintainability. Deprecated helm/v1-alpha in favor of helm/v2-alpha ✨ Add helm/v2-alpha addressing all feedbacks and aiming maintainability. Deprecated helm/v1-alpha in favor of helm/v2-alpha Sep 7, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 7, 2025
@@ -34,6 +34,7 @@ import (
autoupdatev1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha"
grafanav1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
helmv1alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
helmv2alpha1 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about enforcing this import-pattern via golangci-lint by the importas section (https://golangci-lint.run/docs/linters/configuration/#importas).
With that we get a consistent import section ...

I'm fine if we do this in a dedicated PR (have it on my list and happy to raise a PR after that)

@@ -34,6 +34,7 @@ import (
autoupdate "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
hemlv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
hemlv2alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hemlv2alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha"
helmv2alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v2alpha"
  • the line above

enforcing import-names might help here as well (see #5058 (comment))

@mkarlheim
Copy link

Hi @mkarlheim

The chart. labels were added 👍 to allow adding extra labels. However, note that these are the labels defined in Kustomize, and we should retain them. The goal of Helm charts is to package the solution, so the labels should be identical.

Regards the CRDs, see:

Although Helm best practices recommend placing CRDs under a top-level crds/ directory, the Kubebuilder Helm plugin intentionally places them under templates/crd.**

This was a conscious design decision aafter lengthydiscussions in the design proposal and community threads (Slack discussion link).

The reason: CRDs in crds/ are not upgraded by Helm. Keeping them under templates/ ensures CRDs are correctly managed and updated together with releases, avoiding drift. While this means you cannot bundle custom resources directly in the chart (a current Helm limitation), it provides a more straightforward and more reliable workflow for projects generated by Kubebuilder. See; #3632 (comment)

Alternatively, here we could create a chart only for the CRDs, such as a two-chart flow (API first, then operator/app), with a simple readiness/wait check. However, if we change it, it would be better in a follow-up discussion where we can address it. Let's keep the cards under the templates, which is the most adopted approach.

Thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants